Created
November 11, 2014 14:06
-
-
Save adohe-zz/b579bac16d337bae7c51 to your computer and use it in GitHub Desktop.
check gzip stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xqbase.java; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PushbackInputStream; | |
import java.util.zip.GZIPInputStream; | |
public class CheckGZIPStream { | |
public static void main(String[] args) throws Exception { | |
InputStream inputStream = new FileInputStream(""); | |
PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream, 2); | |
System.out.println(isInputStreamGZIPCompressed(pushbackInputStream)); | |
} | |
private 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment