Java Code Examples for java.util.zip.GZIPInputStream#GZIP_MAGIC

The following examples show how to use java.util.zip.GZIPInputStream#GZIP_MAGIC . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GZIPAwareResourceLoader.java    From querqy with Apache License 2.0 6 votes vote down vote up
private static InputStream detectGZIPAndWrap(final InputStream is) throws IOException {
    final PushbackInputStream pb = new PushbackInputStream(is, 2);
    final byte[] signature = new byte[2];
    int count = 0;
    try {
        while (count < 2) {
            final int readCount = is.read(signature, count, 2 - count);
            if (readCount < 0) {
                return pb;
            }
            count = count + readCount;
        }
    } finally {
        pb.unread(signature, 0, count);
    }
    final int head = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00);
    return GZIPInputStream.GZIP_MAGIC == head ? new GZIPInputStream(pb) : pb;
}
 
Example 2
Source File: SVGUniverse.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Wraps input stream in a BufferedInputStream. If it is detected that this input stream is GZIPped, also wraps in a
 * GZIPInputStream for inflation.
 *
 * @param is
 *            Raw input stream
 * @return Uncompressed stream of SVG data
 * @throws java.io.IOException
 */
private InputStream createDocumentInputStream(final InputStream is) throws IOException {
	final BufferedInputStream bin = new BufferedInputStream(is);
	bin.mark(2);
	final int b0 = bin.read();
	final int b1 = bin.read();
	bin.reset();

	// Check for gzip magic number
	if ((b1 << 8 | b0) == GZIPInputStream.GZIP_MAGIC) {
		return new GZIPInputStream(bin);
	} else {
		// Plain text
		return bin;
	}
}
 
Example 3
Source File: GZipUtil.java    From DataFrame with MIT License 5 votes vote down vote up
/**
 * Returns <tt>true</tt> if specified {@link InputStream} is gzipped
 *
 * @param is Input stream to test
 * @return <tt>true</tt> if input stream is gzipped
 */
public static boolean isGzipped(InputStream is) {
    if (!is.markSupported()) {
        is = new BufferedInputStream(is);
    }
    is.mark(2);
    int m;
    try {
        m = is.read() & 0xff | ((is.read() << 8) & 0xff00);
        is.reset();
    } catch (IOException e) {
        return false;
    }
    return m == GZIPInputStream.GZIP_MAGIC;
}
 
Example 4
Source File: AsyncHttpClient.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Checks the InputStream if it contains  GZIP compressed data
 *
 * @param inputStream InputStream to be checked
 * @return true or false if the stream contains GZIP compressed data
 * @throws java.io.IOException
 */
public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException {
    if (inputStream == null)
        return false;

    byte[] signature = new byte[2];
    int readStatus = inputStream.read(signature);
    inputStream.unread(signature);
    int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00);
    return readStatus == 2 && GZIPInputStream.GZIP_MAGIC == streamHeader;
}
 
Example 5
Source File: AsyncHttpClient.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Checks the InputStream if it contains  GZIP compressed data
 *
 * @param inputStream InputStream to be checked
 * @return true or false if the stream contains GZIP compressed data
 * @throws java.io.IOException
 */
public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException {
    if (inputStream == null)
        return false;

    byte[] signature = new byte[2];
    int readStatus = inputStream.read(signature);
    inputStream.unread(signature);
    int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00);
    return readStatus == 2 && GZIPInputStream.GZIP_MAGIC == streamHeader;
}
 
Example 6
Source File: AsyncHttpClient.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the InputStream if it contains  GZIP compressed data
 *
 * @param inputStream InputStream to be checked
 * @return true or false if the stream contains GZIP compressed data
 * @throws java.io.IOException
 */
public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException {
    if (inputStream == null)
        return false;

    byte[] signature = new byte[2];
    int readStatus = inputStream.read(signature);
    inputStream.unread(signature);
    int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00);
    return readStatus == 2 && GZIPInputStream.GZIP_MAGIC == streamHeader;
}
 
Example 7
Source File: GZipUtil.java    From Sparkplug with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isCompressed(byte[] bytes) throws IOException {
	if ((bytes == null) || (bytes.length < 2)) {
		return false;
	} else {
		return ((bytes[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)));
	}
}
 
Example 8
Source File: GZipCompressionUtil.java    From helix with Apache License 2.0 5 votes vote down vote up
public static boolean isCompressed(byte[] bytes) {
  if ((bytes == null) || (bytes.length < 2)) {
    return false;
  } else {
    return ((bytes[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)));
  }
}
 
Example 9
Source File: ZipUtils.java    From TomboloDigitalConnector with MIT License 5 votes vote down vote up
/**
 * Checks if an input stream is gzipped.
 * Gzipped files have a magic number to recognize them.
 */
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) {
        log.warn("Failed to check if gzipped {}", e.getMessage());
        return false;
    }
    return magic == GZIPInputStream.GZIP_MAGIC;
}
 
