use of java.io.Reader in project XobotOS by xamarin.
the class ExpatParser method parseExternalEntity.
/**
* Parses the the external entity provided by the input source.
*/
private void parseExternalEntity(ExpatParser entityParser, InputSource inputSource) throws IOException, SAXException {
/*
* Expat complains if the external entity isn't wrapped with a root
* element so we add one and ignore it later on during parsing.
*/
// Try the character stream.
Reader reader = inputSource.getCharacterStream();
if (reader != null) {
try {
entityParser.append("<externalEntity>");
entityParser.parseFragment(reader);
entityParser.append("</externalEntity>");
} finally {
IoUtils.closeQuietly(reader);
}
return;
}
// Try the byte stream.
InputStream in = inputSource.getByteStream();
if (in != null) {
try {
entityParser.append("<externalEntity>".getBytes(entityParser.encoding));
entityParser.parseFragment(in);
entityParser.append("</externalEntity>".getBytes(entityParser.encoding));
} finally {
IoUtils.closeQuietly(in);
}
return;
}
// Make sure we use the user-provided systemId.
String systemId = inputSource.getSystemId();
if (systemId == null) {
// TODO: We could just try our systemId here.
throw new ParseException("No input specified.", locator);
}
// Try the system id.
in = openUrl(systemId);
try {
entityParser.append("<externalEntity>".getBytes(entityParser.encoding));
entityParser.parseFragment(in);
entityParser.append("</externalEntity>".getBytes(entityParser.encoding));
} finally {
IoUtils.closeQuietly(in);
}
}
use of java.io.Reader in project xUtils3 by wyouflf.
the class IOUtil method readStr.
public static String readStr(InputStream in, String charset) throws IOException {
if (TextUtils.isEmpty(charset))
charset = "UTF-8";
if (!(in instanceof BufferedInputStream)) {
in = new BufferedInputStream(in);
}
Reader reader = new InputStreamReader(in, charset);
StringBuilder sb = new StringBuilder();
char[] buf = new char[1024];
int len;
while ((len = reader.read(buf)) >= 0) {
sb.append(buf, 0, len);
}
return sb.toString();
}
use of java.io.Reader in project guava by hceylan.
the class CharStreamsTest method testAlwaysCloses.
public void testAlwaysCloses() throws IOException {
CheckCloseSupplier.Input<Reader> okRead = newCheckReader(CharStreams.newReaderSupplier(TEXT));
CheckCloseSupplier.Output<Writer> okWrite = newCheckWriter(new OutputSupplier<Writer>() {
@Override
public Writer getOutput() {
return new StringWriter();
}
});
CheckCloseSupplier.Input<Reader> brokenRead = newCheckReader(BROKEN_READ);
CheckCloseSupplier.Output<Writer> brokenWrite = newCheckWriter(BROKEN_WRITE);
CharStreams.copy(okRead, okWrite);
assertTrue(okRead.areClosed());
assertTrue(okWrite.areClosed());
try {
CharStreams.copy(okRead, brokenWrite);
fail("expected exception");
} catch (Exception e) {
assertEquals("broken write", e.getMessage());
}
assertTrue(okRead.areClosed());
assertTrue(brokenWrite.areClosed());
try {
CharStreams.copy(brokenRead, okWrite);
fail("expected exception");
} catch (Exception e) {
assertEquals("broken read", e.getMessage());
}
assertTrue(brokenRead.areClosed());
assertTrue(okWrite.areClosed());
try {
CharStreams.copy(brokenRead, brokenWrite);
fail("expected exception");
} catch (Exception e) {
assertEquals("broken read", e.getMessage());
}
assertTrue(brokenRead.areClosed());
assertTrue(brokenWrite.areClosed());
assertEquals(TEXT, CharStreams.toString(okRead));
assertTrue(okRead.areClosed());
try {
CharStreams.toString(brokenRead);
fail("expected exception");
} catch (Exception e) {
assertEquals("broken read", e.getMessage());
}
assertTrue(brokenRead.areClosed());
try {
CharStreams.write("hello world", brokenWrite);
fail("expected exception");
} catch (Exception e) {
assertEquals("broken write", e.getMessage());
}
assertTrue(brokenWrite.areClosed());
}
use of java.io.Reader in project guava by hceylan.
the class CharStreamsTest method testSkipFully_blockingRead.
public void testSkipFully_blockingRead() throws IOException {
Reader reader = new NonSkippingReader("abcdef");
CharStreams.skipFully(reader, 6);
assertEquals(-1, reader.read());
}
use of java.io.Reader in project guava by hceylan.
the class MultiReaderTest method testSimple.
public void testSimple() throws Exception {
String testString = "abcdefgh";
InputSupplier<Reader> supplier = newReader(testString);
@SuppressWarnings("unchecked") Reader joinedReader = CharStreams.join(supplier, supplier).getInput();
String expectedString = testString + testString;
assertEquals(expectedString, CharStreams.toString(joinedReader));
}
Aggregations