Class ConnectionId

java.lang.Object
io.github.mahmoudimus.quic.ConnectionId
All Implemented Interfaces:
Comparable<ConnectionId>

public final class ConnectionId extends Object implements Comparable<ConnectionId>
A QUIC connection ID (RFC 9000 section 5.1): 0 to 20 opaque bytes with value semantics.

Instances are immutable and thread-safe; the bytes are copied in and out.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final ConnectionId
    The zero-length connection ID.
    static final int
    The longest connection ID QUIC version 1 and 2 allow, in bytes.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Orders by length, then by unsigned bytes.
    boolean
     
    byte
    get(int index)
    One byte of the ID.
    int
     
    boolean
    Whether this is the zero-length connection ID.
    int
    The length in bytes.
    of(byte... bytes)
    A connection ID with a copy of some bytes.
    of(byte[] bytes, int offset, int length)
    A connection ID with a copy of part of an array.
    random(Random random, int length)
    A random connection ID.
    read(ByteBuffer in, int length)
    Reads a connection ID of known length at the buffer's position.
    byte[]
    A copy of the bytes.
    The bytes in lowercase hexadecimal.
    int
    writeTo(byte[] out, int offset)
    Writes the bytes into an array, without a length prefix.
    void
    Writes the bytes, without a length prefix.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • MAX_LENGTH

      public static final int MAX_LENGTH
      The longest connection ID QUIC version 1 and 2 allow, in bytes.
      See Also:
    • EMPTY

      public static final ConnectionId EMPTY
      The zero-length connection ID.
  • Method Details

    • of

      public static ConnectionId of(byte... bytes)
      A connection ID with a copy of some bytes.
      Parameters:
      bytes - 0 to MAX_LENGTH bytes
      Returns:
      the connection ID
      Throws:
      IllegalArgumentException - if there are more than MAX_LENGTH bytes
    • of

      public static ConnectionId of(byte[] bytes, int offset, int length)
      A connection ID with a copy of part of an array.
      Parameters:
      bytes - the array
      offset - the first byte
      length - the length, 0 to MAX_LENGTH
      Returns:
      the connection ID
      Throws:
      IllegalArgumentException - if the length is out of range
      IndexOutOfBoundsException - if the range is outside the array
    • read

      public static ConnectionId read(ByteBuffer in, int length)
      Reads a connection ID of known length at the buffer's position.
      Parameters:
      in - the buffer; its position moves past the connection ID
      length - the length, 0 to MAX_LENGTH
      Returns:
      the connection ID
      Throws:
      IllegalArgumentException - if the length is out of range
      BufferUnderflowException - if fewer than length bytes remain
    • random

      public static ConnectionId random(Random random, int length)
      A random connection ID. Use a SecureRandom for IDs sent on the wire.
      Parameters:
      random - the source of randomness
      length - the length, 0 to MAX_LENGTH
      Returns:
      the connection ID
      Throws:
      IllegalArgumentException - if the length is out of range
    • length

      public int length()
      The length in bytes.
      Returns:
      0 to MAX_LENGTH
    • isEmpty

      public boolean isEmpty()
      Whether this is the zero-length connection ID.
      Returns:
      true if the length is zero
    • get

      public byte get(int index)
      One byte of the ID.
      Parameters:
      index - the index, below length()
      Returns:
      the byte
    • toByteArray

      public byte[] toByteArray()
      A copy of the bytes.
      Returns:
      a new array
    • writeTo

      public void writeTo(ByteBuffer out)
      Writes the bytes, without a length prefix.
      Parameters:
      out - the buffer, written at its position
    • writeTo

      public int writeTo(byte[] out, int offset)
      Writes the bytes into an array, without a length prefix.
      Parameters:
      out - the array
      offset - where to write
      Returns:
      the offset just past the bytes
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(ConnectionId o)
      Orders by length, then by unsigned bytes.
      Specified by:
      compareTo in interface Comparable<ConnectionId>
    • toString

      public String toString()
      The bytes in lowercase hexadecimal.
      Overrides:
      toString in class Object