APIdia Launch Partner
Top Description Fields Constructors Methods
jdk.internal.net.http.quic

public Class VariableLengthEncoder

extends Object
Class Inheritance
Imports
java.nio.ByteBuffer

QUIC packets and frames commonly use a variable-length encoding for non-negative values. This encoding ensures that smaller values will use less in the packet or frame.

The QUIC variable-length encoding reserves the two most significant bits of the first byte to encode the size of the length value as a base 2 logarithm value. The length itself is then encoded on the remaining bits, in network byte order. This means that the length values will be encoded on 1, 2, 4, or 8 bytes and can encode 6-, 14-, 30-, or 62-bit values respectively, or a value within the range of 0 to 4611686018427387903 inclusive.

External Specification
https://www.rfc-editor.org/rfc/rfc9000.html#integer-encoding

Field Summary

Modifier and TypeField and Description
public static final long
MAX_ENCODED_INTEGER

The maximum value a variable length integer can take.

public static final int
MAX_INTEGER_LENGTH

The maximum number of bytes on which a variable length integer can be encoded.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static long

Returns:

the value. If an error occurs, -1 is returned and the buffer position is left unchanged.
decode
(ByteBuffer
the ByteBuffer that the length will be decoded from
buffer
)

Decode a variable length value from ByteBuffer.

public static long

Returns:

the value. If an error occurs, -1 is returned and the buffer position is left unchanged.
decode
(BuffersReader
the BuffersReader that the length will be decoded from
buffers
)

Decode a variable length value from BuffersReader.

public static int

Returns:

the position of the buffer
encode
(ByteBuffer
the ByteBuffer that the length will be encoded into
buffer
,
long
the variable length value
value
)

Encode (a variable length) value into ByteBuffer.

public static int

Returns:

the number of bytes needed to encode the given value as a variable length integer.
getEncodedSize
(long
the value to encode
value
)

Returns the number of bytes needed to encode the given value as a variable length integer.

public static int

Returns:

the base 2 logarithm of the number of bytes required to encode the value as a variable length integer.
getVariableLengthPrefix
(long
the value to encode
value
)

Returns the variable length prefix.

public static long

Returns:

the variable length value encoded at the given offset, or -1
peekEncodedValue
(ByteBuffer
the buffer to read from
buffer
,
int
the offset in the buffer to start reading from
offset
)

Peeks at a variable length value encoded at the given offset.

public static long

Returns:

the variable length value encoded at the given offset, or -1
peekEncodedValue
(BuffersReader
the buffer to read from
buffers
,
long
the offset in the buffer to start reading from
offset
)

Peeks at a variable length value encoded at the given offset.

public static int

Returns:

the number of bytes on which the variable length value is encoded at the given offset, or -1
peekEncodedValueSize
(ByteBuffer
the buffer to read from
buffer
,
int
the offset in the buffer to start reading from
offset
)

Peeks at a variable length value encoded at the given offset, and return the number of bytes on which this value is encoded.

public static int

Returns:

the number of bytes on which the variable length value is encoded at the given offset, or -1
peekEncodedValueSize
(BuffersReader
the buffers to read from
buffers
,
long
the offset in the buffer to start reading from
offset
)

Peeks at a variable length value encoded at the given offset, and return the number of bytes on which this value is encoded.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

MAX_ENCODED_INTEGERback to summary
public static final long MAX_ENCODED_INTEGER

The maximum value a variable length integer can take.

MAX_INTEGER_LENGTHback to summary
public static final int MAX_INTEGER_LENGTH

The maximum number of bytes on which a variable length integer can be encoded.

Constructor Detail

VariableLengthEncoderback to summary
private VariableLengthEncoder()

Method Detail

decodeback to summary
public static long decode(ByteBuffer buffer)

Decode a variable length value from ByteBuffer. This method assumes that the position of buffer has been set to the first byte where the length begins. If the methods completes successfully, the position will be set to the byte after the last byte read.

Parameters
buffer:ByteBuffer

