Example usage for java.io PushbackInputStream unread

List of usage examples for java.io PushbackInputStream unread

Introduction

In this page you can find the example usage for java.io PushbackInputStream unread.

Prototype

public void unread(byte[] b) throws IOException 

Source Link

Document

Pushes back an array of bytes by copying it to the front of the pushback buffer.

Usage

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 www  .  j a v  a 2 s.  c o m*/
        for (int i = 0; i < byteArray.length; i++) {
            arrByte[i] = (byte) pis.read();
            System.out.println((char) arrByte[i]);
        }
        byte[] b = { 'W', 'o', 'r', 'l', 'd' };
        pis.unread(b);
        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:PushbackInputStreamDemo.java

public static void main(String args[]) throws IOException {
    String s = "if (a == 4) a = 0;\n";
    byte buf[] = s.getBytes();
    ByteArrayInputStream in = new ByteArrayInputStream(buf);
    PushbackInputStream f = new PushbackInputStream(in);
    int c;/*ww w  .j a v a  2 s.  com*/

    while ((c = f.read()) != -1) {
        switch (c) {
        case '=':
            if ((c = f.read()) == '=')
                System.out.print(".eq.");
            else {
                System.out.print("<-");
                f.unread(c);
            }
            break;
        default:
            System.out.print((char) c);
            break;
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    byte buf[] = "==  = ".getBytes();
    PushbackInputStream f = new PushbackInputStream(new ByteArrayInputStream(buf));
    int c;//from   ww  w . jav a2 s .c  o m
    while ((c = f.read()) != -1) {
        switch (c) {
        case '=':
            c = f.read();
            if (c == '=')
                System.out.print(".eq.");
            else {
                System.out.print("=");
                f.unread(c);
            }
            break;
        default:
            System.out.print((char) c);
            break;
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    byte buf[] = "==  = ".getBytes();
    PushbackInputStream f = new PushbackInputStream(new ByteArrayInputStream(buf), 100);
    int c;/*  www  .ja v a 2 s  .c o  m*/
    while ((c = f.read()) != -1) {
        switch (c) {
        case '=':
            c = f.read();
            if (c == '=')
                System.out.print(".eq.");
            else {
                System.out.print("=");
                f.unread(c);
            }
            break;
        default:
            System.out.print((char) c);
            break;
        }
    }
}

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 .  j  a  v  a  2s.c  om*/
        // 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  www.j a  v  a 2  s.c o  m*/
        int p = in.read();
        in.unread(p);
        return p;
    } catch (IOException e) {
        return -1;
    }
}

From source file:Main.java

/**
 * Checks the InputStream if it contains  GZIP compressed data
 *
 * @param inputStream InputStream to be checked
 * @return true or false if the stream contains GZIP compressed data
 * @throws java.io.IOException//from w w  w.j  av  a 2 s .co  m
 */
public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException {
    if (inputStream == null)
        return false;

    byte[] signature = new byte[2];
    int readStatus = inputStream.read(signature);
    inputStream.unread(signature);
    int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00);
    return readStatus == 2 && GZIPInputStream.GZIP_MAGIC == streamHeader;
}

From source file:eu.europeana.uim.sugarcrmclient.internal.ExtendedSaajSoapMessageFactory.java

/**
 * Detects if the incoming stream is Gzip encoded
 * /*from ww w .  ja  v  a 2  s.  c o  m*/
 * @param pb
 * @return an InputStream/GZIPInputStream 
 * @throws IOException
 */
public static InputStream decompressStream(PushbackInputStream pb) throws IOException {
    byte[] signature = new byte[2];
    pb.read(signature);
    pb.unread(signature);

    if (signature[0] == 31 && signature[1] == -117)
        return new GZIPInputStream(pb);
    else
        return pb;
}

From source file:com.ning.metrics.serialization.smile.SmileEnvelopeEventDeserializer.java

/**
 * Extracts all events in the stream//w  ww .j ava 2s. 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: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"));
            }/*from   w ww  .j  a v a  2s .  com*/
            pushback.unread(third);
        }
        pushback.unread(second);
    }
    pushback.unread(first);

    return new BufferedReader(new InputStreamReader(pushback, "ISO-8859-1"));
}