Regular Files and Pipe Streams

A regular file usually supports retained bytes and positioning. A pipe normally transports bytes in order between current endpoints.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a reread
  2. Adapt when applicable
  3. Contrast seeking and stream exhaustion
  4. Build an interface around supported capabilities

Trace a reread

A file reader can seek back to a header and reread it. A reader consuming a pipe cannot generally return to earlier bytes once consumed. Seeking explains why some parser interfaces need seekable input.

Adapt when applicable

If an algorithm must see a prefix twice, retain just the necessary prefix or use a deliberate temporary copy. Reading fixed-size blocks supports an incremental reader that does not expect every byte at once.[1]

Contrast seeking and stream exhaustion

An ordinary completed file often permits seeking to a prior offset. A pipe provides a progressing stream and cannot generally seek. A pipe reader may wait because writers still hold the write end open even when no current bytes are available. See detecting end of file.

File: read A, seek back, read A again
Pipe: read A, then wait or finish according to writer and API state
EOF after all current writers close and pending data is consumed under the usual model

Build an interface around supported capabilities

If a parser must re-read a header, save it or require a seekable source explicitly. Test one short read on a pipe and a final close. Do not infer seekability or length just from readable access.

References

  1. Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces ↩