Example usage for java.io Reader markSupported

List of usage examples for java.io Reader markSupported

Introduction

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

Prototype

public boolean markSupported() 

Source Link

Document

Tells whether this stream supports the mark() operation.

Usage

From source file:Main.java

public static void main(String[] args) {

    String s = "tutorial from java2s.com";

    // create a new StringReader
    Reader reader = new StringReader(s);

    try {//from  w  ww.  j  a v a  2  s.  c  om
        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.println(c);
        }
        System.out.println(reader.markSupported());
        reader.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:de.quist.samy.remocon.RemoteSession.java

private static char[] readCharArray(Reader reader) throws IOException {
    if (reader.markSupported())
        reader.mark(1024);// w  w w  .ja  va2 s. c o m
    int length = reader.read();
    int delimiter = reader.read();
    if (delimiter != 0) {
        if (reader.markSupported())
            reader.reset();
        throw new IOException("Unsupported reply exception");
    }
    char[] buffer = new char[length];
    reader.read(buffer);
    return buffer;
}

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);
    }//  w  w  w  . j  av  a  2s . co  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  ava2s  . 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/*from  www .  java2 s  . co 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:org.apache.tika.parser.csv.CSVSniffer.java

List<CSVResult> sniff(Reader reader) throws IOException {
    if (!reader.markSupported()) {
        reader = new BufferedReader(reader);
    }/*from   w  ww.j a va2s .  c o  m*/
    List<CSVResult> ret = new ArrayList<>();
    for (char delimiter : delimiters) {
        reader.mark(markLimit);
        try {
            CSVResult result = new Snifflet(delimiter).sniff(reader);
            ret.add(result);
        } finally {
            reader.reset();
        }
    }
    Collections.sort(ret);
    return ret;
}

From source file:za.co.clock24.dsvparser.DsvParser.java

/**
 * If you want to specify your own record parser to provide you with read-made records
 * in the results, then use this constructor.
 * //from   w  w  w . j  av  a2 s  .c o  m
 * @param reader
 * @param parser
 */
public DsvParser(Reader reader, DsvRecordParser<T> parser) {
    if (!reader.markSupported())
        this.reader = new BufferedReader(reader);
    else
        this.reader = reader;
    this.dsvRecordParser = parser;
}

From source file:com.xie.javacase.json.JSONTokener.java

/**
 * Construct a JSONTokener from a string.
 *
 * @param reader     A reader.//from w  w w  .  j  a  va2s.c o m
 */
public JSONTokener(Reader reader) {
    this.reader = reader.markSupported() ? reader : new BufferedReader(reader);
    this.useLastChar = false;
    this.index = 0;
}

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

/** Parse a JSON encoded stream and emit the events to the handler provided and return the result of finishing the
   stream.//from  w ww  .  ja v a2  s .  c  o  m
   @param <T> The type of value produced by the final call to the handler.
   @param file The stream of JSON encoded text to parse.
   @param handler The handler to emit events to.
   @return The final value produced by the call to .finish().
   @throws IOException if an IO error while reading from the reader.
*/
public <T> T parse(final Reader file, final Handler<T> handler) throws IOException {
    final Reader in = file.markSupported() ? file : new BufferedReader(file);
    return parseJson(in, handler.start()).finish();
}

From source file:com.jskaleel.xml.JSONTokener.java

/**
 * Construct a JSONTokener from a Reader.
 *
 * @param reader     A reader./*from w w w .j a v a2 s . c o  m*/
 */
public JSONTokener(Reader reader) {
    this.reader = reader.markSupported() ? reader : new BufferedReader(reader);
    this.eof = false;
    this.usePrevious = false;
    this.previous = 0;
    this.index = 0;
    this.character = 1;
    this.line = 1;
}