Receiving a Stream Record Across Several Reads
A stream reader may receive a partial header, a partial payload, or bytes from several logical records in one read.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace two arrivals
A two-byte header arrives as one byte then another. The completed header announces three payload bytes. Read until those bytes arrive or exhaustion is reported. Length-prefixed records define this boundary.
Preserve trailing bytes
A read may supply the end of the first payload and the start of a second header. Keep unread bytes for the next parse step. See incremental application framing.[1][2]
Preserve both incomplete data and extra data already received
A stream parser expects a two-byte length and then that many bytes. If one read returns the first header byte only, hold it. A later read can complete the header and include bytes from the next record as well. See reading fixed-size blocks.
Stream frame example: header 00 03, payload A B C
Read one: 00
Read two: 03 A B
Read three: C followed by another header
Emit first record and retain the next header
Treat connection close according to framing state
End of stream between frames can be a clean stop. Close in the middle of a declared payload means truncation. Test both and bound the announced length before allocating storage.