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.Closeable
Processes gzip streams, delegating toInflater
to 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 class
GzipInflatingBuffer.GzipMetadataReader
Reads 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 class
GzipInflatingBuffer.State
-
Field Summary
Fields Modifier and Type Field Description private int
bytesConsumed
Tracks gzipped bytes (including gzip metadata and deflated blocks) consumed duringinflateBytes(byte[], int, int)
calls.private boolean
closed
private java.util.zip.CRC32
crc
private int
deflatedBytesConsumed
Tracks deflated bytes (excluding gzip metadata) consumed by the inflater.private long
expectedGzipTrailerIsize
private static int
GZIP_HEADER_MIN_SIZE
private static int
GZIP_MAGIC
private static int
GZIP_TRAILER_SIZE
private int
gzipHeaderFlag
Extra state variables for parsing gzip header flags.private GzipInflatingBuffer.GzipMetadataReader
gzipMetadataReader
private CompositeReadableBuffer
gzippedData
This buffer holds all input gzipped data, consisting of blocks of deflated data and the surrounding gzip headers and trailers.private static int
HEADER_COMMENT_FLAG
private static int
HEADER_CRC_FLAG
private static int
HEADER_EXTRA_FLAG
private static int
HEADER_NAME_FLAG
private int
headerExtraToRead
private static int
INFLATE_BUFFER_SIZE
private java.util.zip.Inflater
inflater
private byte[]
inflaterInput
private int
inflaterInputEnd
private int
inflaterInputStart
private boolean
isStalled
private GzipInflatingBuffer.State
state
private static int
UNSIGNED_SHORT_SIZE
-
Constructor Summary
Constructors Constructor Description GzipInflatingBuffer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
addGzippedBytes(ReadableBuffer buffer)
Adds more gzipped data, which will be consumed only when needed to fulfill requests made viainflateBytes(byte[], int, int)
.void
close()
private boolean
fill()
(package private) int
getAndResetBytesConsumed()
Reports bytes consumed by calls toinflateBytes(byte[], int, int)
since the last invocation of this method, then resets the count to zero.(package private) int
getAndResetDeflatedBytesConsumed()
Reports bytes consumed by the inflater since the last invocation of this method, then resets the count to zero.(package private) 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.private int
inflate(byte[] b, int off, int len)
(package private) int
inflateBytes(byte[] b, int offset, int length)
Attempts to inflatelength
bytes of data intob
.private boolean
initializeInflater()
(package private) 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.private boolean
processHeader()
private boolean
processHeaderComment()
private boolean
processHeaderCrc()
private boolean
processHeaderExtra()
private boolean
processHeaderExtraLen()
private boolean
processHeaderName()
private boolean
processTrailer()
-
-
-
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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in 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.ZipException
Attempts to inflatelength
bytes 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 tob
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:
java.lang.IndexOutOfBoundsException
- ifb
is too small to hold the requested bytes.java.util.zip.DataFormatException
java.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.DataFormatException
java.util.zip.ZipException
-
fill
private boolean fill()
-
processTrailer
private boolean processTrailer() throws java.util.zip.ZipException
- Throws:
java.util.zip.ZipException
-
-