LengthPrefixed

annotation class LengthPrefixed(val prefix: LengthPrefix = LengthPrefix.Short)

Marks a length-prefixed field: prefix bytes carrying the value's wire size, followed by the value's bytes. Default prefix width is 2-byte big-endian (UShort).

Accepted on:

  • String fields — the value is the field's UTF-8 bytes.

  • @ProtocolMessage data class fields — the value is the message body's wire bytes; encode emits the prefix carrying the body's wireSize, decode reads the prefix, bounds inner decode, restores the outer limit.

For adjacent length carriers, prefer @LengthPrefixed over modeling the length as a separate constructor parameter and a @LengthFrom reference — a redundant length carrier is an impossible-state class (the prefix and body.wireSize() independently encode the same quantity). @LengthFrom is reserved for genuine remote-prefix uses (length carried in a non- adjacent field).

@ProtocolMessage
data class GreetingMessage(
@LengthPrefixed val name: String, // 2-byte prefix (default)
@LengthPrefixed(LengthPrefix.Byte) val nickname: String, // 1-byte prefix (max 255)
@LengthPrefixed(LengthPrefix.Int) val bio: String, // 4-byte prefix
)

@ProtocolMessage
data class WavFmtBody(val audioFormat: UShort, /* ... */)

@ProtocolMessage(wireOrder = Endianness.Little)
data class WavFmtChunk(
val fourCC: UInt,
@LengthPrefixed(LengthPrefix.Int) val body: WavFmtBody, // 4-byte LE prefix carries body.wireSize
)

Properties

Link copied to clipboard