"Input and output centers on the concept of a " { $emphasis "stream" } ", which is a source or sink of characters. Streams also support formatted output, which may be used to present styled text in a manner independent of output medium."
"If your stream supports the first two but not the last one, wrap it in a " { $link <line-reader> } " to get a default implementation."
$terpri
"Seven words are required for output streams:"
{ $subsection stream-flush }
{ $subsection stream-write1 }
{ $subsection stream-write }
{ $subsection stream-terpri }
{ $subsection stream-format }
{ $subsection with-nested-stream }
"If your stream supports the first three but not the rest, wrap it in a " { $link <plain-writer> } ", which provides plain text implementations of the stream formatting words (the so called " { $emphasis "extended stream output protocol" } ")." ;
ARTICLE: "stream-utils" "Stream utilities"
"There are a few useful stream-related words which are not generic, but merely built up from the stream protocol."
$terpri
"First, a simple composition of " { $link stream-write } " and " { $link stream-terpri } ":"
{ $subsection stream-print }
"Next up, a pair of words for reading the entire contents of a stream as an array of lines, or a single string:"
{ $subsection lines }
{ $subsection contents }
"Finally, a word to copy the contents of one stream to another:"
"The " { $link stream-format } ", " { $link with-nested-stream } " and " { $link with-stream-table } " words take a hashtable of style attributes. Output stream implementations are free to ignore style information."
"The " { $link presented } " and " { $link outline } " styles can be used to build sophisticated user interfaces in the Factor UI. Instead of setting them directly, two utility words should be used:"
ARTICLE: "stream-binary" "Working with binary data"
"The core stream words read and write strings. Packed binary integers can be read and written by converting to and from sequences of bytes. Floating point numbers can be read and written by converting them into a their bitwise integer representation (" { $link "floats" } ")."
$terpri
"There are two ways to order the bytes making up an integer; " { $emphasis "little endian" } " byte order outputs the least significant byte first, and the most significant byte last, whereas " { $emphasis "big endian" } " is the other way around."
$terpri
"Consider the hexadecimal integer "{ $snippet "HEX: cafebabe" } ". Big endian byte order yields the following sequence of bytes:"
{ $code
"Byte: 1 2 3 4"
"Value: be ba fe ca"
}
"Compare this with little endian byte order:"
{ $code
"Byte: 1 2 3 4"
"Value: ca fe ba be"
}
"Two words convert a sequence of bytes into an integer:"
{ $subsection be> }
{ $subsection le> }
"Two words convert an integer into a sequence of bytes:"
{ $subsection >be }
{ $subsection >le } ;
ARTICLE: "file-streams" "Reading and writing files"