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.seajas.search.contender.service.modifier.ModifierFilterProcessorTest.java

/**
 * Test the modifier with asExpression = true, when applied to actual sample content.
 * //from  w  w  w  .  jav a 2 s .  c o m
 * @throws IOException
 */
@Test
public void testExpressionFromFile() throws IOException {
    String fileContent = IOUtils.toString(new FileInputStream("support/examples/yomiuri.html"), "Shift_JIS");

    ModifierFilterProcessor processor = new ModifierFilterProcessor();
    ModifierFilter modifierFilter = new ModifierFilter();

    modifierFilter.setId(-1);
    modifierFilter.setModifierId(-1);
    modifierFilter.setFragmentStart("<body [^>]*>");
    modifierFilter.setFragmentEnd("<!--// article_start //-->");
    modifierFilter.setIsExpression(true);

    Reader result = processor.process(modifierFilter, new StringReader(fileContent));

    StringBuffer buffer = new StringBuffer();

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

    // Expected result

    IOUtils.write(buffer, new FileOutputStream("support/examples/yomiuri.actual.html"), "Shift_JIS");

    assertEquals(buffer.toString(),
            IOUtils.toString(new FileInputStream("support/examples/yomiuri.expected.html"), "Shift_JIS"));
}

From source file:com.streamsets.pipeline.lib.csv.CsvParser.java

private long skipLines(Reader reader, int lines) throws IOException {
    int count = 0;
    int skipped = 0;
    while (skipped < lines) {
        int c = reader.read();
        if (c == -1) {
            throw new IOException(Utils.format("Could not skip '{}' lines, reached EOF", lines));
        }// w  w w.j a  va2 s. c o m
        // this is enough to handle \n and \r\n EOL files
        if (c == '\n') {
            skipped++;
        }
        count++;
    }
    return count;
}

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

/**
 * Test the modifier filter asExpression = false parser.
 * //w  ww. j a v a 2  s .  com
 * @throws IOException
 */
@Test
public void testStaticFilter() throws IOException {
    ModifierFilterProcessor processor = new ModifierFilterProcessor();

    ModifierFilter modifierFilter = new ModifierFilter();

    modifierFilter.setId(-1);
    modifierFilter.setModifierId(-1);
    modifierFilter.setFragmentStart("<foo>");
    modifierFilter.setFragmentEnd("</foo>");
    modifierFilter.setIsExpression(false);

    Reader result = processor.process(modifierFilter, new StringReader("abc<foo>def</foo>ghi"));

    StringBuffer buffer = new StringBuffer();

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

    assertEquals(buffer.toString(), "abc<foo></foo>ghi");
}

From source file:io.sqp.core.jackson.JacksonMessageDecoder.java

@Override
public SqpMessage decode(Reader message) throws DecodingException {
    // TODO: check if we need to close the Reader
    // pass a "ValueReader" since we don't have dynamic type dispatch
    try {/*from  ww  w  .  java2 s. co m*/
        MessageType type = MessageType.fromId((char) message.read());
        return decode(type, DataFormat.Text, (mapper, msgClass) -> mapper.readValue(message, msgClass));
    } catch (IOException e) {
        throw new DecodingException("Failed to read the message identifier: " + e.getMessage(), e);
    }
}

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

/**
 * Test the modifier filter asExpression = true parser.
 * /*from  w  w w  .  j a  va2 s  . c o  m*/
 * @throws IOException
 */
@Test
public void testExpressionFilter() throws IOException {
    ModifierFilterProcessor processor = new ModifierFilterProcessor();

    ModifierFilter modifierFilter = new ModifierFilter();

    modifierFilter.setId(-1);
    modifierFilter.setModifierId(-1);
    modifierFilter.setFragmentStart("\\<foo\\>");
    modifierFilter.setFragmentEnd("\\<\\/foo\\>");
    modifierFilter.setIsExpression(true);

    Reader result = processor.process(modifierFilter, new StringReader("abc<foo>def</foo>ghi\n<foo>xy</foo>z"));

    StringBuffer buffer = new StringBuffer();

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

    assertEquals(buffer.toString(), "abc<foo></foo>ghi\n<foo></foo>z");
}

From source file:cn.fintecher.print.web.util.JacksonDecoder.java

