Code example for PushbackInputStream

Methods: read, unread

0
     * @param is InputStream 
     * @return InputStream 
     * @throws IOException  
     */ 
    public static InputStream detectGzip(InputStream is) throws IOException {
        PushbackInputStream pb = new PushbackInputStream(is, 2);
        byte [] signature = new byte[2];
        pb.read(signature);
        pb.unread(signature);
        if(signature[0] == (byte) GZIPInputStream.GZIP_MAGIC && signature[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)) {
          return new GZIPInputStream(pb);
        }   else { 
          return pb;
        } 
    } 
}