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:org.mule.util.Base64.java

/**
 * Decodes data from Base64 notation, automatically detecting gzip-compressed
 * data and decompressing it.//from ww  w. ja v a 2 s . c  o m
 * 
 * @param s the string to decode
 * @return the decoded data
 * @since 1.4
 */
public static byte[] decode(String s) {
    byte[] bytes;
    try {
        bytes = s.getBytes(PREFERRED_ENCODING);
    } // end try
    catch (UnsupportedEncodingException uee) {
        bytes = s.getBytes();
    } // end catch
      // </change>

    // Decode
    bytes = decode(bytes, 0, bytes.length);

    // Check to see if it's gzip-compressed
    // GZIP Magic Two-Byte Number: 0x8b1f (35615)
    if (bytes != null && bytes.length >= 4) {

        int head = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
        if (GZIPInputStream.GZIP_MAGIC == head) {
            ByteArrayInputStream bais = null;
            GZIPInputStream gzis = null;
            ByteArrayOutputStream baos = null;
            byte[] buffer = new byte[4096];
            int length = 0;

            try {
                baos = new ByteArrayOutputStream(4096);
                bais = new ByteArrayInputStream(bytes);
                gzis = new GZIPInputStream(bais);

                while ((length = gzis.read(buffer)) >= 0) {
                    baos.write(buffer, 0, length);
                } // end while: reading input

                // No error? Get new bytes.
                bytes = baos.toByteArray();

            } // end try
            catch (IOException e) {
                // Just return originally-decoded bytes
            } // end catch
            finally {
                IOUtils.closeQuietly(baos);
                IOUtils.closeQuietly(gzis);
                IOUtils.closeQuietly(bais);
            } // end finally

        } // end if: gzipped
    } // end if: bytes.length >= 2

    return bytes;
}

From source file:agilejson.Base64.java

/**
 * Decodes data from Base64 notation, automatically detecting gzip-compressed
 * data and decompressing it.//w  ww . ja va  2s.  com
 * 
 * @param s the string to decode
 * @param options
 * @see Base64#URL_SAFE
 * @see Base64#ORDERED
 * @return the decoded data
 * @since 1.4
 */
public static byte[] decode(String s, int options) {
    byte[] bytes = null;
    try {
        bytes = s.getBytes(PREFERRED_ENCODING);

    } catch (UnsupportedEncodingException uee) {
        bytes = s.getBytes();
    } // end catch

    // Decode

    bytes = decode(bytes, 0, bytes.length, options);

    // Check to see if it's gzip-compressed
    // GZIP Magic Two-Byte Number: 0x8b1f (35615)

    if (bytes != null && bytes.length >= 4) {
        int head = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
        if (GZIPInputStream.GZIP_MAGIC == head) {
            GZIPInputStream gzis = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                gzis = new GZIPInputStream(new ByteArrayInputStream(bytes));

                byte[] buffer = new byte[2048];
                for (int length = 0; (length = gzis.read(buffer)) >= 0;) {
                    baos.write(buffer, 0, length);
                } // end while: reading input

                // No error? Get new bytes.
                bytes = baos.toByteArray();

            } catch (IOException e) {
                // Just return originally-decoded bytes

            } finally {
                try {
                    baos.close();
                } catch (Exception e) {
                    LOG.error("error closing ByteArrayOutputStream", e);
                }
                if (gzis != null) {
                    try {
                        gzis.close();
                    } catch (Exception e) {
                        LOG.error("error closing GZIPInputStream", e);
                    }
                }
            } // end finally
        } // end if: gzipped
    } // end if: bytes.length >= 2

    return bytes;
}

From source file:org.apache.hadoop.hbase.util.Base64.java

/**
 * Decodes data from Base64 notation, automatically detecting gzip-compressed
 * data and decompressing it.//from w  w  w  . j a v  a2  s  .  c o  m
 *
 * @param s the string to decode
 * @param options options for decode
 * @see Base64#URL_SAFE
 * @see Base64#ORDERED
 * @return the decoded data
 * @since 1.4
 */
