Top Description Fields Constructors Methods
org.apache.commons.compress.compressors.gzip

public Class GzipCompressorInputStream

extends CompressorInputStream
implements InputStreamStatistics
Class Inheritance
All Implemented Interfaces
org.apache.commons.compress.utils.InputStreamStatistics
Imports
java.io.BufferedInputStream, .ByteArrayOutputStream, .DataInput, .DataInputStream, .EOFException, .IOException, .InputStream, java.util.zip.CRC32, .DataFormatException, .Deflater, .Inflater, org.apache.commons.compress.compressors.CompressorInputStream, org.apache.commons.compress.utils.ByteUtils, .InputStreamStatistics, org.apache.commons.io.input.CountingInputStream

Input stream that decompresses .gz files.

This supports decompressing concatenated .gz files which is important when decompressing standalone .gz files.

java.util.zip.GZIPInputStream doesn't decompress concatenated .gz files: it stops after the first member and silently ignores the rest. It doesn't leave the read position to point to the beginning of the next member, which makes it difficult workaround the lack of concatenation support.

Instead of using GZIPInputStream, this class has its own .gz container format decoder. The actual decompression is done with java.util.zip.Inflater.

If you use the constructor GzipCompressorInputStream(in) or GzipCompressorInputStream(in, false) with some InputStream in then read will return -1 as soon as the first internal member has been read completely. The stream in will be positioned at the start of the second gzip member if there is one.

If you use the constructor GzipCompressorInputStream(in, true) with some InputStream in then read will return -1 once the stream in has been exhausted. The data read from a stream constructed this way will consist of the concatenated data of all gzip members contained inside in.

See Also
https://tools.ietf.org/html/rfc1952

Field Summary

Modifier and TypeField and Description
private final byte[]
private int
private final CountingInputStream
private final CRC32
private final boolean
private boolean
private static final int
private static final int
private static final int
private static final int
private static final int
private final InputStream
private Inflater
private final byte[]
private final GzipParameters

Constructor Summary

AccessConstructor and Description
public
GzipCompressorInputStream(final InputStream
the InputStream from which this object should be created of
inputStream
)

Constructs a new input stream that decompresses gzip-compressed data from the specified input stream.

public
GzipCompressorInputStream(final InputStream
the InputStream from which this object should be created of
inputStream
,
final boolean
if true, decompress until the end of the input; if false, stop after the first .gz member
decompressConcatenated
)

Constructs a new input stream that decompresses gzip-compressed data from the specified input stream.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes the input stream (unless it is System.in).
public long
getCompressedCount()

Implements org.apache.commons.compress.utils.InputStreamStatistics.getCompressedCount.

Gets the amount of raw or compressed bytes read by the stream.
public GzipParameters

Returns:

the stream's meta data
getMetaData
()

Provides the stream's meta data - may change with each stream when decompressing concatenated streams.

private boolean
init(final boolean isFirstMember)

public static boolean

Returns:

true if this is a .gz stream, false otherwise
matches
(final byte[]
the bytes to check
signature
,
final int
the number of bytes to check
length
)

Checks if the signature matches what is expected for a .gz file.

public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the input stream.
public int
read(final byte[]
the buffer into which the data is read.
b
,
int
the start offset in array b at which the data is written.
off
,
int
the maximum number of bytes to read.
len
)

Overrides java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes.
private static byte[]
readToNull(final DataInput inData)

Inherited from org.apache.commons.compress.compressors.CompressorInputStream:
countcountgetBytesReadgetCountgetUncompressedCountpushedBackBytes

Field Detail

bufback to summary
private final byte[] buf
bufUsedback to summary
private int bufUsed
countingStreamback to summary
private final CountingInputStream countingStream
crcback to summary
private final CRC32 crc
decompressConcatenatedback to summary
private final boolean decompressConcatenated
endReachedback to summary
private boolean endReached
FCOMMENTback to summary
private static final int FCOMMENT
FEXTRAback to summary
private static final int FEXTRA
FHCRCback to summary
private static final int FHCRC
FNAMEback to summary
private static final int FNAME
FRESERVEDback to summary
private static final int FRESERVED
inback to summary
private final InputStream in
infback to summary
private Inflater inf
oneByteback to summary
private final byte[] oneByte
parametersback to summary
private final GzipParameters parameters

Constructor Detail

GzipCompressorInputStreamback to summary
public GzipCompressorInputStream(final InputStream inputStream) throws IOException

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:InputStream

the InputStream from which this object should be created of

Exceptions
IOException:
if the stream could not be created
GzipCompressorInputStreamback to summary
public GzipCompressorInputStream(final InputStream inputStream, final boolean decompressConcatenated) throws IOException

Constructs a new input stream that decompresses gzip-compressed data from the specified input stream.

If decompressConcatenated is false: This decompressor might read more input than it will actually use. If inputStream supports mark and reset, then the input position will be adjusted so that it is right after the last byte of the compressed stream. If mark isn't supported, the input position will be undefined.

Parameters
inputStream:InputStream

the InputStream from which this object should be created of

decompressConcatenated:boolean

if true, decompress until the end of the input; if false, stop after the first .gz member

Exceptions
IOException:
if the stream could not be created

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes the input stream (unless it is System.in).

Annotations
@Override
Exceptions
IOException:

Doc from java.io.InputStream.close.

if an I/O error occurs.

Since
1.2
getCompressedCountback to summary
public long getCompressedCount()

Implements org.apache.commons.compress.utils.InputStreamStatistics.getCompressedCount.

Doc from org.apache.commons.compress.utils.InputStreamStatistics.getCompressedCount.

Gets the amount of raw or compressed bytes read by the stream.

Returns:long

the amount of raw or compressed bytes read by the stream.

Annotations
@Override
Since
1.17
getMetaDataback to summary
public GzipParameters getMetaData()

Provides the stream's meta data - may change with each stream when decompressing concatenated streams.

Returns:GzipParameters

the stream's meta data

Since
1.8
initback to summary
private boolean init(final boolean isFirstMember) throws IOException
matchesback to summary
public static boolean matches(final byte[] signature, final int length)

Checks if the signature matches what is expected for a .gz file.

Parameters
signature:byte[]

the bytes to check

length:int

the number of bytes to check

Returns:boolean

true if this is a .gz stream, false otherwise

Since
1.1
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns:int

the next byte of data, or -1 if the end of the stream is reached.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read(final byte[] b, int off, int len) throws IOException

Overrides java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b[0] through b[off-1] and elements b[off+len] through b[b.length-1] are unaffected.

Parameters
b:byte[]

the buffer into which the data is read.

off:int

the start offset in array b at which the data is written.

len:int

the maximum number of bytes to read.

Returns:int

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Annotations
@Override
Exceptions
IOException:
If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
Since
1.1
readToNullback to summary
private static byte[] readToNull(final DataInput inData) throws IOException