UnsignedVarIntCodec

Unsigned LEB128 variable-length integer codec for UInt — the classic 7-bits-per-byte, high-bit-as-continuation encoding (protobuf varint, DWARF LEB128). A self-delimiting VariableLengthCodec, so it reports Exact(encodedLength) at runtime (no BackPatch) and frames a stream via peekValue.

value range          bytes
0 .. 2^7-1 (127) 1
.. 2^14-1 2
.. 2^21-1 3
.. 2^28-1 4
.. 2^32-1 5 (the 5th byte carries only the top 4 bits)

Each byte stores 7 value bits (little-endian group order); the high bit (0x80) is set on every byte except the last. Encoding is minimal. Decoding rejects an over-long sequence (a value that cannot fit in UInt, or a 6th continuation byte) with DecodeException so a truncated/malicious stream can never loop or overflow.

This is the library's first shipped self-delimiting integer encoding — used by the generated enum-discriminator codec (a @ProtocolMessage enum field's ordinal rides as an UnsignedVarIntCodec value), and available to consumers via @UseCodec(UnsignedVarIntCodec::class).

Properties

Link copied to clipboard
const val MAX_BYTES: Int = 5

Maximum encoded length of a 32-bit value: ceil(32 / 7) = 5 bytes.

Functions

Link copied to clipboard
open override fun decode(buffer: ReadBuffer, context: DecodeContext): UInt
Link copied to clipboard
open override fun encode(buffer: WriteBuffer, value: UInt, context: EncodeContext)
Link copied to clipboard
open override fun encodedLength(value: UInt): Int

Bytes value encodes to — always known from the value alone.

Link copied to clipboard
open override fun peekFrameSize(stream: StreamProcessor, baseOffset: Int = 0): PeekResult
Link copied to clipboard
open override fun peekValue(stream: StreamProcessor, baseOffset: Int = 0): VarLenPeek<UInt>

Decode the value and its byte length from a stream prefix beginning at baseOffset, without consuming any bytes. Returns VarLenPeek.NeedsMoreData when the prefix is too short to determine either.

Link copied to clipboard
open override fun wireSize(value: UInt, context: EncodeContext): WireSize

Reports the on-wire byte size. Defaults to WireSize.BackPatch.