decodeFramed

fun <T> Flow<ByteArray>.decodeFramed(decoder: BleDecoder<T>, framer: Framer = LengthPrefixFramer(), onDecodeFailure: (ByteArray) -> Unit = {}): Flow<T>

Combines unframing and decoding into one operator: byte chunks in, typed values out.

Each collection allocates its own fresh Unframer via framer, so the same Framer instance is safe to share across multiple collectors and re-collecting the same returned Flow starts fresh (no leftover buffered tail from a prior collection).

A frame is sent through decoder. If the decoder throws (the BleDecoder contract for parse failure), the frame's raw bytes are routed to onDecodeFailure (default no-op) and dropped from the output stream. Pass a logger or metrics sink in production to surface corrupted payloads.

A malformed length prefix is a different class of failure: it throws FrameTooLargeException downstream and cancels the flow. Wrap with .catch { } to recover.