Class GzipInflatingBuffer

java.lang.Object
io.grpc.internal.GzipInflatingBuffer
All Implemented Interfaces:
Closeable, AutoCloseable

@NotThreadSafe class GzipInflatingBuffer extends Object implements Closeable
Processes gzip streams, delegating to Inflater to perform on-demand inflation of the deflated blocks. Like GZIPInputStream, this handles concatenated gzip streams. Unlike GZIPInputStream, this allows for incremental processing of gzip streams, allowing data to be inflated as it arrives over the wire.

This also frees the inflate context when the end of a gzip stream is reached without another concatenated stream available to inflate.

  • Field Details

    • INFLATE_BUFFER_SIZE

      private static final int INFLATE_BUFFER_SIZE
      See Also:
    • UNSIGNED_SHORT_SIZE

      private static final int UNSIGNED_SHORT_SIZE
      See Also:
    • GZIP_MAGIC

      private static final int GZIP_MAGIC
      See Also:
    • GZIP_HEADER_MIN_SIZE

      private static final int GZIP_HEADER_MIN_SIZE
      See Also:
    • GZIP_TRAILER_SIZE

      private static final int GZIP_TRAILER_SIZE
      See Also:
    • HEADER_CRC_FLAG

      private static final int HEADER_CRC_FLAG
      See Also:
    • HEADER_EXTRA_FLAG

      private static final int HEADER_EXTRA_FLAG
      See Also:
    • HEADER_NAME_FLAG

      private static final int HEADER_NAME_FLAG
      See Also:
    • HEADER_COMMENT_FLAG

      private static final int HEADER_COMMENT_FLAG
      See Also:
    • gzippedData

      private final CompositeReadableBuffer gzippedData
      This buffer holds all input gzipped data, consisting of blocks of deflated data and the surrounding gzip headers and trailers. All access to the Gzip headers and trailers must be made via GzipInflatingBuffer.GzipMetadataReader.
    • crc

      private final CRC32 crc
    • gzipMetadataReader

      private final GzipInflatingBuffer.GzipMetadataReader gzipMetadataReader
    • inflaterInput

      private final byte[] inflaterInput
    • inflaterInputStart

      private int inflaterInputStart
    • inflaterInputEnd

      private int inflaterInputEnd
    • inflater

      private Inflater inflater
    • state

    • closed

      private boolean closed
    • gzipHeaderFlag

      private int gzipHeaderFlag
      Extra state variables for parsing gzip header flags.
    • headerExtraToRead

      private int headerExtraToRead
    • expectedGzipTrailerIsize

      private long expectedGzipTrailerIsize
    • bytesConsumed

      private int bytesConsumed
      Tracks gzipped bytes (including gzip metadata and deflated blocks) consumed during inflateBytes(byte[], int, int) calls.
    • deflatedBytesConsumed

      private int deflatedBytesConsumed
      Tracks deflated bytes (excluding gzip metadata) consumed by the inflater.
    • isStalled

      private boolean isStalled
  • Constructor Details

    • GzipInflatingBuffer

      GzipInflatingBuffer()
  • Method Details

    • isStalled

      boolean isStalled()
      Returns true when more bytes must be added via addGzippedBytes(io.grpc.internal.ReadableBuffer) to enable additional calls to inflateBytes(byte[], int, int) to make progress.
    • hasPartialData

      boolean hasPartialData()
      Returns true when there is gzippedData that has not been input to the inflater or the inflater has not consumed all of its input, or all data has been consumed but we are at not at the boundary between gzip streams.
    • addGzippedBytes

      void addGzippedBytes(ReadableBuffer buffer)
      Adds more gzipped data, which will be consumed only when needed to fulfill requests made via inflateBytes(byte[], int, int).
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • getAndResetBytesConsumed

      int getAndResetBytesConsumed()
      Reports bytes consumed by calls to inflateBytes(byte[], int, int) since the last invocation of this method, then resets the count to zero.
    • getAndResetDeflatedBytesConsumed

      int getAndResetDeflatedBytesConsumed()
      Reports bytes consumed by the inflater since the last invocation of this method, then resets the count to zero.
    • inflateBytes

      int inflateBytes(byte[] b, int offset, int length) throws DataFormatException, ZipException
      Attempts to inflate length bytes of data into b.

      Any gzipped bytes consumed by this method will be added to the counter returned by getAndResetBytesConsumed(). This method may consume gzipped bytes without writing any data to b, and may also write data to b without consuming additional gzipped bytes (if the inflater on an earlier call consumed the bytes necessary to produce output).

      Parameters:
      b - the destination array to receive the bytes.
      offset - the starting offset in the destination array.
      length - the number of bytes to be copied.
      Throws:
      IndexOutOfBoundsException - if b is too small to hold the requested bytes.
      DataFormatException
      ZipException
    • processHeader

      private boolean processHeader() throws ZipException
      Throws:
      ZipException
    • processHeaderExtraLen

      private boolean processHeaderExtraLen()
    • processHeaderExtra

      private boolean processHeaderExtra()
    • processHeaderName

      private boolean processHeaderName()
    • processHeaderComment

      private boolean processHeaderComment()
    • processHeaderCrc

      private boolean processHeaderCrc() throws ZipException
      Throws:
      ZipException
    • initializeInflater

      private boolean initializeInflater()
    • inflate

      private int inflate(byte[] b, int off, int len) throws DataFormatException, ZipException
      Throws:
      DataFormatException
      ZipException
    • fill

      private boolean fill()
    • processTrailer

      private boolean processTrailer() throws ZipException
      Throws:
      ZipException