Class GzipCompressorInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable,InputStreamStatistics
This supports decompressing concatenated GZIP files which is important when decompressing standalone GZIP files.
Instead of using java.util.zip.GZIPInputStream, this class has its own GZIP member decoder. Internally, decompression is done using
Inflater.
If you use the constructor GzipCompressorInputStream(in), Builder.setDecompressConcatenated(false), or
GzipCompressorInputStream(in, false), then read() will return -1 as soon as the first encoded GZIP member has been completely read. In this
case, if the underlying input stream supports mark() and reset(), 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 Builder.setDecompressConcatenated(true) or GzipCompressorInputStream(in, true) then read() 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 an
IOException is 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.
To build an instance, use GzipCompressorInputStream.Builder.
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]Buffer to hold the input data.private intAmount of data in buf.private final org.apache.commons.io.input.BoundedInputStreamprivate final CRC32CRC32 from uncompressed data.private final booleanTrue if decompressing multi-member streams.private booleanTrue once everything has been decompressed.private final Charsetprivate final InputStreamCompressed input stream, possibly wrapped in a BufferedInputStream, always wrapped in countingStream aboveprivate InflaterDecompressor.private static final org.apache.commons.io.function.IOConsumer<GzipCompressorInputStream> private final byte[]Buffer for no-argument read method.private final org.apache.commons.io.function.IOConsumer<GzipCompressorInputStream> private final org.apache.commons.io.function.IOConsumer<GzipCompressorInputStream> private GzipParameters -
Constructor Summary
ConstructorsModifierConstructorDescriptionGzipCompressorInputStream(InputStream inputStream) Constructs a new input stream that decompresses gzip-compressed data from the specified input stream.GzipCompressorInputStream(InputStream inputStream, boolean decompressConcatenated) Deprecated.private -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Constructs a new builder ofGzipCompressorInputStream.voidclose()Closes the input stream (unless it is System.in).longGets the amount of raw or compressed bytes read by the stream.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(DataInput inData) Methods inherited from class org.apache.commons.compress.compressors.CompressorInputStream
count, count, getBytesRead, getCount, getUncompressedCount, pushedBackBytesMethods inherited from class java.io.InputStream
available, mark, markSupported, read, reset, skipMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.commons.compress.utils.InputStreamStatistics
getUncompressedCount
-
Field Details
-
NOOP
-
buf
private final byte[] bufBuffer to hold the input data. -
bufUsed
private int bufUsedAmount of data in buf. -
countingStream
private final org.apache.commons.io.input.BoundedInputStream countingStream -
crc
CRC32 from uncompressed data. -
decompressConcatenated
private final boolean decompressConcatenatedTrue if decompressing multi-member streams. -
endReached
private boolean endReachedTrue once everything has been decompressed. -
fileNameCharset
-
in
Compressed input stream, possibly wrapped in a BufferedInputStream, always wrapped in countingStream above -
inflater
Decompressor. -
oneByte
private final byte[] oneByteBuffer for no-argument read method. -
parameters
-
onMemberStart
-
onMemberEnd
-
-
Constructor Details
-
GzipCompressorInputStream
- Throws:
IOException
-
GzipCompressorInputStream
Constructs 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:
IOException- if the stream could not be created
-
GzipCompressorInputStream
@Deprecated public GzipCompressorInputStream(InputStream inputStream, boolean decompressConcatenated) throws IOException Deprecated.Constructs 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:
IOException- if the stream could not be created
-
-
Method Details
-
builder
Constructs a new builder ofGzipCompressorInputStream.- Returns:
- a new builder of
GzipCompressorInputStream. - Since:
- 1.28.0
-
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
- Throws:
IOException
-
close
Closes the input stream (unless it is System.in).- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- Since:
- 1.2
-
getCompressedCount
public long getCompressedCount()Gets 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
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
- Throws:
IOException
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException- Since:
- 1.1
-
GzipCompressorInputStream.Builder.get().