java.util.zip.GZIPInputStream#GZIP_MAGIC源码实例Demo

下面列出了java.util.zip.GZIPInputStream#GZIP_MAGIC 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: querqy   文件: GZIPAwareResourceLoader.java
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;
}
 
源代码2 项目: gama   文件: SVGUniverse.java
/**
 * 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;
	}
}
 
源代码3 项目: varsim   文件: GzFileParser.java
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;
    }
}
 
源代码4 项目: petscii-bbs   文件: Utility.java
/**
 * @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);
	}
}
 
源代码5 项目: financisto   文件: DatabaseImport.java
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;
}
 
源代码6 项目: incubator-gobblin   文件: StreamUtils.java
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)));
  }
}
 
源代码7 项目: NBT   文件: NBTUtil.java
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;
}
 
源代码8 项目: DataFrame   文件: GZipUtil.java
/**
 * 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;
}
 
源代码9 项目: TomboloDigitalConnector   文件: ZipUtils.java
/**
 * 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;
}
 
源代码10 项目: helix   文件: GZipCompressionUtil.java
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)));
  }
}
 
源代码11 项目: Sparkplug   文件: GZipUtil.java
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)));
	}
}
 
源代码12 项目: android-project-wo2b   文件: AsyncHttpClient.java
/**
 * 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;
}
 
源代码13 项目: Android-Basics-Codes   文件: AsyncHttpClient.java
/**
 * 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;
}
 
源代码14 项目: Android-Basics-Codes   文件: AsyncHttpClient.java
/**
 * 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;
}
 
源代码15 项目: Angelia-core   文件: CompressionManager.java
private static boolean isGZipCompressed(final byte[] compressed) {
	return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
			&& (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
}
 
源代码16 项目: endpoints-java   文件: IoUtil.java
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;
}
 
源代码17 项目: mrgeo   文件: MbVectorTilesRecordReader.java
public static boolean isGZIPStream(byte[] bytes)
{
  return bytes[0] == (byte) GZIPInputStream.GZIP_MAGIC
          && bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >>> 8);
}
 
源代码18 项目: uavstack   文件: CompressHelper.java
/**
 * 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;
}
 
源代码19 项目: dyno   文件: ZipUtils.java
/**
 * 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);
}
 
源代码20 项目: webarchive-commons   文件: GzipHeader.java
/**
 * 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;
}