APIdia Launch Partner
Top Description Fields Constructors Methods
java.util.zip

public Class GZIPInputStream

extends InflaterInputStream
Class Inheritance
Imports
java.io.SequenceInputStream, .ByteArrayInputStream, .FilterInputStream, .InputStream, .IOException, .EOFException, java.util.Objects

This class implements a stream filter for decompressing GZIP file format data.

GZIP file format

The GZIP file format is specified by RFC 1952. The format, as specified in section 2.2 of the RFC, consists of a series of "members" that appear one after another in the stream with no additional information before, between, or after them. Each member consists of a header, followed by data that is compressed using the deflate algorithm, and then a trailer.

This class is capable of reading a stream consisting of a series of members.

Reading from the stream may read and buffer bytes from the underlying stream. This includes bytes that follow a member's trailer. Whether or not any additional bytes have been read past a member's trailer, the read methods on this class yield decompressed data from at most one member; data from multiple members is not combined in a single read operation.

Thread safety

GZIPInputStream is not safe for use by multiple concurrent threads. Any multithreaded concurrent use must be guarded by appropriate synchronization.

API Note

The close method should be called to release resources used by this stream, either directly, or with the try-with-resources statement.

Implementation Note

After reading a member trailer, the read method calls InputStream#available() on the underlying stream to determine whether additional bytes are available that may represent a subsequent member. If the jdk.util.gzip.tryReadAheadAfterTrailer system property is set to true, then the call to InputStream.available() is skipped and the implementation instead attempts to read a subsequent member in the stream. GZIPInputStream depends on the return value of InputStream.available() to reliably process a stream with a series of members. Consequently, it may be necessary to set this property in environments that process streams with a series of members. By default, the jdk.util.gzip.tryReadAheadAfterTrailer system property is not set, and InputStream.available() gets called.

Since
1.1
External Specification
https://www.rfc-editor.org/info/rfc1952
See Also
InflaterInputStream

Field Summary

Modifier and TypeField and Description
private static final boolean
private boolean
protected CRC32
crc

CRC-32 for decompressed data.

protected boolean
eos

Indicates end of input stream.

private static final int
private static final int
private static final int
private static final int
public static final int
GZIP_MAGIC

GZIP header magic number.

private final byte[]
Inherited from java.util.zip.InflaterInputStream:
bufinflenusesDefaultInflater

Constructor Summary

AccessConstructor and Description
public
GZIPInputStream(InputStream
the input stream
in
,
int
the input buffer size
size
)

Creates a new input stream with the specified buffer size.

public
GZIPInputStream(InputStream
the input stream
in
)

Creates a new input stream with a default buffer size.

Method Summary

Modifier and TypeMethod and Description
private void
checkUnexpectedByte(final int b)

public void
close()

Overrides java.util.zip.InflaterInputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.

private static Inflater
private void
ensureOpen()

Hides java.util.zip.InflaterInputStream.ensureOpen.

Check to make sure that this stream has not been closed

private boolean
hasNoMoreMembers()

Reads the current GZIP member's trailer and returns true if the end-of-stream is reached.

public int

Returns:

the actual number of bytes decompressed from a GZIP member, or -1 if the end-of-stream is reached
read
(byte[]
the buffer into which the data is read
buf
,
int
the start offset in the destination array buf
off
,
int
the maximum number of bytes to read into buf
len
)

Overrides java.util.zip.InflaterInputStream.read.

Reads decompressed data into an array of bytes, returning the number of decompressed bytes.

private int
readHeader(InputStream stream, boolean failOnEOF)

private void
readTrailer(final InputStream
the InputStream containing the trailer
stream
)

Reads the current member's trailer

private int
private long
private int
private void
Inherited from java.util.zip.InflaterInputStream:
availablefillmarkmarkSupportedreadresetskip

Field Detail

alwaysReadNextMemberback to summary
private static final boolean alwaysReadNextMember
closedback to summary
private boolean closed

Hides java.util.zip.InflaterInputStream.closed.

crcback to summary
protected CRC32 crc

CRC-32 for decompressed data.

eosback to summary
protected boolean eos

Indicates end of input stream.

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
GZIP_MAGICback to summary
public static final int GZIP_MAGIC

GZIP header magic number.

tmpbufback to summary
private final byte[] tmpbuf

Constructor Detail

GZIPInputStreamback to summary
public GZIPInputStream(InputStream in, int size) throws IOException

Creates a new input stream with the specified buffer size.

Parameters
in:InputStream

the input stream

size:int

the input buffer size

Exceptions
IOException:
if an I/O error occurs when reading the member header from the underlying stream
ZipException:
if a GZIP format error has occurred or the compression method used is unsupported
NullPointerException:
if in is null
IllegalArgumentException:
if size <= 0
GZIPInputStreamback to summary
public GZIPInputStream(InputStream in) throws IOException

Creates a new input stream with a default buffer size.

Parameters
in:InputStream

the input stream

Exceptions
IOException:
if an I/O error occurs when reading the member header from the underlying stream
ZipException:
if a GZIP format error has occurred or the compression method used is unsupported
NullPointerException:
if in is null

Method Detail

checkUnexpectedByteback to summary
private void checkUnexpectedByte(final int b) throws IOException
closeback to summary
public void close() throws IOException

Overrides java.util.zip.InflaterInputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error has occurred
createInflaterback to summary
private static Inflater createInflater(InputStream in, int size)
ensureOpenback to summary
private void ensureOpen() throws IOException

Hides java.util.zip.InflaterInputStream.ensureOpen.

Check to make sure that this stream has not been closed

hasNoMoreMembersback to summary
private boolean hasNoMoreMembers() throws IOException

Reads the current GZIP member's trailer and returns true if the end-of-stream is reached. After reading the current member's trailer, if the stream has a subsequent GZIP member, then this method reads that member's header and returns false indicating that there is another member in the stream.

readback to summary
public int read(byte[] buf, int off, int len) throws IOException

Overrides java.util.zip.InflaterInputStream.read.

Reads decompressed data into an array of bytes, returning the number of decompressed bytes. If len is not zero, the method will block until some input can be decompressed; otherwise, no bytes are read and 0 is returned.

If this method returns a nonzero integer n then buf[off] through buf[off+n-1] contain the decompressed data. The content of elements buf[off+n] through buf[off+len-1] is undefined, contrary to the specification of the InputStream superclass, so an implementation is free to modify these elements during the inflate operation. If this method returns -1 or throws an exception then the content of buf[off] through buf[off+len-1] is undefined.

Parameters
buf:byte[]

the buffer into which the data is read

off:int

the start offset in the destination array buf

len:int

the maximum number of bytes to read into buf

Returns:int

the actual number of bytes decompressed from a GZIP member, or -1 if the end-of-stream is reached

Annotations
@Override
Exceptions
IOException:
if the stream is closed or an I/O error has occurred.
NullPointerException:
If buf is null.
IndexOutOfBoundsException:
If off is negative, len is negative, or len is greater than buf.length - off
ZipException:
if the compressed input data is corrupt.
See Also
GZIP file format
readHeaderback to summary
private int readHeader(InputStream stream, boolean failOnEOF) throws IOException
readTrailerback to summary
private void readTrailer(final InputStream stream) throws IOException

Reads the current member's trailer

Parameters
stream:InputStream

the InputStream containing the trailer

readUByteback to summary
private int readUByte(InputStream in) throws IOException
readUIntback to summary
private long readUInt(InputStream in) throws IOException
readUShortback to summary
private int readUShort(InputStream in) throws IOException
skipBytesback to summary
private void skipBytes(InputStream in, int n) throws IOException