Class PacketNumber

java.lang.Object
io.github.mahmoudimus.quic.packet.PacketNumber

public final class PacketNumber extends Object
Packet number truncation and recovery (RFC 9000 section 17.1 and appendix A). A packet carries the 1 to 4 least significant bytes of its 62-bit packet number; the receiver recovers the rest from the largest packet number it has processed in the same space.

All methods are static and thread-safe.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    The largest packet number, 262-1.
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    decode(long largestPacketNumber, long truncated, int length)
    Recovers a full packet number from its truncated value (RFC 9000 appendix A.3): the value closest to one more than the largest packet number processed so far.
    static int
    encodedLength(long fullPacketNumber, long largestAcked)
    The number of bytes needed to send a packet number so that the peer recovers it: enough to represent twice the distance from the largest acknowledged packet (RFC 9000 appendix A.2).
    static long
    read(ByteBuffer in, int length)
    Reads a truncated packet number.
    static long
    truncate(long fullPacketNumber, int length)
    The low bytes of a packet number.
    static void
    write(ByteBuffer out, long fullPacketNumber, int length)
    Writes a truncated packet number in big-endian order.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • encodedLength

      public static int encodedLength(long fullPacketNumber, long largestAcked)
      The number of bytes needed to send a packet number so that the peer recovers it: enough to represent twice the distance from the largest acknowledged packet (RFC 9000 appendix A.2).
      Parameters:
      fullPacketNumber - the packet number to send
      largestAcked - the largest packet number the peer has acknowledged in this space, or -1 if none
      Returns:
      1 to 4
      Throws:
      IllegalArgumentException - if the distance needs more than four bytes
    • truncate

      public static long truncate(long fullPacketNumber, int length)
      The low bytes of a packet number.
      Parameters:
      fullPacketNumber - the full packet number
      length - the encoded length, 1 to 4
      Returns:
      the truncated value
    • decode

      public static long decode(long largestPacketNumber, long truncated, int length)
      Recovers a full packet number from its truncated value (RFC 9000 appendix A.3): the value closest to one more than the largest packet number processed so far.
      Parameters:
      largestPacketNumber - the largest packet number successfully processed in this space, or -1 if none
      truncated - the value of the Packet Number field
      length - the field's length in bytes, 1 to 4
      Returns:
      the full packet number
    • write

      public static void write(ByteBuffer out, long fullPacketNumber, int length)
      Writes a truncated packet number in big-endian order.
      Parameters:
      out - the buffer, written at its position
      fullPacketNumber - the full packet number; only its low length bytes are written
      length - the encoded length, 1 to 4
    • read

      public static long read(ByteBuffer in, int length)
      Reads a truncated packet number.
      Parameters:
      in - the buffer; its position moves past the field
      length - the encoded length, 1 to 4
      Returns:
      the truncated value
      Throws:
      BufferUnderflowException - if fewer than length bytes remain