Example usage for java.io PushbackInputStream PushbackInputStream

List of usage examples for java.io PushbackInputStream PushbackInputStream

Introduction

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

Prototype

public PushbackInputStream(InputStream in, int size) 

Source Link

Document

Creates a PushbackInputStream with a pushback buffer of the specified size, and saves its argument, the input stream in, for later use.

Usage

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;/* ww  w . j  av  a 2  s .  com*/
    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 {/*  ww w  . j  a  v a 2s .  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: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 ww .ja va  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, 2, 3);

        for (int i = 0; i < 3; 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 {/*  www . ja v a  2s  .co m*/
        // 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 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. ja v  a  2 s  . c  om*/
            pushback.unread(third);
        }
        pushback.unread(second);
    }
    pushback.unread(first);

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

From source file:Main.java

/**
 * Convenience method to get a PushbackInputStream so that we can read the BOM
 *
 * @param is a regular InputStream/*from   ww  w.j  av  a  2s  .co  m*/
 * @return a PushbackInputStream wrapping the passed one
 */
public static PushbackInputStream getPushbackInputStream(InputStream is) {
    return new PushbackInputStream(is, BOM_SIZE);
}

From source file:Main.java

/**
 * Creates a reader allowing to read the contents of specified text source.
 * <p>This method implements the detection of the encoding.
 * <p>Note that the detection of the encoding always works 
 * because it uses a fallback value./*ww w  .  j  a  v a  2s . com*/
 *
 * @param in the text source
 * @param encoding the detected encoding is copied there.
 * May be <code>null</code>.
 * @return a reader allowing to read the contents of the text source.
 * This reader will automatically skip the BOM if any.
 * @exception IOException if there is an I/O problem
 */
public static Reader createReader(InputStream in, String fallbackEncoding, String[] encoding)
        throws IOException {
    byte[] bytes = new byte[1024];
    int byteCount = -1;

    PushbackInputStream in2 = new PushbackInputStream(in, bytes.length);
    try {
        int count = in2.read(bytes, 0, bytes.length);
        if (count > 0) {
            in2.unread(bytes, 0, count);
        }
        byteCount = count;
    } catch (IOException ignored) {
    }

    String charset = null;

    if (byteCount > 0) {
        if (byteCount >= 2) {
            // Use BOM ---

            int b0 = (bytes[0] & 0xFF);
            int b1 = (bytes[1] & 0xFF);

            switch ((b0 << 8) | b1) {
            case 0xFEFF:
                charset = "UTF-16BE";
                // We don't want to read the BOM.
                in2.skip(2);
                break;
            case 0xFFFE:
                charset = "UTF-16LE";
                in2.skip(2);
                break;
            case 0xEFBB:
                if (byteCount >= 3 && (bytes[2] & 0xFF) == 0xBF) {
                    charset = "UTF-8";
                    in2.skip(3);
                }
                break;
            }
        }

        if (charset == null) {
            // Unsupported characters are replaced by U+FFFD.
            String text = new String(bytes, 0, byteCount, "US-ASCII");

            if (text.startsWith("<?xml")) {
                Pattern pattern = Pattern.compile("encoding\\s*=\\s*['\"]([^'\"]+)");
                Matcher matcher = pattern.matcher(text);
                if (matcher.find()) {
                    charset = matcher.group(1);
                } else {
                    charset = "UTF-8";
                }
            }
        }
    }

    if (charset == null) {
        charset = fallbackEncoding;
        if (charset == null) {
            charset = "UTF-8";
        }
    }

    if (encoding != null) {
        encoding[0] = charset;
    }
    return new InputStreamReader(in2, charset);
}

From source file:org.apache.pulsar.io.file.utils.GZipFiles.java

/**
 * Returns true if the given file is a gzip file.
 *//*from  www  .j a  v  a  2  s. c  o m*/
@SuppressWarnings("deprecation")
public static boolean isGzip(File f) {

    InputStream input = null;
    try {
        input = new FileInputStream(f);
        PushbackInputStream pb = new PushbackInputStream(input, 2);
        byte[] signature = new byte[2];
        int len = pb.read(signature); //read the signature
        pb.unread(signature, 0, len); //push back the signature to the stream
        // check if matches standard gzip magic number
        return (signature[0] == (byte) 0x1f && signature[1] == (byte) 0x8b);
    } catch (final Exception e) {
        return false;
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:org.jcodec.codecs.mjpeg.JpegParser.java

public CodedImage parse(InputStream is) throws IOException {
    CountingInputStream counter = new CountingInputStream(is);
    return parse(new PushbackInputStream(counter, 2), counter);
}

From source file:org.jcodec.codecs.mjpeg.MJPEGParser.java

@Test
@Ignore/*from  w w  w .  j av  a  2  s  .c  o  m*/
public void testPerformance() throws Exception {
    JpegDecoder dec = new JpegDecoder();
    JpegParser parser = new JpegParser();
    InputStream is = new BufferedInputStream(new FileInputStream("/Users/zhukov/Movies/MVI_0636.mjpg"));
    CountingInputStream counter = new CountingInputStream(is);
    PushbackInputStream push = new PushbackInputStream(counter, 2);
    CodedImage codedImage = parser.parse(is);
    DecodedImage decode = new DecodedImage(codedImage.getWidth(), codedImage.getHeight());
    dec.decode(codedImage, decode);
    long start = System.currentTimeMillis();
    for (int i = 1; i < 2; i++) {
        if (i % 10 == 0) {
            long time = System.currentTimeMillis() - start;
            long fps = i * 1000 / time;
            System.out.println(i + " " + fps + "fps");
        }
        codedImage = parser.parse(push, counter);
        dec.decode(codedImage, decode);
    }

}