Package io.grpc.internal
Class GzipInflatingBuffer
- java.lang.Object
-
- io.grpc.internal.GzipInflatingBuffer
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
@NotThreadSafe class GzipInflatingBuffer extends java.lang.Object implements java.io.CloseableProcesses gzip streams, delegating toInflaterto perform on-demand inflation of the deflated blocks. LikeGZIPInputStream, this handles concatenated gzip streams. UnlikeGZIPInputStream, 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classGzipInflatingBuffer.GzipMetadataReaderReads gzip header and trailer bytes from the inflater's buffer (if bytes beyond the inflate block were given to the inflater) and then fromgzippedData, and handles updating the CRC and the count of gzipped bytes consumed.private static classGzipInflatingBuffer.State
-
Field Summary
Fields Modifier and Type Field Description private intbytesConsumedTracks gzipped bytes (including gzip metadata and deflated blocks) consumed duringinflateBytes(byte[], int, int)calls.private booleanclosedprivate java.util.zip.CRC32crcprivate intdeflatedBytesConsumedTracks deflated bytes (excluding gzip metadata) consumed by the inflater.private longexpectedGzipTrailerIsizeprivate static intGZIP_HEADER_MIN_SIZEprivate static intGZIP_MAGICprivate static intGZIP_TRAILER_SIZEprivate intgzipHeaderFlagExtra state variables for parsing gzip header flags.private GzipInflatingBuffer.GzipMetadataReadergzipMetadataReaderprivate CompositeReadableBuffergzippedDataThis buffer holds all input gzipped data, consisting of blocks of deflated data and the surrounding gzip headers and trailers.private static intHEADER_COMMENT_FLAGprivate static intHEADER_CRC_FLAGprivate static intHEADER_EXTRA_FLAGprivate static intHEADER_NAME_FLAGprivate intheaderExtraToReadprivate static intINFLATE_BUFFER_SIZEprivate java.util.zip.Inflaterinflaterprivate byte[]inflaterInputprivate intinflaterInputEndprivate intinflaterInputStartprivate booleanisStalledprivate GzipInflatingBuffer.Statestateprivate static intUNSIGNED_SHORT_SIZE
-
Constructor Summary
Constructors Constructor Description GzipInflatingBuffer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidaddGzippedBytes(ReadableBuffer buffer)Adds more gzipped data, which will be consumed only when needed to fulfill requests made viainflateBytes(byte[], int, int).voidclose()private booleanfill()(package private) intgetAndResetBytesConsumed()Reports bytes consumed by calls toinflateBytes(byte[], int, int)since the last invocation of this method, then resets the count to zero.(package private) intgetAndResetDeflatedBytesConsumed()Reports bytes consumed by the inflater since the last invocation of this method, then resets the count to zero.(package private) booleanhasPartialData()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.private intinflate(byte[] b, int off, int len)(package private) intinflateBytes(byte[] b, int offset, int length)Attempts to inflatelengthbytes of data intob.private booleaninitializeInflater()(package private) booleanisStalled()Returns true when more bytes must be added viaaddGzippedBytes(io.grpc.internal.ReadableBuffer)to enable additional calls toinflateBytes(byte[], int, int)to make progress.private booleanprocessHeader()private booleanprocessHeaderComment()private booleanprocessHeaderCrc()private booleanprocessHeaderExtra()private booleanprocessHeaderExtraLen()private booleanprocessHeaderName()private booleanprocessTrailer()
-
-
-
Field Detail
-
INFLATE_BUFFER_SIZE
private static final int INFLATE_BUFFER_SIZE
- See Also:
- Constant Field Values
-
UNSIGNED_SHORT_SIZE
private static final int UNSIGNED_SHORT_SIZE
- See Also:
- Constant Field Values
-
GZIP_MAGIC
private static final int GZIP_MAGIC
- See Also:
- Constant Field Values
-
GZIP_HEADER_MIN_SIZE
private static final int GZIP_HEADER_MIN_SIZE
- See Also:
- Constant Field Values
-
GZIP_TRAILER_SIZE
private static final int GZIP_TRAILER_SIZE
- See Also:
- Constant Field Values
-
HEADER_CRC_FLAG
private static final int HEADER_CRC_FLAG
- See Also:
- Constant Field Values
-
HEADER_EXTRA_FLAG
private static final int HEADER_EXTRA_FLAG
- See Also:
- Constant Field Values
-
HEADER_NAME_FLAG
private static final int HEADER_NAME_FLAG
- See Also:
- Constant Field Values
-
HEADER_COMMENT_FLAG
private static final int HEADER_COMMENT_FLAG
- See Also:
- Constant Field Values
-
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 viaGzipInflatingBuffer.GzipMetadataReader.
-
crc
private final java.util.zip.CRC32 crc
-
gzipMetadataReader
private final GzipInflatingBuffer.GzipMetadataReader gzipMetadataReader
-
inflaterInput
private final byte[] inflaterInput
-
inflaterInputStart
private int inflaterInputStart
-
inflaterInputEnd
private int inflaterInputEnd
-
inflater
private java.util.zip.Inflater inflater
-
state
private GzipInflatingBuffer.State 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 duringinflateBytes(byte[], int, int)calls.
-
deflatedBytesConsumed
private int deflatedBytesConsumed
Tracks deflated bytes (excluding gzip metadata) consumed by the inflater.
-
isStalled
private boolean isStalled
-
-
Method Detail
-
isStalled
boolean isStalled()
Returns true when more bytes must be added viaaddGzippedBytes(io.grpc.internal.ReadableBuffer)to enable additional calls toinflateBytes(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 viainflateBytes(byte[], int, int).
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
getAndResetBytesConsumed
int getAndResetBytesConsumed()
Reports bytes consumed by calls toinflateBytes(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 java.util.zip.DataFormatException, java.util.zip.ZipExceptionAttempts to inflatelengthbytes of data intob.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 tob, and may also write data tobwithout 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:
java.lang.IndexOutOfBoundsException- ifbis too small to hold the requested bytes.java.util.zip.DataFormatExceptionjava.util.zip.ZipException
-
processHeader
private boolean processHeader() throws java.util.zip.ZipException- Throws:
java.util.zip.ZipException
-
processHeaderExtraLen
private boolean processHeaderExtraLen()
-
processHeaderExtra
private boolean processHeaderExtra()
-
processHeaderName
private boolean processHeaderName()
-
processHeaderComment
private boolean processHeaderComment()
-
processHeaderCrc
private boolean processHeaderCrc() throws java.util.zip.ZipException- Throws:
java.util.zip.ZipException
-
initializeInflater
private boolean initializeInflater()
-
inflate
private int inflate(byte[] b, int off, int len) throws java.util.zip.DataFormatException, java.util.zip.ZipException- Throws:
java.util.zip.DataFormatExceptionjava.util.zip.ZipException
-
fill
private boolean fill()
-
processTrailer
private boolean processTrailer() throws java.util.zip.ZipException- Throws:
java.util.zip.ZipException
-
-