Stream protocol
Factor handbook » Input and output » Streams

Prev:Stream example
Next:Default input and output streams


The stream protocol consists of a large number of generic words, many of which are optional.

Stream protocol words are rarely called directly, since code which only works with one stream at a time should be written to use Default input and output streams instead, wrapping I/O operations such as read and write in with-input-stream and with-output-stream.

All streams must implement the dispose word in addition to the stream protocol.
Binary and text streams

These words are required for binary and string input streams:
stream-read1 ( stream -- elt )

stream-read-unsafe ( n buf stream -- count )

stream-read-until ( seps stream -- seq sep/f )

stream-read-partial-unsafe ( n buf stream -- count )


The stream-read-unsafe and stream-read-partial-unsafe words should be implemented by streams but not used by client code. The following safe words are provided for reading from input streams:
stream-read ( n stream -- seq/f )

stream-read-into ( buf stream -- buf-slice more? )

stream-read-partial ( n stream -- seq/f )

stream-read-partial-into ( buf stream -- buf-slice more? )


This word is only required for string input streams:
stream-readln ( stream -- str/f )


These words are required for binary and string output streams:
stream-flush ( stream -- )

stream-write1 ( elt stream -- )

stream-write ( data stream -- )


This word is only required for string output streams:
stream-nl ( stream -- )


These words are for seekable streams:
stream-seekable? ( stream -- ? )

stream-tell ( stream -- n )

stream-seek ( n seek-type stream -- )

tell-input ( -- n )

tell-output ( -- n )

stream-length ( stream -- n/f )


See also
I/O timeout protocol