Class GzipCompressorInputStream
- java.lang.Object
-
- java.io.InputStream
-
- kala.compress.compressors.CompressorInputStream
-
- kala.compress.compressors.gzip.GzipCompressorInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,InputStreamStatistics
public class GzipCompressorInputStream extends CompressorInputStream implements InputStreamStatistics
Input stream that decompresses .gz files.This supports decompressing concatenated .gz files which is important when decompressing standalone .gz files.
Instead of using
java.util.zip.GZIPInputStream, this class has its own GZIP member decoder. The actual decompression is done withInflater.If you use the constructor
GzipCompressorInputStream(in)orGzipCompressorInputStream(in, false), thenread()will return -1 as soon as the first encoded GZIP member has been completely read. In this case, if the underlying input stream supportsmark()andreset(), then it will be left positioned just after the end of the encoded GZIP member; otherwise, some indeterminate number of extra bytes following the encoded GZIP member will have been consumed and discarded.If you use the constructor
GzipCompressorInputStream(in, true)thenread()will return -1 only after the entire input stream has been exhausted; any bytes that follow an encoded GZIP member must constitute a new encoded GZIP member, otherwise anIOExceptionis thrown. The data read from a stream constructed this way will consist of the concatenated data of all of the encoded GZIP members in order.- See Also:
- RFC 1952 GZIP File Format Specification
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]bufprivate intbufUsedprivate CountingInputStreamcountingStreamprivate java.util.zip.CRC32crcprivate booleandecompressConcatenatedprivate booleanendReachedprivate static intFCOMMENTprivate static intFEXTRAprivate static intFHCRCprivate static intFNAMEprivate static intFRESERVEDprivate java.io.InputStreaminprivate java.util.zip.Inflaterinfprivate byte[]oneByteprivate GzipParametersparameters
-
Constructor Summary
Constructors Constructor Description GzipCompressorInputStream(java.io.InputStream inputStream)Constructs a new input stream that decompresses gzip-compressed data from the specified input stream.GzipCompressorInputStream(java.io.InputStream inputStream, boolean decompressConcatenated)Constructs a new input stream that decompresses gzip-compressed data from the specified input stream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the input stream (unless it is System.in).longgetCompressedCount()Gets the amount of raw or compressed bytes read by the stream.GzipParametersgetMetaData()Provides the stream's meta data - may change with each stream when decompressing concatenated streams.private booleaninit(boolean isFirstMember)static booleanmatches(byte[] signature, int length)Checks if the signature matches what is expected for a .gz file.intread()intread(byte[] b, int off, int len)private static byte[]readToNull(java.io.DataInput inData)-
Methods inherited from class kala.compress.compressors.CompressorInputStream
count, count, getBytesRead, getUncompressedCount, pushedBackBytes
-
Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface kala.compress.utils.InputStreamStatistics
getUncompressedCount
-
-
-
-
Field Detail
-
FHCRC
private static final int FHCRC
- See Also:
- Constant Field Values
-
FEXTRA
private static final int FEXTRA
- See Also:
- Constant Field Values
-
FNAME
private static final int FNAME
- See Also:
- Constant Field Values
-
FCOMMENT
private static final int FCOMMENT
- See Also:
- Constant Field Values
-
FRESERVED
private static final int FRESERVED
- See Also:
- Constant Field Values
-
countingStream
private final CountingInputStream countingStream
-
in
private final java.io.InputStream in
-
decompressConcatenated
private final boolean decompressConcatenated
-
buf
private final byte[] buf
-
bufUsed
private int bufUsed
-
inf
private java.util.zip.Inflater inf
-
crc
private final java.util.zip.CRC32 crc
-
endReached
private boolean endReached
-
oneByte
private final byte[] oneByte
-
parameters
private final GzipParameters parameters
-
-
Constructor Detail
-
GzipCompressorInputStream
public GzipCompressorInputStream(java.io.InputStream inputStream) throws java.io.IOExceptionConstructs a new input stream that decompresses gzip-compressed data from the specified input stream.This is equivalent to
GzipCompressorInputStream(inputStream, false)and thus will not decompress concatenated .gz files.- Parameters:
inputStream- the InputStream from which this object should be created of- Throws:
java.io.IOException- if the stream could not be created
-
GzipCompressorInputStream
public GzipCompressorInputStream(java.io.InputStream inputStream, boolean decompressConcatenated) throws java.io.IOExceptionConstructs a new input stream that decompresses gzip-compressed data from the specified input stream.If
decompressConcatenatedisfalse: This decompressor might read more input than it will actually use. IfinputStreamsupportsmarkandreset, then the input position will be adjusted so that it is right after the last byte of the compressed stream. Ifmarkisn't supported, the input position will be undefined.- Parameters:
inputStream- the InputStream from which this object should be created ofdecompressConcatenated- if true, decompress until the end of the input; if false, stop after the first .gz member- Throws:
java.io.IOException- if the stream could not be created
-
-
Method Detail
-
matches
public static boolean matches(byte[] signature, int length)Checks if the signature matches what is expected for a .gz file.- Parameters:
signature- the bytes to checklength- the number of bytes to check- Returns:
- true if this is a .gz stream, false otherwise
- Since:
- 1.1
-
readToNull
private static byte[] readToNull(java.io.DataInput inData) throws java.io.IOException- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionCloses the input stream (unless it is System.in).- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException- Since:
- 1.2
-
getCompressedCount
public long getCompressedCount()
Description copied from interface:InputStreamStatisticsGets the amount of raw or compressed bytes read by the stream.- Specified by:
getCompressedCountin interfaceInputStreamStatistics- Returns:
- the amount of raw or compressed bytes read by the stream.
- Since:
- 1.17
-
getMetaData
public GzipParameters getMetaData()
Provides the stream's meta data - may change with each stream when decompressing concatenated streams.- Returns:
- the stream's meta data
- Since:
- 1.8
-
init
private boolean init(boolean isFirstMember) throws java.io.IOException- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException- Since:
- 1.1
-
-