the ByteBuffer that the length will be decoded from

Returns:long

the value. If an error occurs, -1 is returned and the buffer position is left unchanged.

decodeback to summary
public static long decode(BuffersReader buffers)

Decode a variable length value from BuffersReader. This method assumes that the position of buffers has been set to the first byte where the length begins. If the methods completes successfully, the position will be set to the byte after the last byte.

Parameters
buffers:BuffersReader

the BuffersReader that the length will be decoded from

Returns:long

the value. If an error occurs, -1 is returned and the buffer position is left unchanged.

encodeback to summary
public static int encode(ByteBuffer buffer, long value) throws IllegalArgumentException

Encode (a variable length) value into ByteBuffer. This method assumes that the position of buffer has been set to the first byte where the length begins. If the methods completes successfully, the position will be set to the byte after the last length byte.

Parameters
buffer:ByteBuffer

the ByteBuffer that the length will be encoded into

value:long

the variable length value

Returns:int

the position of the buffer

Exceptions
IllegalArgumentException:
if value supplied falls outside of acceptable bounds [0, 2^62-1], or if the given buffer doesn't contain enough space to encode the value
getEncodedSizeback to summary
public static int getEncodedSize(long value) throws IllegalArgumentException

Returns the number of bytes needed to encode the given value as a variable length integer. This a number between 1 and 8.

Parameters
value:long

the value to encode

Returns:int

the number of bytes needed to encode the given value as a variable length integer.

Exceptions
IllegalArgumentException:
if the value supplied falls outside of acceptable bounds [0, 2^62-1]
getVariableLengthPrefixback to summary
public static int getVariableLengthPrefix(long value) throws IllegalArgumentException

Returns the variable length prefix. The variable length prefix is the base 2 logarithm of the number of bytes required to encode a positive value as a variable length integer: [0, 1, 2, 3] for [1, 2, 4, 8] bytes.

Parameters
value:long

the value to encode

Returns:int

the base 2 logarithm of the number of bytes required to encode the value as a variable length integer.

Exceptions
IllegalArgumentException:
if the supplied value falls outside the acceptable bounds [0, 2^62-1]
peekEncodedValueback to summary
public static long peekEncodedValue(ByteBuffer buffer, int offset)

Peeks at a variable length value encoded at the given offset. If the byte buffer doesn't contain enough bytes to read the variable length value, -1 is returned.

This method doesn't advance the buffer position.

Parameters
buffer:ByteBuffer

the buffer to read from

offset:int

the offset in the buffer to start reading from

Returns:long

the variable length value encoded at the given offset, or -1

peekEncodedValueback to summary
public static long peekEncodedValue(BuffersReader buffers, long offset)

Peeks at a variable length value encoded at the given offset. If the byte buffer doesn't contain enough bytes to read the variable length value, -1 is returned. This method doesn't advance the buffer position.

Parameters
buffers:BuffersReader

the buffer to read from

offset:long

the offset in the buffer to start reading from

Returns:long

the variable length value encoded at the given offset, or -1

peekEncodedValueSizeback to summary
public static int peekEncodedValueSize(ByteBuffer buffer, int offset)

Peeks at a variable length value encoded at the given offset, and return the number of bytes on which this value is encoded. If the byte buffer is empty or the offset is past the limit -1 is returned. This method doesn't advance the buffer position.

Parameters
buffer:ByteBuffer

the buffer to read from

offset:int

the offset in the buffer to start reading from

Returns:int

the number of bytes on which the variable length value is encoded at the given offset, or -1

peekEncodedValueSizeback to summary
public static int peekEncodedValueSize(BuffersReader buffers, long offset)

Peeks at a variable length value encoded at the given offset, and return the number of bytes on which this value is encoded. If the byte buffer is empty or the offset is past the limit -1 is returned. This method doesn't advance the buffer position.

Parameters
buffers:BuffersReader

the buffers to read from

offset:long

the offset in the buffer to start reading from

Returns:int

the number of bytes on which the variable length value is encoded at the given offset, or -1