Example 10
Source File: GzFileParser.java    From varsim with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected InputStream decompressStream(final File file) throws IOException {
    PushbackInputStream pb = new PushbackInputStream(new FileInputStream(file), 1024 * 1024); //we need a pushbackstream to look ahead
    byte[] signature = new byte[2];
    pb.read(signature); //read the signature
    pb.unread(signature); //push back the signature to the stream
    if (signature[0] == (byte) (GZIPInputStream.GZIP_MAGIC & 0xff) && signature[1] == (byte) ((GZIPInputStream.GZIP_MAGIC >> 8) & 0xff)) {
        return new GZIPInputStream(pb, 1024 * 1024);
    } else {
        return pb;
    }
}
 
Example 11
Source File: NBTUtil.java    From NBT with MIT License 5 votes vote down vote up
private static InputStream detectDecompression(InputStream is) throws IOException {
	PushbackInputStream pbis = new PushbackInputStream(is, 2);
	int signature = (pbis.read() & 0xFF) + (pbis.read() << 8);
	pbis.unread(signature >> 8);
	pbis.unread(signature & 0xFF);
	if (signature == GZIPInputStream.GZIP_MAGIC) {
		return new GZIPInputStream(pbis);
	}
	return pbis;
}
 
Example 12
Source File: StreamUtils.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
public static boolean isCompressed(byte[] bytes) {
  if ((bytes == null) || (bytes.length < 2)) {
    return false;
  } else {
    return ((bytes[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) &&
        (bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)));
  }
}
 
Example 13
Source File: DatabaseImport.java    From financisto with GNU General Public License v2.0 5 votes vote down vote up
private InputStream decompressStream(InputStream input) throws IOException {
    PushbackInputStream pb = new PushbackInputStream(input, 2);
    byte[] bytes = new byte[2];
    pb.read(bytes);
    pb.unread(bytes);
    int head = ((int) bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
    if (GZIPInputStream.GZIP_MAGIC == head)
        return new GZIPInputStream(pb);
    else
        return pb;
}
 
Example 14
Source File: Utility.java    From petscii-bbs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @param file
 *            file to check
 * @return true if file seems to be gzipped.
 * @throws CbmException
 *             if error
 */
public static boolean isGZipped(String file) throws CbmException {
	try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
		return GZIPInputStream.GZIP_MAGIC == (raf.read() & 0xff | ((raf.read() << 8) & 0xff00));
	} catch (IOException e) {
		throw new CbmException("Failed to open zip header. " + e.getMessage(), e);
	}
}
 
Example 15
Source File: IoUtil.java    From endpoints-java with Apache License 2.0 4 votes vote down vote up
private static boolean isGzipHeader(byte[] header) {
  // GZIP_MAGIC represents the 16-bit header that identify all gzipped content, as defined in
  // section 2.3.1 of https://tools.ietf.org/html/rfc1952.
  return header.length >= 2
      && (header[0] | ((header[1] & 0xff) << 8)) == GZIPInputStream.GZIP_MAGIC;
}
 
Example 16
Source File: CompressionManager.java    From Angelia-core with GNU General Public License v3.0 4 votes vote down vote up
private static boolean isGZipCompressed(final byte[] compressed) {
	return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
			&& (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
}
 
Example 17
Source File: MbVectorTilesRecordReader.java    From mrgeo with Apache License 2.0 4 votes vote down vote up
public static boolean isGZIPStream(byte[] bytes)
{
  return bytes[0] == (byte) GZIPInputStream.GZIP_MAGIC
          && bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >>> 8);
}
 
Example 18
Source File: CompressHelper.java    From uavstack with Apache License 2.0 3 votes vote down vote up
/**
 * Check if a byte array is compressed by GZIP
 * 
 * @param byteArr
 *            a byte array
 * @return true stands for compressed data; false stands for uncompressed data
 * @throws IOException
 */
public static boolean isCompressedByGZIP(byte[] byteArr) throws IOException {

    ByteArrayInputStream bis = new ByteArrayInputStream(byteArr);
    int b = readUByte(bis);
    int num = readUByte(bis) << 8 | b;
    return num == GZIPInputStream.GZIP_MAGIC;
}
 
Example 19
Source File: GzipHeader.java    From webarchive-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Test gzip magic is next in the stream.
 * Reads two bytes.  Caller needs to manage resetting stream.
 * @param in InputStream to read.
 * @param crc CRC to update.
 * @return true if found gzip magic.  False otherwise
 * or an IOException (including EOFException).
 * @throws IOException
 */
public boolean testGzipMagic(InputStream in, CRC32 crc)
        throws IOException {
    return readShort(in, crc) == GZIPInputStream.GZIP_MAGIC;
}
 
Example 20
Source File: ZipUtils.java    From dyno with Apache License 2.0 2 votes vote down vote up
/**
 * Determines if a byte array is compressed. The java.util.zip GZip
 * implementation does not expose the GZip header so it is difficult to determine
 * if a string is compressed.
 *
 * @param bytes an array of bytes
 * @return true if the array is compressed or false otherwise
 * @throws java.io.IOException if the byte array couldn't be read
 */
public static boolean isCompressed(byte[] bytes) throws IOException {
    return bytes != null && bytes.length >= 2 &&
            bytes[0] == (byte) (GZIPInputStream.GZIP_MAGIC) && bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8);
}