Example usage for java.io Reader read

List of usage examples for java.io Reader read

Introduction

In this page you can find the example usage for java.io Reader read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:com.norconex.importer.handler.transformer.AbstractStringTransformer.java

@Override
protected final void transformTextDocument(String reference, Reader input, Writer output,
        ImporterMetadata metadata, boolean parsed) throws IOException {

    // Initial size is half free memory, considering chars take 2 bytes
    StringBuilder b = new StringBuilder((int) (MemoryUtil.getFreeMemory() / STRING_TOTAL_MEMORY_DIVIDER));
    int i;/*from  w  w  w  . j a va2  s  .  c  o m*/
    while ((i = input.read()) != -1) {
        char ch = (char) i;
        b.append(ch);
        if (b.length() * 2 % READ_CHUNK_SIZE == 0 && isTakingTooMuchMemory(b)) {
            transformStringContent(reference, b, metadata, parsed, true);
            BufferUtil.flushBuffer(b, output, true);
        }
    }
    if (b.length() > 0) {
        transformStringContent(reference, b, metadata, parsed, false);
        BufferUtil.flushBuffer(b, output, false);
    }
    b.setLength(0);
    b = null;
}

From source file:name.vysoky.xhtml.Corrector.java

private void process(File inputFile, File outputFile, String codePage) {
    Reader reader = null;
    Writer writer = null;/*from  ww w  . j a v  a2  s .c  om*/
    try {
        reader = new InputStreamReader(new BufferedInputStream(new FileInputStream(inputFile)), codePage);
        writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(outputFile)), codePage);
        char c;
        int b = reader.read();
        while (b != -1) {
            c = (char) b;
            writer.write(processChar(c));
            b = reader.read();
        }
        if (builder.length() > 0)
            writer.write(builder.toString().toCharArray());
    } catch (IOException e) {
        System.err.println("Unable write streams!");
        e.printStackTrace();
    } finally {
        try {
            if (reader != null)
                reader.close();
            if (writer != null)
                writer.close();
        } catch (IOException e) {
            System.err.println("Unable to close streams!");
            e.printStackTrace();
        }
    }
}

From source file:com.seajas.search.contender.service.modifier.ModifierFilterProcessorTest.java

/**
 * Test the modifier filter asExpression = true parser.
 * //from  w w  w  .j a v a2 s .c om
 * @throws IOException
 */
@Test
public void testExpressionFilterMultiLine() throws IOException {
    ModifierFilterProcessor processor = new ModifierFilterProcessor();

    ModifierFilter modifierFilter = new ModifierFilter();

    modifierFilter.setId(-1);
    modifierFilter.setModifierId(-1);
    modifierFilter.setFragmentStart("<item>[^<]*<title><\\!\\[CDATA\\[AD\\:.*</title>");
    modifierFilter.setFragmentEnd("<\\/item>");
    modifierFilter.setIsExpression(true);

    Reader result = processor.process(modifierFilter,
            new StringReader("Foo<item>\n\t  <title><![CDATA[AD: FooBar]]></title>\n"
                    + "\t<link>http://foo/</link>\n\t<description><![CDATA[<div></div>]]></description>\n\t</item>Bar"));

    StringBuffer buffer = new StringBuffer();

    for (int c; (c = result.read()) != -1;)
        buffer.append((char) c);

    assertEquals(buffer.toString(), "Foo<item>\n\t  <title><![CDATA[AD: FooBar]]></title></item>Bar");
}

From source file:com.norconex.importer.handler.tagger.AbstractStringTagger.java

@Override
protected final void tagTextDocument(String reference, Reader input, ImporterMetadata metadata, boolean parsed)
        throws ImporterHandlerException {

    try {/*from   w  ww.  ja v a2s  .c o m*/
        // Initial size is half free memory, considering chars take 2 bytes
        StringBuilder b = new StringBuilder((int) (MemoryUtil.getFreeMemory() / STRING_TOTAL_MEMORY_DIVIDER));
        int i;
        while ((i = input.read()) != -1) {
            char ch = (char) i;
            b.append(ch);
            if (b.length() * 2 % READ_CHUNK_SIZE == 0 && isTakingTooMuchMemory(b)) {
                tagStringContent(reference, b, metadata, parsed, true);
                BufferUtil.flushBuffer(b, null, true);
            }
        }
        if (b.length() > 0) {
            tagStringContent(reference, b, metadata, parsed, false);
            BufferUtil.flushBuffer(b, null, false);
        }
        b.setLength(0);
        b = null;
    } catch (IOException e) {
        throw new ImporterHandlerException("Cannot tag text document.", e);
    }

}

From source file:fedroot.dacs.swingdemo.DacsClientFrame.java

/**
 * Loads contents of the input stream in a separate thread.
 * @param is input stream to be rendered as HTML
 *///from  ww  w.  j a va2  s  .  co  m
