List of usage examples for java.io Reader mark
public void mark(int readAheadLimit) throws IOException
From source file:Main.java
public static void main(String[] args) { try {// ww w . j a v a 2 s . com String s = "tutorial from java2s.com"; Reader reader = new StringReader(s); for (int i = 0; i < 5; i++) { char c = (char) reader.read(); System.out.println(c); } reader.mark(10); for (int i = 0; i < 6; i++) { char c = (char) reader.read(); System.out.println(c); } reader.reset(); for (int i = 0; i < 6; i++) { char c = (char) reader.read(); System.out.println(c); } reader.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { String s = "tutorial from java2s.com"; Reader reader = new StringReader(s); try {/* ww w. j a v a 2 s .c o m*/ for (int i = 0; i < 5; i++) { char c = (char) reader.read(); System.out.print(c); } reader.mark(10); for (int i = 0; i < 6; i++) { char c = (char) reader.read(); System.out.println(c); } reader.reset(); for (int i = 0; i < 6; i++) { char c = (char) reader.read(); System.out.println(c); } reader.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
private static Reader getReader(InputStream is) throws IOException { Reader reader = new BufferedReader(new InputStreamReader(is)); char c[] = "<?".toCharArray(); int pos = 0;/* w w w . jav a 2 s . c o m*/ reader.mark(LOOKAHEAD); while (true) { int value = reader.read(); // Check to see if we hit the end of the stream. if (value == -1) { throw new IOException("Encounter end of stream before start of XML."); } else if (value == c[pos]) { pos++; } else { if (pos > 0) { pos = 0; } reader.mark(LOOKAHEAD); } if (pos == c.length) { // We found the character set we were looking for. reader.reset(); break; } } return reader; }
From source file:de.quist.samy.remocon.RemoteSession.java
private static char[] readCharArray(Reader reader) throws IOException { if (reader.markSupported()) reader.mark(1024); int length = reader.read(); int delimiter = reader.read(); if (delimiter != 0) { if (reader.markSupported()) reader.reset();/*from w w w . j av a 2s . co m*/ 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 2 s . 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; }//ww w.j ava2s . c om 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 .j av a2 s . com * @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:net.metanotion.json.StreamingParser.java
private String lexExp(final Reader in) throws IOException { in.mark(MAX_BUFFER); int c = in.read(); if (Character.toLowerCase(c) == 'e') { c = in.read();/*from w w w . ja va 2s .co m*/ if (c == '+') { return "e+" + lexDigits(in); } else if (c == '-') { return "e-" + lexDigits(in); } else if (Character.isDigit(c)) { return (new String(Character.toChars(c))) + lexDigits(in); } else if (c == -1) { throw new ParserException("Unexpected end of stream"); } else { throw new ParserException( "Expected exponent, instead found: '" + (new String(Character.toChars(c))) + QUOTE); } } else { in.reset(); return ""; } }
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); final int c = in.read(); if (Character.isDigit(c)) { sb.append(Character.toChars(c)); } else {//from www. ja v a 2s.c om in.reset(); if (sb.length() == 0) { throw new ParserException(EXPECTED_DIGIT); } return sb.toString(); } } }
From source file:com.webcohesion.ofx4j.io.BaseOFXReader.java
/** * Parse the reader, including the headers. * * @param reader The reader./*from w w w . j av a 2 s .c o m*/ */ public void parse(Reader reader) throws IOException, OFXParseException { //make sure we're buffering... reader = new BufferedReader(reader); StringBuilder header = new StringBuilder(); final char[] firstElementStart = getFirstElementStart(); final char[] buffer = new char[firstElementStart.length]; reader.mark(firstElementStart.length); int ch = reader.read(buffer); while ((ch != -1) && (!Arrays.equals(buffer, firstElementStart))) { if (!contains(buffer, '<')) { //if the buffer contains a '<', then we might already have marked the beginning. reader.mark(firstElementStart.length); } ch = reader.read(); char shifted = shiftAndAppend(buffer, (char) ch); header.append(shifted); } if (ch == -1) { throw new OFXParseException("Invalid OFX: no root <OFX> element!"); } else { Matcher matcher = OFX_2_PROCESSING_INSTRUCTION_PATTERN.matcher(header); if (matcher.find()) { if (LOG.isInfoEnabled()) { LOG.info("Processing OFX 2 header..."); } processOFXv2Headers(matcher.group(1)); reader.reset(); parseV2FromFirstElement(reader); } else { LOG.info("Processing OFX 1 headers..."); processOFXv1Headers(header.toString()); reader.reset(); parseV1FromFirstElement(reader); } } }