下面列出了java.util.zip.GZIPInputStream#GZIP_MAGIC 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
/**
* 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;
}
}
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;
}
}
/**
* @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);
}
}
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;
}
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)));
}
}
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;
}
/**
* 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;
}
/**
* 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;
}
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)));
}
}
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)));
}
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
private static boolean isGZipCompressed(final byte[] compressed) {
return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
&& (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
}
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;
}
public static boolean isGZIPStream(byte[] bytes)
{
return bytes[0] == (byte) GZIPInputStream.GZIP_MAGIC
&& bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >>> 8);
}
/**
* 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;
}
/**
* 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);
}
/**
* 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;
}