Search in sources :

Example 71 with Reader

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);
    }
}
Also used : InputStream(java.io.InputStream) Reader(java.io.Reader) SAXParseException(org.xml.sax.SAXParseException)

Example 72 with Reader

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();
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader)

Example 73 with Reader

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());
}
Also used : StringWriter(java.io.StringWriter) FilterReader(java.io.FilterReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) FilterWriter(java.io.FilterWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 74 with Reader

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());
}
Also used : FilterReader(java.io.FilterReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader)

Example 75 with Reader

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));
}
Also used : StringReader(java.io.StringReader) FilterReader(java.io.FilterReader) Reader(java.io.Reader)

Aggregations

Reader (java.io.Reader)1734 InputStreamReader (java.io.InputStreamReader)627 StringReader (java.io.StringReader)565 IOException (java.io.IOException)482 BufferedReader (java.io.BufferedReader)293 Test (org.junit.Test)253 InputStream (java.io.InputStream)252 TokenStream (org.apache.lucene.analysis.TokenStream)171 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)159 Connection (java.sql.Connection)138 FileReader (java.io.FileReader)129 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)126 FileInputStream (java.io.FileInputStream)124 File (java.io.File)117 BeforeClass (org.junit.BeforeClass)99 Tokenizer (org.apache.lucene.analysis.Tokenizer)91 StringWriter (java.io.StringWriter)89 ArrayList (java.util.ArrayList)86 SqlSession (org.apache.ibatis.session.SqlSession)83 List (java.util.List)76