Java GZip InputStream Check isGZipped(InputStream in)

Here you can find the source of isGZipped(InputStream in)

Description

Checks if an input stream is gzipped.

License

Apache License

Parameter

Parameter Description
in a parameter

Declaration

public static boolean isGZipped(InputStream in) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.zip.GZIPInputStream;

public class Main {
    /**/*  ww w . j  a v a  2  s.  co  m*/
     * Checks if an input stream is gzipped.
     * 
     * @param in
     * @return
     */
    public static boolean isGZipped(InputStream in) {
        int magic = 0;
        try {
            if (!in.markSupported()) {
                in = new BufferedInputStream(in);
            }
            in.mark(2);
            try {
                magic = in.read() & 0xff | ((in.read() << 8) & 0xff00);
                in.reset();
            } catch (IOException e) {
                e.printStackTrace(System.err);
                return false;
            }
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace(System.err);
                return false;
            }
        }
        return magic == GZIPInputStream.GZIP_MAGIC;
    }
}

Related

  1. isGZip(BufferedInputStream instream)
  2. isGZIPCompressed(InputStream in)
  3. isGZipped(InputStream in)
  4. isGzipStm(InputStream in)