Class HPackContext
- All Implemented Interfaces:
Serializable
HPackContext
contains one encoder and decoder for use on a HTTP/2 connection.
Creating a HPackContext
also requires a HPackContext.Session
. A HPack session is useful when forwarding HTTP/2 messages because it shares the list of headers that are not
allowed to be compressed. By default, creating a HPackContext
also creates a new session.
This class is not thread-safe. The Session
class is thread-safe.
- Since:
- 1.2.1
- See Also:
-
- RFC 7541
- Serialized Form
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
AnEncoderContext
is used for encoding a single header block.static class
static class
-
Constructor Summary
ConstructorsConstructorDescriptionHPackContext
(boolean useHuffmanEncoding, int decoderDynamicTableMaxSize) Creates a newHPackContext
with a newSession
.HPackContext
(HPackContext.Session session, boolean useHuffmanEncoding, int decoderDynamicTableMaxSize) Creates a newHPackContext
. -
Method Summary
Modifier and TypeMethodDescriptiondecodeHeaderBlock
(byte[] data) Decodes the given full header block data according to RFC 7541.void
encodeHeader
(HPackContext.EncoderContext context, String name, String value) Encodes the given header in theEncoderContext
according to RFC 7541.byte[]
encodeHeaders
(Map<String, String> headers) Encodes the given header name-value pairs according to RFC 7541 with a single newHPackContext.EncoderContext
.int
Returns the current maximum size of the dynamic table of the decoder.int
Returns the maximum size of the dynamic table of the decoder, as sent in a SETTINGS frame.int
Returns the current size of the dynamic table of the decoder.int
Returns the current maximum size of the dynamic table of the encoder, as received in a SETTINGS frame, possibly lowered usingsetEncoderDynamicTableMaxSizeCurrent(int)
.int
Returns the maximum size of the dynamic table of the encoder, as received in a SETTINGS frame.int
Returns the current size of the dynamic table of the encoder.Returns theSession
configured in the constructor.boolean
Returnstrue
if Huffman Encoding was enabled in the constructor.static long
readInteger
(byte[] data, int offset, int n, HPackContext.IntRef read) Decodes an integer from a byte array according to RFC 7541, section 5.1.static int
readInteger32
(byte[] data, int offset, int n, HPackContext.IntRef read) Equivalent toreadInteger(byte[], int, int, IntRef)
, except that-1
will be returned if the decoded integer is higher than a signed 32-bit value can represent (larger thanInteger.MAX_VALUE
).static byte[]
readString
(byte[] data, int offset, HPackContext.IntRef read) Decodes a UTF-8 string from the given byte array according to RFC 7541, section 5.2.void
setDecoderDynamicTableMaxSizeSettings
(int dynamicTableMaxSize) Sets the maximum size of the dynamic table of the decoder, as sent in a SETTINGS frame.void
setEncoderDynamicTableMaxSizeCurrent
(int dynamicTableMaxSize) Sets the current maximum size of the dynamic table of the encoder.void
setEncoderDynamicTableMaxSizeSettings
(int dynamicTableMaxSize) Sets the maximum size of the dynamic table of the encoder, as received in a SETTINGS frame.static int
writeInteger
(byte[] dest, int offset, int n, long value) Encodes an integer into a byte array according to RFC 7541, section 5.1.static int
writeString
(byte[] dest, int offset, byte[] strdata, boolean huffman) Encodes string data into the given byte array according to RFC 7541, section 5.2.
-
Constructor Details
-
HPackContext
public HPackContext(boolean useHuffmanEncoding, int decoderDynamicTableMaxSize) Creates a newHPackContext
with a newSession
.- Parameters:
useHuffmanEncoding
- Whether to use Huffman EncodingdecoderDynamicTableMaxSize
- The maximum table size of the decoder, as sent in a SETTINGS frame
-
HPackContext
public HPackContext(HPackContext.Session session, boolean useHuffmanEncoding, int decoderDynamicTableMaxSize) Creates a newHPackContext
.- Parameters:
session
- TheHPackContext.Session
useHuffmanEncoding
- Whether to use Huffman EncodingdecoderDynamicTableMaxSize
- The maximum table size of the decoder, as sent in a SETTINGS frame
-
-
Method Details
-
decodeHeaderBlock
Decodes the given full header block data according to RFC 7541.- Parameters:
data
- The full header data- Returns:
- A
HTTPHeaderContainer
containing the decoded header data, ornull
if a decoding error occurred
-
encodeHeaders
Encodes the given header name-value pairs according to RFC 7541 with a single newHPackContext.EncoderContext
.- Parameters:
headers
- The header pairs- Returns:
- The encoded data
- See Also:
-
encodeHeader
Encodes the given header in theEncoderContext
according to RFC 7541.- Parameters:
context
- TheEncoderContext
to encode this header inname
- The name of the headervalue
- The value of the header- Throws:
IllegalStateException
- IfsetEncoderDynamicTableMaxSizeSettings(int)
was never called- See Also:
-
isUseHuffmanEncoding
public boolean isUseHuffmanEncoding()Returnstrue
if Huffman Encoding was enabled in the constructor.- Returns:
- Whether Huffman Encoding is enabled
-
getEncoderDynamicTableMaxSizeSettings
public int getEncoderDynamicTableMaxSizeSettings()Returns the maximum size of the dynamic table of the encoder, as received in a SETTINGS frame.- Returns:
- The maximum encoder dynamic table size
- Throws:
IllegalStateException
- IfsetEncoderDynamicTableMaxSizeSettings(int)
was never called- See Also:
-
setEncoderDynamicTableMaxSizeSettings
public void setEncoderDynamicTableMaxSizeSettings(int dynamicTableMaxSize) Sets the maximum size of the dynamic table of the encoder, as received in a SETTINGS frame.This method must be called before encoding headers using this
HPackContext
.- Parameters:
dynamicTableMaxSize
- The maximum encoder dynamic table size- See Also:
-
getEncoderDynamicTableMaxSizeCurrent
public int getEncoderDynamicTableMaxSizeCurrent()Returns the current maximum size of the dynamic table of the encoder, as received in a SETTINGS frame, possibly lowered usingsetEncoderDynamicTableMaxSizeCurrent(int)
.- Returns:
- The maximum encoder dynamic table size
- See Also:
-
setEncoderDynamicTableMaxSizeCurrent
public void setEncoderDynamicTableMaxSizeCurrent(int dynamicTableMaxSize) Sets the current maximum size of the dynamic table of the encoder.If
setEncoderDynamicTableMaxSizeSettings(int)
was called with the value received in a SETTINGS frame, this must be lower than or equal to that value. If lower, a Dynamic Table Size Update will be sent in the next encoded HPACK headers.This method may be called before a call to
setEncoderDynamicTableMaxSizeSettings(int)
to set a maximum value before a SETTINGS frame was received. If the SETTINGS maximum is lower than this value, the maximum set by this method will be lowered to the received value, otherwise, it will be unchanged.The given dynamicTableMaxSize may be
-1
, to always use the value set bysetEncoderDynamicTableMaxSizeSettings(int)
as the maximum dynamic table size. This is the default behavior.- Parameters:
dynamicTableMaxSize
- The new maximum encoder dynamic table size- See Also:
-
getEncoderDynamicTableSize
public int getEncoderDynamicTableSize()Returns the current size of the dynamic table of the encoder.- Returns:
- The current encoder dynamic table size
-
getDecoderDynamicTableMaxSizeSettings
public int getDecoderDynamicTableMaxSizeSettings()Returns the maximum size of the dynamic table of the decoder, as sent in a SETTINGS frame.- Returns:
- The maximum decoder dynamic table size
- See Also:
-
setDecoderDynamicTableMaxSizeSettings
public void setDecoderDynamicTableMaxSizeSettings(int dynamicTableMaxSize) Sets the maximum size of the dynamic table of the decoder, as sent in a SETTINGS frame.- Parameters:
dynamicTableMaxSize
- The maximum decoder dynamic table size- See Also:
-
getDecoderDynamicTableMaxSizeCurrent
public int getDecoderDynamicTableMaxSizeCurrent()Returns the current maximum size of the dynamic table of the decoder. This value is lower or equal to a value sent in a SETTINGS frame (set bysetDecoderDynamicTableMaxSizeSettings(int)
), possibly modified by a received Dynamic Table Size Update.- Returns:
- The maximum decoder dynamic table size
- See Also:
-
getDecoderDynamicTableSize
public int getDecoderDynamicTableSize()Returns the current size of the dynamic table of the decoder.- Returns:
- The current decoder dynamic table size
-
getSession
Returns theSession
configured in the constructor.- Returns:
- The
Session
-
writeInteger
public static int writeInteger(byte[] dest, int offset, int n, long value) Encodes an integer into a byte array according to RFC 7541, section 5.1.- Parameters:
dest
- The destination arrayoffset
- The offset in the array where to start writing ton
- The prefix size Nvalue
- The integer value to write- Returns:
- The number of additional bytes written. A value of
0
means only the byte at index offset was modified - Throws:
IndexOutOfBoundsException
- The full value could not be written because the array is too short; all bytes starting at offset may or may not have been modified in that case- See Also:
-
readInteger
Decodes an integer from a byte array according to RFC 7541, section 5.1.- Parameters:
data
- The byte arrayoffset
- The offset where to start reading fromn
- The prefix size Nread
- Returns the number of additional bytes read, by adding it to the current value; if unchanged means only the byte at offset was read. Only valid if the return value of this function is positive. May benull
- Returns:
- The decoded integer, or a negative value if an error occurred
- See Also:
-
readInteger32
Equivalent toreadInteger(byte[], int, int, IntRef)
, except that-1
will be returned if the decoded integer is higher than a signed 32-bit value can represent (larger thanInteger.MAX_VALUE
).- Parameters:
data
- The byte arrayoffset
- The offset where to start reading fromn
- The prefix size Nread
- Returns the number of additional bytes read- Returns:
- The decoded integer, or a negative value if an error occurred or the decoded integer is larger than 31 bits
-
writeString
public static int writeString(byte[] dest, int offset, byte[] strdata, boolean huffman) Encodes string data into the given byte array according to RFC 7541, section 5.2.- Parameters:
dest
- The destination arrayoffset
- The offset in the array where to start writing tostrdata
- The encoded string data to writehuffman
- Whether the string should be encoded using huffman coding- Returns:
- The number of additional bytes written. A value of
0
means only the byte at index offset was modified - Throws:
IndexOutOfBoundsException
- The full string could not be written because the array is too short; all bytes starting at offset may or may not have been modified in that case
-
readString
Decodes a UTF-8 string from the given byte array according to RFC 7541, section 5.2.- Parameters:
data
- The byte arrayoffset
- The offset where to start reading fromread
- Returns the number of additional bytes read, by adding it to the current value; if unchanged means only the byte at offset was read and the string is empty. Only valid if the return value of this function is notnull
. May benull
- Returns:
- The decoded string data, or
null
if the array is truncated or a decoding error occurred
-