本文整理汇总了Java中java.util.zip.GZIPInputStream.GZIP_MAGIC属性的典型用法代码示例。如果您正苦于以下问题:Java GZIPInputStream.GZIP_MAGIC属性的具体用法?Java GZIPInputStream.GZIP_MAGIC怎么用?Java GZIPInputStream.GZIP_MAGIC使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类java.util.zip.GZIPInputStream
的用法示例。
在下文中一共展示了GZIPInputStream.GZIP_MAGIC属性的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isInputStreamGZIPCompressed
/**
* 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 if read from inputStream fails
*/
public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException {
if (inputStream == null)
return false;
byte[] signature = new byte[2];
int count = 0;
try {
while (count < 2) {
int readCount = inputStream.read(signature, count, 2 - count);
if (readCount < 0) return false;
count = count + readCount;
}
} finally {
inputStream.unread(signature, 0, count);
}
int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00);
return GZIPInputStream.GZIP_MAGIC == streamHeader;
}
示例2: isGZipped
public static boolean isGZipped(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)));
}
}
示例3: maybeDecompress
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);
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;
}
}
示例4: isCompressed
/**
* Determines if a byte array is compressed. The java.util.zip GZip
* implementaiton does not expose the GZip header so it is difficult to
* determine if a string is compressed.
*
* @return true if the array is compressed or false otherwise
*/
public boolean isCompressed() {
if ((bytes == null) || (bytes.length < 2)) {
return false;
} else {
return ((bytes[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
&& (bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)));
}
}
示例5: unGzipBytesToString
public static String unGzipBytesToString(InputStream in) {
try {
PushbackInputStream pis = new PushbackInputStream(in, 2);
byte[] signature = new byte[2];
pis.read(signature);
pis.unread(signature);
int head = ((signature[0] & 0x00FF) | ((signature[1] << 8) & 0xFF00));
if (head != GZIPInputStream.GZIP_MAGIC) {
return new String(toByteArray(pis), "UTF-8").trim();
}
GZIPInputStream gzip = new GZIPInputStream(pis);
byte[] readBuf = new byte[8 * 1024];
ByteArrayOutputStream outputByte = new ByteArrayOutputStream();
int readCount = 0;
do {
readCount = gzip.read(readBuf);
if (readCount > 0) {
outputByte.write(readBuf, 0, readCount);
}
} while (readCount > 0);
closeQuietly(gzip);
closeQuietly(pis);
closeQuietly(in);
if (outputByte.size() > 0) {
return new String(outputByte.toByteArray());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例6: isInputStreamGZIPCompressed
/**
* 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;
}
示例7: isInputStreamGZIPCompressed
/**
* 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 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;
}
示例8: decompressStream
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;
}
示例9: isCompressed
public static boolean isCompressed (final byte[] compressed) {
return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
}
示例10: isCompressedByGZIP
/**
* 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;
}
示例11: isCompressed
/**
* Checks if is compressed.
*
* @param compressed the compressed
* @return true, if is compressed
*/
public static boolean isCompressed(final byte[] compressed) {
return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
&& (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
}
示例12: isGzipStream
/**
* Tests for a gzip byte array.
*
* <p>Source: https://www.javacodegeeks.com/2015/01/working-with-gzip-and-compressed-data.html
*
* @param bytes the bytes to test
* @return true if a gzip byte array
*/
public static boolean isGzipStream(byte[] bytes) {
return bytes[0] == (byte) GZIPInputStream.GZIP_MAGIC
&& bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >>> 8);
}