List of usage examples for java.io PushbackInputStream read
public int read() throws IOException
From source file:Main.java
public static void main(String[] args) { byte[] arrByte = new byte[1024]; byte[] byteArray = new byte[] { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' }; try {//from www . jav a2 s . c o m InputStream is = new ByteArrayInputStream(byteArray); PushbackInputStream pis = new PushbackInputStream(is); System.out.println(pis.available()); for (int i = 0; i < byteArray.length; i++) { arrByte[i] = (byte) pis.read(); System.out.println((char) arrByte[i]); } } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { byte[] arrByte = new byte[1024]; byte[] byteArray = new byte[] { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' }; InputStream is = new ByteArrayInputStream(byteArray); PushbackInputStream pis = new PushbackInputStream(is, 10); try {//from w w w. ja va 2 s .com // read from the buffer one character at a time for (int i = 0; i < byteArray.length; i++) { // read a char into our array arrByte[i] = (byte) pis.read(); System.out.println((char) arrByte[i]); } // unread a char pis.unread('F'); // read again from the buffer arrByte[1] = (byte) pis.read(); System.out.println((char) arrByte[1]); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static final int peek(PushbackInputStream in) { try {//from w w w . j a va2 s . c o m int p = in.read(); in.unread(p); return p; } catch (IOException e) { return -1; } }
From source file:Main.java
public static BufferedReader readMap(InputStream in) throws IOException { PushbackInputStream pushback = new PushbackInputStream(in, 3); int first = pushback.read(); if (first == 0xEF) { int second = pushback.read(); if (second == 0xBB) { int third = pushback.read(); if (third == 0xBF) { return new BufferedReader(new InputStreamReader(pushback, "UTF-8")); }// ww w .j a v a 2 s . c o m pushback.unread(third); } pushback.unread(second); } pushback.unread(first); return new BufferedReader(new InputStreamReader(pushback, "ISO-8859-1")); }
From source file:Main.java
public static final int ReadInt(PushbackInputStream in, int def) { int b;/* w ww . j av a 2s .c om*/ boolean neg = false; int i = 0, digits = 0; try { while (true) { b = (int) in.read() & 0xff; if (b == '-' && digits == 0) neg = !neg; else if (Character.isDigit(b)) { i = 10 * i + (b - (int) '0'); ++digits; } else if (digits != 0 || !Character.isSpace((char) b)) { if (b != -1) in.unread(b); break; } } } catch (IOException e) { } try { while ((b = in.read()) != '/' && b != -1) ; } catch (IOException e) { } if (neg) i = -i; return digits > 0 ? i : def; }
From source file:com.ning.metrics.serialization.smile.SmileEnvelopeEventDeserializer.java
/** * Extracts all events in the stream/*from ww w . ja va 2 s .co m*/ * Note: Stream must be formatted as an array of (serialized) SmileEnvelopeEvents. * * @param in InputStream containing events * @return A list of SmileEnvelopeEvents * @throws IOException generic I/O exception */ public static List<SmileEnvelopeEvent> extractEvents(final InputStream in) throws IOException { final PushbackInputStream pbIn = new PushbackInputStream(in); final byte firstByte = (byte) pbIn.read(); // EOF? if (firstByte == -1) { return null; } pbIn.unread(firstByte); SmileEnvelopeEventDeserializer deser = (firstByte == SMILE_MARKER) ? new SmileEnvelopeEventDeserializer(pbIn, false) : new SmileEnvelopeEventDeserializer(pbIn, true); final List<SmileEnvelopeEvent> events = new LinkedList<SmileEnvelopeEvent>(); while (deser.hasNextEvent()) { SmileEnvelopeEvent event = deser.getNextEvent(); if (event == null) { // TODO: this is NOT expected, should warn break; } events.add(event); } return events; }
From source file:com.digitalpebble.behemoth.io.warc.HttpResponse.java
private static int readLine(PushbackInputStream in, StringBuilder line, boolean allowContinuedLine) throws IOException { line.setLength(0);/*from w w w . j ava2s.c o m*/ for (int c = in.read(); c != -1; c = in.read()) { switch (c) { case '\r': if (peek(in) == '\n') { in.read(); } case '\n': if (line.length() > 0) { // at EOL -- check for continued line if the current // (possibly continued) line wasn't blank if (allowContinuedLine) switch (peek(in)) { case ' ': case '\t': // line is continued in.read(); continue; } } return line.length(); // else complete default: line.append((char) c); } } throw new EOFException(); }
From source file:com.digitalpebble.storm.crawler.protocol.http.HttpResponse.java
private static int peek(PushbackInputStream in) throws IOException { int value = in.read(); in.unread(value);/*from w w w. ja va 2s .com*/ return value; }
From source file:com.digitalpebble.storm.crawler.protocol.http.HttpResponse.java
private static int readLine(PushbackInputStream in, StringBuffer line, boolean allowContinuedLine) throws IOException { line.setLength(0);/* w w w . ja v a 2s.c o m*/ for (int c = in.read(); c != -1; c = in.read()) { switch (c) { case '\r': if (peek(in) == '\n') { in.read(); } case '\n': if (line.length() > 0) { // at EOL -- check for continued line if the current // (possibly continued) line wasn't blank if (allowContinuedLine) switch (peek(in)) { case ' ': case '\t': // line is continued in.read(); continue; } } return line.length(); // else complete default: line.append((char) c); } } throw new EOFException(); }
From source file:grails.converters.JSON.java
/** * Parses the given request's InputStream and returns ether a JSONObject or a JSONArry * * @param request the JSON Request//from ww w . j a v a 2 s .com * @return ether a JSONObject or a JSONArray - depending on the given JSON * @throws ConverterException when the JSON content is not valid */ public static Object parse(HttpServletRequest request) throws ConverterException { Object json = request.getAttribute(CACHED_JSON); if (json != null) { return json; } String encoding = request.getCharacterEncoding(); if (encoding == null) { encoding = Converter.DEFAULT_REQUEST_ENCODING; } try { PushbackInputStream pushbackInputStream = null; int firstByte = -1; try { pushbackInputStream = new PushbackInputStream(request.getInputStream()); firstByte = pushbackInputStream.read(); } catch (IOException ioe) { } if (firstByte == -1) { return new JSONObject(); } pushbackInputStream.unread(firstByte); json = parse(pushbackInputStream, encoding); request.setAttribute(CACHED_JSON, json); return json; } catch (IOException e) { throw new ConverterException("Error parsing JSON", e); } }