Class PacketNumber
java.lang.Object
io.github.mahmoudimus.quic.packet.PacketNumber
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic longdecode(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 intencodedLength(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 longread(ByteBuffer in, int length) Reads a truncated packet number.static longtruncate(long fullPacketNumber, int length) The low bytes of a packet number.static voidwrite(ByteBuffer out, long fullPacketNumber, int length) Writes a truncated packet number in big-endian order.
-
Field Details
-
MAX
public static final long MAXThe largest packet number, 262-1.- See Also:
-
-
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 sendlargestAcked- 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 numberlength- 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 nonetruncated- the value of the Packet Number fieldlength- the field's length in bytes, 1 to 4- Returns:
- the full packet number
-
write
Writes a truncated packet number in big-endian order.- Parameters:
out- the buffer, written at its positionfullPacketNumber- the full packet number; only its lowlengthbytes are writtenlength- the encoded length, 1 to 4
-
read
Reads a truncated packet number.- Parameters:
in- the buffer; its position moves past the fieldlength- the encoded length, 1 to 4- Returns:
- the truncated value
- Throws:
BufferUnderflowException- if fewer thanlengthbytes remain
-