@Override
public Object decode(Response response, Type type) throws IOException {
    if (response.status() == 404)
        return Util.emptyValueOf(type);
    if (response.body() == null)
        return null;
    Reader reader = response.body().asReader();
    if (!reader.markSupported()) {
        reader = new BufferedReader(reader, 1);
    }//from w w w .  j a va 2s. c o  m
    try {
        // Read the first byte to see if we have any data
        reader.mark(1);
        if (reader.read() == -1) {
            return null; // Eagerly returning null avoids "No content to map due to end-of-input"
        }
        reader.reset();
        return mapper.readValue(reader, mapper.constructType(type));
    } catch (RuntimeJsonMappingException e) {
        if (e.getCause() != null && e.getCause() instanceof IOException) {
            throw IOException.class.cast(e.getCause());
        }
        throw e;
    }
}

From source file:org.wso2.msf4j.client.codec.MSF4JJacksonDecoder.java

@Override
public Object decode(Response response, Type type) throws IOException, FeignException {
    if (response.body() == null) {
        return null;
    }//from  w  w w  .j  a  va  2 s . com
    Reader reader = response.body().asReader();
    if (!reader.markSupported()) {
        reader = new BufferedReader(reader, 1);
    }
    try {
        // Read the first byte to see if we have any data
        reader.mark(1);
        if (reader.read() == -1) {
            return null; // Eagerly returning null avoids "No content to map due to end-of-input"
        }
        reader.reset();
        return mapper.readValue(reader, mapper.constructType(type));
    } catch (RuntimeJsonMappingException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause instanceof IOException) {
            throw IOException.class.cast(cause);
        }
        throw e;
    }
}

From source file:io.instacount.client.decoders.AbstractInstacountDecoder.java

/**
 * Helper method to construct an instance of {@link Reader} for reading the JSON response from Instacount.
 *
 * @param response//  w w w  .  j  a v a2s .c o  m
 * @return
 * @throws IOException
 */
protected Optional<Reader> constructReader(final Response response) throws IOException {
    Preconditions.checkNotNull(response);
    Preconditions.checkNotNull(response.body());

    Reader reader = response.body().asReader();
    if (!reader.markSupported()) {
        reader = new BufferedReader(reader, 1);
    }
    try {
        // Read the first byte to see if we have any data
        reader.mark(1);
        if (reader.read() == -1) {
            // Eagerly returning null avoids "No content to map due to end-of-input"
            return Optional.absent();
        }
        reader.reset();
    } catch (RuntimeJsonMappingException e) {
        if (e.getCause() != null && e.getCause() instanceof IOException) {
            throw IOException.class.cast(e.getCause());
        }
        throw e;
    }

    return Optional.fromNullable(reader);
}

From source file:com.aoyetech.fee.commons.utils.IOUtils.java

/**
 * Compare the contents of two Readers to determine if they are equal or
 * not./* w w  w . j a  v a2  s  .c  o  m*/
 * <p>
 * This method buffers the input internally using
 * <code>BufferedReader</code> if they are not already buffered.
 * 
 * @param input1 the first reader
 * @param input2 the second reader
 * @return true if the content of the readers are equal or they both don't
 *         exist, false otherwise
 * @throws NullPointerException if either input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static boolean contentEquals(Reader input1, Reader input2) throws IOException {
    if (!(input1 instanceof BufferedReader)) {
        input1 = new BufferedReader(input1);
    }
    if (!(input2 instanceof BufferedReader)) {
        input2 = new BufferedReader(input2);
    }

    int ch = input1.read();
    while (-1 != ch) {
        final int ch2 = input2.read();
        if (ch != ch2) {
            return false;
        }
        ch = input1.read();
    }

    final int ch2 = input2.read();
    return (ch2 == -1);
}

From source file:IOUtils.java

/**
 * Compare the contents of two Readers to determine if they are equal or
 * not./*w w  w. java2 s .  c  o  m*/
 * <p>
 * This method buffers the input internally using
 * <code>BufferedReader</code> if they are not already buffered.
 *
 * @param input1  the first reader
 * @param input2  the second reader
 * @return true if the content of the readers are equal or they both don't
 * exist, false otherwise
 * @throws NullPointerException if either input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static boolean contentEquals(Reader input1, Reader input2) throws IOException {
    if (!(input1 instanceof BufferedReader)) {
        input1 = new BufferedReader(input1);
    }
    if (!(input2 instanceof BufferedReader)) {
        input2 = new BufferedReader(input2);
    }

    int ch = input1.read();
    while (-1 != ch) {
        int ch2 = input2.read();
        if (ch != ch2) {
            return false;
        }
        ch = input1.read();
    }

    int ch2 = input2.read();
    return (ch2 == -1);
}