public static byte[] decode(String s, int options) {
    byte[] bytes;
    try {
        bytes = s.getBytes(PREFERRED_ENCODING);

    } catch (UnsupportedEncodingException uee) {
        bytes = s.getBytes();
    } // end catch

    // Decode

    bytes = decode(bytes, 0, bytes.length, options);

    // Check to see if it's gzip-compressed
    // GZIP Magic Two-Byte Number: 0x8b1f (35615)

    if (bytes != null && bytes.length >= 4) {
        int head = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
        if (GZIPInputStream.GZIP_MAGIC == head) {
            GZIPInputStream gzis = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                gzis = new GZIPInputStream(new ByteArrayInputStream(bytes));

                byte[] buffer = new byte[2048];
                for (int length; (length = gzis.read(buffer)) >= 0;) {
                    baos.write(buffer, 0, length);
                } // end while: reading input

                // No error? Get new bytes.
                bytes = baos.toByteArray();

            } catch (IOException e) {
                // Just return originally-decoded bytes

            } finally {
                try {
                    baos.close();
                } catch (Exception e) {
                    LOG.error("error closing ByteArrayOutputStream", e);
                }
                if (gzis != null) {
                    try {
                        gzis.close();
                    } catch (Exception e) {
                        LOG.error("error closing GZIPInputStream", e);
                    }
                }
            } // end finally
        } // end if: gzipped
    } // end if: bytes.length >= 2

    return bytes;
}

From source file:es.mityc.firmaJava.libreria.utilidades.Base64.java

/**
 * Decodes data from Base64 notation, automatically
 * detecting gzip-compressed data and decompressing it.
 *
 * @param s the string to decode/*from w w w .  j av a  2s .  c om*/
* @param options encode options such as URL_SAFE
 * @return the decoded data
 * @since 1.4
 */
public static byte[] decode(String s, int options) {
    byte[] bytes;
    try {
        bytes = s.getBytes(PREFERRED_ENCODING);
    } // end try
    catch (UnsupportedEncodingException uee) {
        bytes = s.getBytes();
    } // end catch
    //</change>

    // Decode
    bytes = decode(bytes, 0, bytes.length, options);

    // Check to see if it's gzip-compressed
    // GZIP Magic Two-Byte Number: 0x8b1f (35615)
    int l = bytes.length;
    if (bytes != null && l >= 4) {

        int head = ((int) bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
        if (GZIPInputStream.GZIP_MAGIC == head) {
            ByteArrayInputStream bais = null;
            GZIPInputStream gzis = null;
            ByteArrayOutputStream baos = null;
            byte[] buffer = new byte[2048];
            int length = 0;

            try {
                baos = new ByteArrayOutputStream();
                bais = new ByteArrayInputStream(bytes);
                gzis = new GZIPInputStream(bais);

                int longitud = gzis.read(buffer);
                while ((length = longitud) >= 0) {
                    baos.write(buffer, 0, length);
                    longitud = gzis.read(buffer);
                } // end while: reading input

                // No error? Get new bytes.
                bytes = baos.toByteArray();

            } // end try
            catch (IOException e) {
                // Just return originally-decoded bytes
            } // end catch
            finally {
                try {
                    baos.close();
                } catch (Exception e) {
                    log.error(e);
                }
                try {
                    gzis.close();
                } catch (Exception e) {
                    log.error(e);
                }
                try {
                    bais.close();
                } catch (Exception e) {
                    log.error(e);
                }
            } // end finally

        } // end if: gzipped
    } // end if: bytes.length >= 2

    return bytes;
}

From source file:org.physical_web.physicalweb.Utils.java

/**
 * Determines if a file is gzipped by examining the signature of
 * the file, which is the first two bytes.
 * @param file to be determined if is gzipped.
 * @return true If the contents of the file are gzipped otherwise false.
 *///from w w w .ja  v a 2 s.c  om
public static boolean isGzippedFile(File file) {
    InputStream input;

    try {
        input = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        return false;
    }

    byte[] signature = new byte[GZIP_SIGNATURE_LENGTH];

    try {
        input.read(signature);
    } catch (IOException e) {
        return false;
    }

    return ((signature[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
            && (signature[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)));
}