public class GzipCompressorInputStream extends CompressorInputStream implements InputStreamStatistics
This supports decompressing concatenated .gz files which is important when decompressing standalone .gz files.
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
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
.
Modifier and Type | Field and Description |
---|---|
private byte[] |
buf |
private int |
bufUsed |
private CountingInputStream |
countingStream |
private java.util.zip.CRC32 |
crc |
private boolean |
decompressConcatenated |
private boolean |
endReached |
private static int |
FCOMMENT |
private static int |
FEXTRA |
private static int |
FHCRC |
private static int |
FNAME |
private static int |
FRESERVED |
private java.io.InputStream |
in |
private java.util.zip.Inflater |
inf |
private byte[] |
oneByte |
private GzipParameters |
parameters |
Constructor and 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.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the input stream (unless it is System.in).
|
long |
getCompressedCount() |
GzipParameters |
getMetaData()
Provides the stream's meta data - may change with each stream
when decompressing concatenated streams.
|
private boolean |
init(boolean isFirstMember) |
static boolean |
matches(byte[] signature,
int length)
Checks if the signature matches what is expected for a .gz file.
|
int |
read() |
int |
read(byte[] b,
int off,
int len) |
private static byte[] |
readToNull(java.io.DataInput inData) |
count, count, getBytesRead, getCount, getUncompressedCount, pushedBackBytes
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getUncompressedCount
private static final int FHCRC
private static final int FEXTRA
private static final int FNAME
private static final int FCOMMENT
private static final int FRESERVED
private final CountingInputStream countingStream
private final java.io.InputStream in
private final boolean decompressConcatenated
private final byte[] buf
private int bufUsed
private java.util.zip.Inflater inf
private final java.util.zip.CRC32 crc
private boolean endReached
private final byte[] oneByte
private final GzipParameters parameters
public GzipCompressorInputStream(java.io.InputStream inputStream) throws java.io.IOException
This is equivalent to
GzipCompressorInputStream(inputStream, false)
and thus
will not decompress concatenated .gz files.
inputStream
- the InputStream from which this object should
be created ofjava.io.IOException
- if the stream could not be createdpublic GzipCompressorInputStream(java.io.InputStream inputStream, boolean decompressConcatenated) throws java.io.IOException
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.
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 memberjava.io.IOException
- if the stream could not be createdpublic GzipParameters getMetaData()
private boolean init(boolean isFirstMember) throws java.io.IOException
java.io.IOException
private static byte[] readToNull(java.io.DataInput inData) throws java.io.IOException
java.io.IOException
public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public int read(byte[] b, int off, int len) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public static boolean matches(byte[] signature, int length)
signature
- the bytes to checklength
- the number of bytes to checkpublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
public long getCompressedCount()
getCompressedCount
in interface InputStreamStatistics