Class QuicVarInt

java.lang.Object
io.github.mahmoudimus.http3.QuicVarInt

public final class QuicVarInt extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The longest encoding, in bytes.
    static final long
    The largest value, 2^62 - 1.
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    encode(long value)
    The shortest encoding of value.
    static int
    length(long value)
    The length of the shortest encoding of value.
    static int
    lengthOf(int firstByte)
    The total length of an encoding, from its first byte.
    static long
    read(byte[] src, int offset, int end)
    Reads one integer from src[offset..end).
    static long
    Reads one integer.
    static long
    Reads one integer from the buffer's position.
    static int
    write(long value, byte[] dst, int offset)
    Writes the shortest encoding of value at offset.
    static void
    write(long value, OutputStream out)
    Writes the shortest encoding of value.
    static void
    write(long value, ByteBuffer buf)
    Writes the shortest encoding of value at the buffer's position.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_VALUE

      public static final long MAX_VALUE
      The largest value, 2^62 - 1.
      See Also:
    • MAX_LENGTH

      public static final int MAX_LENGTH
      The longest encoding, in bytes.
      See Also:
  • Method Details

    • length

      public static int length(long value)
      The length of the shortest encoding of value.
      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 of value.
      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 of value at offset.
      Parameters:
      value - the value, 0 to 2^62-1
      dst - where to write
      offset - where in dst
      Returns:
      the offset just past it
      Throws:
      IndexOutOfBoundsException - if it does not fit
    • write

      public static void write(long value, OutputStream out) throws IOException
      Writes the shortest encoding of value.
      Parameters:
      value - the value, 0 to 2^62-1
      out - where to write
      Throws:
      IOException - if writing fails
    • write

      public static void write(long value, ByteBuffer buf)
      Writes the shortest encoding of value at the buffer's position.
      Parameters:
      value - the value, 0 to 2^62-1
      buf - where to write
      Throws:
      BufferOverflowException - if it does not fit; nothing is written then
    • read

      public static long read(InputStream in) throws IOException
      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 it
      IOException
    • read

      public static long read(ByteBuffer buf)
      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 from src[offset..end).
      Parameters:
      src - the bytes
      offset - where the integer starts
      end - 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