Java GZip InputStream Check isGZIPCompressed(InputStream in)

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

Description

Indicates whether an InputStream is GZIP compressed.

License

Apache License

Parameter

Parameter Description
in the InputStream (must return true on markSupported())

Exception

Parameter Description
IOException in case of an I/O error

Return

true if the InputStream is GZIP compressed

Declaration

public static boolean isGZIPCompressed(InputStream in)
        throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w  ww .j a  v  a2  s  . c  o m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    /**
     * GZIP header magic number bytes, like found in a gzipped
     * files, which are encoded in Intel format (i..e. little indian).
     */
    private static final byte[] GZIP_MAGIC = { (byte) 0x1f, (byte) 0x8b };

    /**
     * Indicates whether an InputStream is GZIP compressed. The InputStream must support
     * mark()/reset().
     * @param in the InputStream (must return true on markSupported())
     * @return true if the InputStream is GZIP compressed
     * @throws IOException in case of an I/O error
     */
    public static boolean isGZIPCompressed(InputStream in)
            throws IOException {
        if (!in.markSupported()) {
            throw new IllegalArgumentException(
                    "InputStream must support mark()!");
        }
        byte[] data = new byte[2];
        in.mark(2);
        in.read(data);
        in.reset();
        return ((data[0] == GZIP_MAGIC[0]) && (data[1] == GZIP_MAGIC[1]));
    }
}

Related

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