ChunkCodec

object ChunkCodec

Splits an arbitrarily-sized payload into 1..N BYTES chunks and reassembles them.

Wire header (8 bytes, big-endian)

[msgId: Int (4 bytes)] [chunkIndex: UShort (2 bytes)] [chunkCount: UShort (2 bytes)] [payload bytes...]

A ≤MAX_CHUNK_PAYLOAD-byte message is a single chunk (chunkIndex=0, chunkCount=1). An empty payload is encoded as one chunk with zero payload bytes.

Thread-safety: encode and decodeChunk are stateless and safe to call concurrently. Reassembler is NOT thread-safe — callers must synchronise (or use one instance per coroutine / per-endpoint).

Types

Link copied to clipboard

Per-endpoint reassembler. Feed decoded chunks in arrival order; feed returns the complete payload once all chunks for a message have arrived.

Properties

Link copied to clipboard
const val HEADER_SIZE: Int = 8

Byte size of the fixed chunk header.

Link copied to clipboard

Default maximum bytes of message payload per chunk (header excluded). Approximates ConnectionsClient.MAX_BYTES_DATA_SIZE (≈32 768) minus the header. The real cap is injected by the Android binding at construction time.

Functions

Link copied to clipboard

Decode the header fields and payload slice from a raw received chunk. Returns null if bytes is shorter than HEADER_SIZE or the header is internally inconsistent (e.g. chunkIndex ≥ chunkCount, chunkCount = 0).

Link copied to clipboard
fun encode(payload: ByteArray, msgId: Int, maxChunkPayload: Int = MAX_CHUNK_PAYLOAD): List<ByteArray>

Encode payload into one or more chunks.