All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class GunzipOutputStreamextends InflaterOutputStream
Gunzip implementation per RFC 1952, reusing java's standard CRC32 and Inflater and InflaterOutputStream implementations.

Note that the underlying InflaterOutputStream cannot be reused after close(), so we don't have a Reusable version of this.

Sets up GunzipOutputStream -- InflaterOutputStream -- CRC32OutputStream -- uncompressedStream

Not a public API, subject to change, not for external use.

Modified from net.i2p.util.ResettableGZIPInputStream to use Java 6 InflaterOutputstream.

  • Field Details

    • _buf1

      private final byte[] _buf1
      One-byte read buffer.
    • _complete

      private boolean _complete
      True once the gzip stream is complete.
    • _validated

      private boolean _validated
      True once the footer checks passed.
    • _bytesReceived

      private long _bytesReceived
      Total bytes fed to the stream.
    • _bytesReceivedAtCompletion

      private long _bytesReceivedAtCompletion
      Byte count when the stream completed.
    • _state

      Current state of the header parse.
    • _flags

      private int _flags
      The gzip flags read from the header.
    • _extHdrToRead

      private int _extHdrToRead
      Bytes left of the extra header field.
    • _callback

      private final LimitOutputStream.DoneCallback _callback
      Callback run when the stream completes.
    • _log

      private final Log _log
      Logger for the gunzip stream.
  • Constructor Details

    • GunzipOutputStream

      public GunzipOutputStream(OutputStream uncompressedStream) throws IOException
      Build a new Gunzip stream.
      Parameters:
      uncompressedStream - the sink the decompressed bytes are written to
      Throws:
      IOException - if the underlying CRC32/Deflater stream cannot be created
    • GunzipOutputStream

      public GunzipOutputStream(OutputStream uncompressedStream, LimitOutputStream.DoneCallback cb)
      With a callback when done.
      Parameters:
      uncompressedStream - the sink the decompressed bytes are written to
      cb - may be null
  • Method Details

    • write

      public void write(int b) throws IOException
      Overrides:
      write in class InflaterOutputStream
      Throws:
      IOException
    • write

      public void write(byte[] buf, int off, int len) throws IOException
      Overrides:
      write in class InflaterOutputStream
      Throws:
      IOException
    • getTotalRead

      public long getTotalRead()
      Inflater statistic.
      Returns:
      the total read
    • getTotalExpanded

      public long getTotalExpanded()
      Inflater statistic.
      Returns:
      the total expanded
    • getRemaining

      public long getRemaining()
      Inflater statistic.
      Returns:
      the number of inflated bytes still buffered inside the inflater, or 0 if the inflater is unavailable
    • getFinished

      public boolean getFinished()
      Inflater statistic.
      Returns:
      true if the inflater has finished decompressing the compressed input; also true if the inflater is unavailable
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InflaterOutputStream
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • verifyFooter

      private void verifyFooter() throws IOException
      Check the gzip CRC and size trailer.
      Throws:
      IOException - on CRC or length check fail
    • verifyHeader

      private void verifyHeader(byte b) throws IOException
      Make sure the header is valid, throwing an IOException if it is bad. Pushes through the state machine, checking as we go. Call for each byte until HeaderState is DONE.
      Parameters:
      b - the next header byte, read as unsigned
      Throws:
      IOException - if the byte does not match what the current header state expects