Class GZIPInputStream

All Implemented Interfaces:
Closeable, AutoCloseable

public class GZIPInputStream extends InflaterInputStream
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.
Since:
1.1
External Specifications
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected CRC32
    CRC-32 for decompressed data.
    protected boolean
    Indicates end of input stream.
    static final int
    GZIP header magic number.

    Fields declared in class InflaterInputStream

    buf, inf, len
    Modifier and Type
    Field
    Description
    protected byte[]
    Input buffer for decompression.
    protected Inflater
    Decompressor for this stream.
    protected int
    The total number of bytes read into the input buffer.

    Fields declared in class FilterInputStream

    in
    Modifier and Type
    Field
    Description
    protected InputStream
    The input stream to be filtered.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new input stream with a default buffer size.
    Creates a new input stream with the specified buffer size.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this input stream and releases any system resources associated with the stream.
    int
    read(byte[] buf, int off, int len)
    Reads decompressed data into an array of bytes, returning the number of decompressed bytes.

    Methods declared in class InflaterInputStream

    available, fill, mark, markSupported, read, reset, skip
    Modifier and Type
    Method
    Description
    int
    Returns 0 after EOF has been reached, otherwise always return 1.
    protected void
    Fills input buffer with more data to decompress.
    void
    mark(int readlimit)
    Marks the current position in this input stream.
    boolean
    Tests if this input stream supports the mark and reset methods.
    int
    Reads a byte of uncompressed data.
    void
    Repositions this stream to the position at the time the mark method was last called on this input stream.
    long
    skip(long n)
    Skips specified number of bytes of uncompressed data.

    Methods declared in class FilterInputStream

    read
    Modifier and Type
    Method
    Description
    int
    read(byte[] b)
    Reads up to b.length bytes of data from this input stream into an array of bytes.

    Methods declared in class InputStream

    nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
    Modifier and Type
    Method
    Description
    Returns a new InputStream that reads no bytes.
    byte[]
    Reads all remaining bytes from the input stream.
    int
    readNBytes(byte[] b, int off, int len)
    Reads the requested number of bytes from the input stream into the given byte array.
    byte[]
    readNBytes(int len)
    Reads up to a specified number of bytes from the input stream.
    void
    skipNBytes(long n)
    Skips over and discards exactly n bytes of data from this input stream.
    long
    Reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read.

    Methods declared in class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    Modifier and Type
    Method
    Description
    protected Object
    Creates and returns a copy of this object.
    boolean
    Indicates whether some other object is "equal to" this one.
    protected void
    Deprecated, for removal: This API element is subject to removal in a future version.
    Finalization is deprecated and subject to removal in a future release.
    final Class<?>
    Returns the runtime class of this Object.
    int
    Returns a hash code value for this object.
    final void
    Wakes up a single thread that is waiting on this object's monitor.
    final void
    Wakes up all threads that are waiting on this object's monitor.
    Returns a string representation of the object.
    final void
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
    final void
    wait(long timeoutMillis)
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
    final void
    wait(long timeoutMillis, int nanos)
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  • Field Details

    • GZIP_MAGIC

      public static final int GZIP_MAGIC
      GZIP header magic number.
      See Also:
    • crc

      protected CRC32 crc
      CRC-32 for decompressed data.
    • eos

      protected boolean eos
      Indicates end of input stream.
  • Constructor Details

    • GZIPInputStream

      public GZIPInputStream(InputStream in, int size) throws IOException
      Creates a new input stream with the specified buffer size.
      Parameters:
      in - the input stream
      size - the input buffer size
      Throws:
      ZipException - if a GZIP format error has occurred or the compression method used is unsupported
      NullPointerException - if in is null
      IOException - if an I/O error occurs when reading the member header from the underlying stream
      IllegalArgumentException - if size <= 0
    • GZIPInputStream

      public GZIPInputStream(InputStream in) throws IOException
      Creates a new input stream with a default buffer size.
      Parameters:
      in - the input stream
      Throws:
      ZipException - if a GZIP format error has occurred or the compression method used is unsupported
      NullPointerException - if in is null
      IOException - if an I/O error occurs when reading the member header from the underlying stream
  • Method Details

    • read

      public int read(byte[] buf, int off, int len) throws IOException
      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.

      Overrides:
      read in class InflaterInputStream
      Parameters:
      buf - the buffer into which the data is read
      off - the start offset in the destination array buf
      len - the maximum number of bytes to read into buf
      Returns:
      the actual number of bytes decompressed from a GZIP member, or -1 if the end-of-stream is reached
      Throws:
      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.
      IOException - if the stream is closed or an I/O error has occurred.
      See Also:
    • close

      public void close() throws IOException
      Closes this input stream and releases any system resources associated with the stream.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InflaterInputStream
      Throws:
      IOException - if an I/O error has occurred
      See Also: