Example usage for java.util.zip GZIPInputStream GZIP_MAGIC

List of usage examples for java.util.zip GZIPInputStream GZIP_MAGIC

Introduction

In this page you can find the example usage for java.util.zip GZIPInputStream GZIP_MAGIC.

Prototype

int GZIP_MAGIC

To view the source code for java.util.zip GZIPInputStream GZIP_MAGIC.

Click Source Link

Document

GZIP header magic number.

Usage

From source file:gobblin.util.io.StreamUtils.java

public static boolean isCompressed(byte[] bytes) {
    if ((bytes == null) || (bytes.length < 2)) {
        return false;
    } else {/* w w  w .j  av  a 2 s .  c  om*/
        return ((bytes[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
                && (bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)));
    }
}

From source file:org.xlcloud.commons.compress.CompressUtils.java

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

From source file:com.krawler.common.util.ByteUtil.java

/**
 * Determines if the data contained in the buffer is gzipped by matching the
 * first 2 bytes with GZIP magic GZIP_MAGIC (0x8b1f).
 * /* ww w  . j a  va2  s  . c  o m*/
 * @param data
 * @return
 */
public static boolean isGzipped(byte[] data) {
    return data != null && data.length > 2 && ((data[0] | (data[1] << 8)) == GZIPInputStream.GZIP_MAGIC);
}

From source file:org.ut.biolab.medsavant.shared.util.IOUtils.java

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

From source file:org.ut.biolab.medsavant.shared.util.IOUtils.java

/**
 * Checks if a file is gzipped./* ww w. j  a v  a 2s .com*/
 *
 * @param f
 * @return
 */
public static boolean isGZipped(File f) {
    int magic = 0;
    try {
        RandomAccessFile raf = new RandomAccessFile(f, "r");
        magic = raf.read() & 0xff | ((raf.read() << 8) & 0xff00);
        raf.close();
    } catch (Throwable e) {
        e.printStackTrace(System.err);
    }
    return magic == GZIPInputStream.GZIP_MAGIC;
}

From source file:com.zimbra.common.util.ByteUtil.java

/**
 * Determines if the data contained in the buffer is gzipped
 * by matching the first 2 bytes with GZIP magic GZIP_MAGIC (0x8b1f).
 * @param data//from  www.ja v a 2 s  . com
 * @return
 */
public static boolean isGzipped(byte[] data) {
    if (data == null || data.length < 2) {
        return false;
    }
    int byte1 = data[0];
    int byte2 = data[1] & 0xff; // Remove sign, since bytes are signed in Java.
    return (byte1 | (byte2 << 8)) == GZIPInputStream.GZIP_MAGIC;
}

From source file:com.zimbra.common.util.ByteUtil.java

/**
 * Determines if the data in the given stream is gzipped.
 * Requires that the <tt>InputStream</tt> supports mark/reset.
 *//*  w  ww.j a va 2s .c  o  m*/
public static boolean isGzipped(InputStream in) throws IOException {
    in.mark(2);
    int header = in.read() | (in.read() << 8);
    in.reset();
    if (header == GZIPInputStream.GZIP_MAGIC) {
        return true;
    }
    return false;
}

From source file:net.yacy.kelondro.util.FileUtils.java

/**
 * This function determines if a byte array is gzip compressed and uncompress it
 *
 * @param source properly gzip compressed byte array
 * @return uncompressed byte array/*  ww  w . java 2s .  c om*/
 * @throws IOException
 */
public static byte[] uncompressGZipArray(byte[] source) throws IOException {
    if (source == null) {
        return null;
    }

    // support of gzipped data (requested by roland)
    /* "Bitwise OR of signed byte value
     *
     * [...] Values loaded from a byte array are sign extended to 32 bits before
     * any any bitwise operations are performed on the value. Thus, if b[0]
     * contains the value 0xff, and x is initially 0, then the code ((x <<
     * 8) | b[0]) will sign extend 0xff to get 0xffffffff, and thus give the
     * value 0xffffffff as the result. [...]" findbugs description of BIT_IOR_OF_SIGNED_BYTE
     */
    if ((source.length > 1) && (((source[1] << 8) | (source[0] & 0xff)) == GZIPInputStream.GZIP_MAGIC)) {
        System.out.println("DEBUG: uncompressGZipArray - uncompressing source");
        try {
            final ByteArrayInputStream byteInput = new ByteArrayInputStream(source);
            final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(source.length / 5);
            final GZIPInputStream zippedContent = new GZIPInputStream(byteInput);
            final byte[] data = new byte[1024];
            int read = 0;

            // reading gzip file and store it uncompressed
            while ((read = zippedContent.read(data, 0, 1024)) != -1) {
                byteOutput.write(data, 0, read);
            }
            zippedContent.close();
            byteOutput.close();

            source = byteOutput.toByteArray();
        } catch (final Exception e) {
            if (!e.getMessage().equals("Not in GZIP format")) {
                throw new IOException(e.getMessage());
            }
        }
    }

    return source;
}

From source file:com.github.pascalgn.jiracli.web.HttpClient.java

private static InputStream maybeDecompress(InputStream input) throws IOException {
    // Due to a bug, Jira sometimes returns double-compressed responses. See JRA-37608
    BufferedInputStream buffered = new BufferedInputStream(input, 2);
    buffered.mark(2);/*from www.  j  av a  2  s  .  c  o  m*/
    int[] buf = new int[2];
    buf[0] = buffered.read();
    buf[1] = buffered.read();
    buffered.reset();
    int header = (buf[1] << 8) | buf[0];
    if (header == GZIPInputStream.GZIP_MAGIC) {
        return new GZIPInputStream(buffered);
    } else {
        return buffered;
    }
}

From source file:VCF.VCF.java

private static boolean isGZipped(File f) throws IOException {
    int magic;//from   w ww.  ja  v  a 2 s  .  c o  m
    RandomAccessFile raf = new RandomAccessFile(f, "r");
    magic = raf.read() & 0xff | ((raf.read() << 8) & 0xff00);
    raf.close();
    return magic == GZIPInputStream.GZIP_MAGIC;
}