private void loadPage(final String contenttype, final InputStream is) {
    // create a new thread to load the URL from
    final StringBuffer sb = new StringBuffer();
    new Thread() {

        public void run() {

            try {
                // chain the InputStream to a Reader
                Reader r = new InputStreamReader(new BufferedInputStream(is));
                int c;
                while ((c = r.read()) != -1) {
                    sb.append((char) c);
                }
                is.close();
                if (is != null) {
                    // set the HTML on the UI thread
                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                            setDocumentContent(contenttype, sb.toString());
                        }
                    });
                }
            } catch (IOException ex) {
                DacsClientFrame.logger.severe(ex.getMessage());
                //  ex.printStackTrace();
            } finally {
                setDocumentContent(contenttype, sb.toString());
            }
        }
    }.start();
}

From source file:fedroot.dacs.swingdemo.DemoFrame.java

/**
 * Loads contents of the input stream in a separate thread.
 * @param is input stream to be rendered as HTML
 *//*  w w w  .  j a  v  a  2  s. co  m*/
private void loadPage(final String contenttype, final InputStream is) {
    // create a new thread to load the URL from
    final StringBuffer sb = new StringBuffer();
    new Thread() {

        public void run() {

            try {
                // chain the InputStream to a Reader
                Reader r = new InputStreamReader(new BufferedInputStream(is));
                int c;
                while ((c = r.read()) != -1) {
                    sb.append((char) c);
                }
                is.close();
                if (is != null) {
                    // set the HTML on the UI thread
                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                            setDocumentContent(contenttype, sb.toString());
                        }
                    });
                }
            } catch (IOException ex) {
                DemoFrame.logger.severe(ex.getMessage());
                //  ex.printStackTrace();
            } finally {
                setDocumentContent(contenttype, sb.toString());
            }
        }
    }.start();
}

From source file:org.osiam.client.OsiamUserMeTest.java

private User get_expected_user() throws Exception {
    Reader reader = null;
    StringBuilder jsonUser = null;
    User expectedUser;//from www  .ja v  a2 s .  co  m
    try {
        reader = new FileReader("src/test/resources/__files/user_" + USER_ID + ".json");
        jsonUser = new StringBuilder();
        for (int c; (c = reader.read()) != -1;)
            jsonUser.append((char) c);
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
        }
    }

    expectedUser = new ObjectMapper().readValue(jsonUser.toString(), User.class);
    return expectedUser;
}

From source file:com.norconex.importer.handler.filter.AbstractStringFilter.java

@Override
protected final boolean isTextDocumentMatching(String reference, Reader input, ImporterMetadata metadata,
        boolean parsed) throws ImporterHandlerException {

    try {/*w ww. j  ava2  s . c  o  m*/
        // Initial size is half free memory, considering chars take 2 bytes
        StringBuilder b = new StringBuilder((int) (MemoryUtil.getFreeMemory() / STRING_TOTAL_MEMORY_DIVIDER));
        int i;
        while ((i = input.read()) != -1) {
            char ch = (char) i;
            b.append(ch);
            if (b.length() * 2 % READ_CHUNK_SIZE == 0 && isTakingTooMuchMemory(b)) {
                boolean matched = isStringContentMatching(reference, b, metadata, parsed, true);
                BufferUtil.flushBuffer(b, null, true);
                if (matched) {
                    return true;
                }
            }
        }
        if (b.length() > 0) {
            boolean matched = isStringContentMatching(reference, b, metadata, parsed, false);
            BufferUtil.flushBuffer(b, null, false);
            if (matched) {
                return true;
            }
        }
        b.setLength(0);
        b = null;
    } catch (IOException e) {
        throw new ImporterHandlerException("Cannot tag text document.", e);
    }
    return false;
}

From source file:net.metanotion.json.StreamingParser.java

private String lexDigits(final Reader in) throws IOException {
    final StringBuilder sb = new StringBuilder();
    while (true) {
        in.mark(MAX_BUFFER);/*ww w  . j  a  va2  s  . c  om*/
        final int c = in.read();
        if (Character.isDigit(c)) {
            sb.append(Character.toChars(c));
        } else {
            in.reset();
            if (sb.length() == 0) {
                throw new ParserException(EXPECTED_DIGIT);
            }
            return sb.toString();
        }
    }
}

From source file:net.metanotion.json.StreamingParser.java

private Lexeme lexString(final Reader in) throws IOException {
    final StringBuilder sb = new StringBuilder();
    boolean escape = false;
    while (true) {
        final int c = in.read();
        if (c == -1) {
            throw new ParserException("Expected a character instead of end of stream");
        }/*from   w ww .j  a v  a  2  s  .co m*/
        final String cbuf = new String(Character.toChars(c));
        if (("\"".equals(cbuf)) && !escape) {
            return new Lexeme(Token.STRING, StringEscapeUtils.unescapeJson(sb.toString()));
        } else if ("\\".equals(cbuf)) {
            escape = true;
            sb.append(cbuf);
        } else {
            escape = false;
            sb.append(cbuf);
        }
    }
}