Package io.github.mahmoudimus.http3
Class QuicVarInt
java.lang.Object
io.github.mahmoudimus.http3.QuicVarInt
QUIC variable-length integers (RFC 9000 §16), which HTTP/3 uses for frame types, lengths,
settings, stream types, push IDs and error codes. The two most significant bits of the first
byte give the length (1, 2, 4 or 8 bytes); the rest is the value, big-endian, from 0 to
2^62 - 1.
Writing always uses the shortest encoding. Reading accepts any encoding, including non-minimal ones, as RFC 9000 requires.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe longest encoding, in bytes.static final longThe largest value, 2^62 - 1. -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]encode(long value) The shortest encoding ofvalue.static intlength(long value) The length of the shortest encoding ofvalue.static intlengthOf(int firstByte) The total length of an encoding, from its first byte.static longread(byte[] src, int offset, int end) Reads one integer fromsrc[offset..end).static longread(InputStream in) Reads one integer.static longread(ByteBuffer buf) Reads one integer from the buffer's position.static intwrite(long value, byte[] dst, int offset) Writes the shortest encoding ofvalueatoffset.static voidwrite(long value, OutputStream out) Writes the shortest encoding ofvalue.static voidwrite(long value, ByteBuffer buf) Writes the shortest encoding ofvalueat the buffer's position.
-
Field Details
-
MAX_VALUE
public static final long MAX_VALUEThe largest value, 2^62 - 1.- See Also:
-
MAX_LENGTH
public static final int MAX_LENGTHThe longest encoding, in bytes.- See Also:
-
-
Method Details
-
length
public static int length(long value) The length of the shortest encoding ofvalue.- Parameters:
value- the value, 0 to 2^62-1- Returns:
- 1, 2, 4 or 8
-
lengthOf
public static int lengthOf(int firstByte) The total length of an encoding, from its first byte.- Parameters:
firstByte- the first byte of the encoding- Returns:
- 1, 2, 4 or 8
-
encode
public static byte[] encode(long value) The shortest encoding ofvalue.- Parameters:
value- the value, 0 to 2^62-1- Returns:
- its encoding
-
write
public static int write(long value, byte[] dst, int offset) Writes the shortest encoding ofvalueatoffset.- Parameters:
value- the value, 0 to 2^62-1dst- where to writeoffset- where indst- Returns:
- the offset just past it
- Throws:
IndexOutOfBoundsException- if it does not fit
-
write
Writes the shortest encoding ofvalue.- Parameters:
value- the value, 0 to 2^62-1out- where to write- Throws:
IOException- if writing fails
-
write
Writes the shortest encoding ofvalueat the buffer's position.- Parameters:
value- the value, 0 to 2^62-1buf- where to write- Throws:
BufferOverflowException- if it does not fit; nothing is written then
-
read
Reads one integer.- Parameters:
in- the stream- Returns:
- the value, or -1 if the stream ended before its first byte
- Throws:
EOFException- if the stream ends inside itIOException
-
read
Reads one integer from the buffer's position.- Parameters:
buf- the buffer- Returns:
- the value, with the position moved past it; or -1, with the position unchanged, if the buffer does not hold all of it
-
read
public static long read(byte[] src, int offset, int end) Reads one integer fromsrc[offset..end).- Parameters:
src- the bytesoffset- where the integer startsend- the end of the range, exclusive- Returns:
- the value, or -1 if the range does not hold all of it;
lengthOf(int)of the first byte tells how many bytes it took
-