]> gitweb.factorcode.org Git - factor.git/blob - core/io/io-docs.factor
models.search: Fix regression.
[factor.git] / core / io / io-docs.factor
1 USING: alien byte-arrays destructors help.markup help.syntax
2 kernel math quotations sequences specialized-arrays strings ;
3 IN: io
4
5 ARTICLE: "stream-types" "Binary and text streams"
6 "A word which outputs the stream element type:"
7 { $subsections stream-element-type }
8 "Stream element types:"
9 { $subsections +byte+ +character+ }
10 "The stream element type is the data type read and written by " { $link stream-read1 } " and " { $link stream-write1 } "."
11 $nl
12 "Binary streams have an element type of " { $link +byte+ } ". Elements are integers in the range " { $snippet "[0,255]" } ", representing bytes. Reading a sequence of elements produces a " { $link byte-array } ". Any object implementing the " { $link >c-ptr } " and " { $link byte-length } " generic words can be written to a binary stream."
13 $nl
14 "Character streams have an element type of " { $link +character+ } ". Elements are non-negative integers, representing Unicode code points. Only instances of the " { $link string } " class can be read or written on a character stream."
15 $nl
16 "Most external streams are binary streams, and can be wrapped in string streams once a suitable encoding has been provided; see " { $link "io.encodings" } "." ;
17
18 HELP: +byte+
19 { $description "A stream element type. See " { $link stream-element-type } " for explanation." } ;
20
21 HELP: +character+
22 { $description "A stream element type. See " { $link stream-element-type } " for explanation." } ;
23
24 HELP: stream-element-type
25 { $values { "stream" "a stream" } { "type" { $link +byte+ } " or " { $link +character+ } } }
26 { $contract "Outputs one of " { $link +byte+ } " or " { $link +character+ } "." } ;
27
28 HELP: stream-readln
29 { $values { "stream" "an input stream" } { "str/f" { $maybe string } } }
30 { $contract "Reads a line of input from the stream. Outputs " { $link f } " on stream exhaustion." }
31 { $notes "Most code only works on one stream at a time and should instead use " { $link readln } "; see " { $link "stdio" } "." }
32 $io-error ;
33
34 HELP: stream-read1
35 { $values { "stream" "an input stream" } { "elt" { $maybe "an element" } } }
36 { $contract "Reads an element from the stream. Outputs " { $link f } " on stream exhaustion." }
37 { $notes "Most code only works on one stream at a time and should instead use " { $link read1 } "; see " { $link "stdio" } "." }
38 $io-error ;
39
40 HELP: stream-read
41 { $values { "n" "a non-negative integer" } { "stream" "an input stream" } { "seq/f" { $or byte-array string f } } }
42 { $contract "Reads " { $snippet "n" } " elements from the stream. Outputs a truncated string or " { $link f } " on stream exhaustion." }
43 { $notes "Most code only works on one stream at a time and should instead use " { $link read } "; see " { $link "stdio" } "." }
44 $io-error ;
45
46 HELP: stream-read-unsafe
47 { $values { "n" "a non-negative integer" } { "buf" { $or c-ptr string } } { "stream" "an input stream" } { "count" integer } }
48 { $contract "Reads up to " { $snippet "n" } " elements from the stream. The data is stored directly into the buffer provided by " { $snippet "buf" } ", which must be a string (in the case of a character stream) or a byte array, specialized array, or other pointer to memory (in the case of a byte stream). There must be space in the buffer for at least " { $snippet "n" } " elements. Returns the number of elements read from the stream, which will be equal to " { $snippet "n" } " unless the end of the stream is reached. If the stream is exhausted, returns zero." }
49 { $warning "This word does not perform bounds checking on " { $snippet "buf" } ". Most code should use " { $link stream-read } " or " { $link stream-read-into } " instead." }
50 $io-error ;
51
52 HELP: read-into
53 { $values { "buf" { $or byte-array specialized-array string } } { "buf-slice" slice } { "more?" boolean } }
54 { $contract "Reads from the current " { $link input-stream } " into the sequence " { $snippet "buf" } ", until either the length of " { $snippet "buf" } " is reached or the stream is exhausted. Returns a " { $link slice } " over the part of " { $snippet "buf" } " that was written to, and a boolean value that will be " { $link f } " if the stream was exhausted." }
55 $io-error ;
56
57 HELP: stream-read-into
58 { $values { "buf" { $or byte-array specialized-array string } } { "stream" "an input stream" } { "buf-slice" slice } { "more?" boolean } }
59 { $contract "Reads from the stream into the sequence " { $snippet "buf" } ", until either the length of " { $snippet "buf" } " is reached or the stream is exhausted. Returns a " { $link slice } " over the part of " { $snippet "buf" } " that was written to, and a boolean value that will be " { $link f } " if the stream was exhausted." }
60 { $notes "Most code only works on one stream at a time and should instead use " { $link read-into } "; see " { $link "stdio" } "." }
61 $io-error ;
62
63 HELP: stream-read-until
64 { $values { "seps" string } { "stream" "an input stream" } { "seq" { $or byte-array string f } } { "sep/f" { $maybe "a character" } } }
65 { $contract "Reads elements from the stream, until the first occurrence of a separator character, or stream exhaustion. In the former case, the separator is pushed on the stack, and is not part of the output string. In the latter case, the entire stream contents are output, along with " { $link f } "." }
66 { $notes "Most code only works on one stream at a time and should instead use " { $link read-until } "; see " { $link "stdio" } "." }
67 $io-error ;
68
69 HELP: stream-read-partial
70 { $values
71      { "n" "a non-negative integer" } { "stream" "an input stream" }
72      { "seq/f" { $or byte-array string f } } }
73 { $description "Reads at most " { $snippet "n" } " elements from a stream and returns up to that many characters without blocking. If no characters are available, blocks until some are and returns them." } ;
74
75 HELP: stream-read-partial-unsafe
76 { $values { "n" "a non-negative integer" } { "buf" { $or c-ptr string } } { "stream" "an input stream" } { "count" integer } }
77 { $contract "Reads up to " { $snippet "n" } " elements from the stream without blocking. If no data is available immediately on the stream, blocks until data is available. The data is stored directly into the buffer provided by " { $snippet "buf" } ", which must be a string (in the case of a character stream), or a byte array, specialized array, or other pointer to memory (in the case of a byte stream). There must be space in the buffer for at least " { $snippet "n" } " elements. Returns the number of elements read from the stream, or zero if the end of the stream was reached." }
78 { $warning "This word does not perform bounds checking on " { $snippet "buf" } ". Most code should use " { $link stream-read-partial } " or " { $link stream-read-partial-into } " instead." }
79 $io-error ;
80
81 HELP: read-partial-into
82 { $values { "buf" { $or byte-array specialized-array string } } { "buf-slice" slice } { "more?" boolean } }
83 { $contract "Reads available data from the current " { $link input-stream } " into the sequence " { $snippet "buf" } " without blocking until all immediately available data is read or the length of " { $snippet "buf" } " is reached. If no data is immediately available, blocks until data is available. Returns a " { $link slice } " over the part of " { $snippet "buf" } " that was written to, and a boolean that will be " { $link f } " if the stream was exhausted." }
84 $io-error ;
85
86 HELP: stream-read-partial-into
87 { $values { "buf" { $or byte-array specialized-array string } } { "stream" "an input stream" } { "buf-slice" slice } { "more?" boolean } }
88 { $contract "Reads available data from the stream into the sequence " { $snippet "buf" } " without blocking until all immediately available data is read or the length of " { $snippet "buf" } " is reached. If no data is immediately available, blocks until data is available. Returns a " { $link slice } " over the part of " { $snippet "buf" } " that was written to, and a boolean that will be " { $link f } " if the stream was exhausted." }
89 { $notes "Most code only works on one stream at a time and should instead use " { $link read-partial-into } "; see " { $link "stdio" } "." }
90 $io-error ;
91
92 HELP: stream-write1
93 { $values { "elt" "an element" } { "stream" "an output stream" } }
94 { $contract "Writes an element to the stream. If the stream does buffering, output may not be performed immediately; use " { $link stream-flush } " to force output." }
95 { $notes "Most code only works on one stream at a time and should instead use " { $link write1 } "; see " { $link "stdio" } "." }
96 $io-error ;
97
98 HELP: stream-write
99 { $values { "data" "binary data or a string" } { "stream" "an output stream" } }
100 { $contract "Writes a piece of data to the stream. If the stream performs buffering, output may not be performed immediately; use " { $link stream-flush } " to force output." }
101 { $notes "Most code only works on one stream at a time and should instead use " { $link write } "; see " { $link "stdio" } "." }
102 $io-error ;
103
104 HELP: stream-flush
105 { $values { "stream" "an output stream" } }
106 { $contract "Waits for any pending output to complete." }
107 { $notes "With many output streams, written output is buffered and not sent to the underlying resource until either the buffer is full, or this word is called." }
108 { $notes "Most code only works on one stream at a time and should instead use " { $link flush } "; see " { $link "stdio" } "." }
109 $io-error ;
110
111 HELP: stream-nl
112 { $values { "stream" "an output stream" } }
113 { $contract "Writes a line terminator. If the stream does buffering, output may not be performed immediately; use " { $link stream-flush } " to force output." }
114 { $notes "Most code only works on one stream at a time and should instead use " { $link nl } "; see " { $link "stdio" } "." }
115 $io-error ;
116
117 HELP: stream-print
118 { $values { "str" string } { "stream" "an output stream" } }
119 { $description "Writes a newline-terminated string." }
120 { $notes "Most code only works on one stream at a time and should instead use " { $link print } "; see " { $link "stdio" } "." }
121 $io-error ;
122
123 HELP: stream-copy*
124 { $values { "in" "an input stream" } { "out" "an output stream" } }
125 { $description "Copies the contents of one stream into another. The streams are left open when the copy is completed. The " { $link stream-copy } " word can be used instead to close them on completion, if desired." }
126 $io-error ;
127
128 HELP: stream-copy
129 { $values { "in" "an input stream" } { "out" "an output stream" } }
130 { $description "Copies the contents of one stream into another, closing both streams when done. To copy without closing the streams, use " { $link stream-copy* } "." }
131 $io-error ;
132
133 HELP: stream-tell
134 { $values
135      { "stream" "a stream" } { "n" integer }
136 }
137 { $description "Returns the index of the stream pointer if the stream is seekable." }
138 { $notes "Stream seeking is not supported on streams that do not have a known length, e.g. TCP/IP streams." } ;
139
140 HELP: stream-seek
141 { $values
142      { "n" integer } { "seek-type" "a seek singleton" } { "stream" "a stream" }
143 }
144 { $description "Moves the pointer associated with a stream's handle to an offset " { $snippet "n" } " bytes from the seek type so that further reading or writing happens at the new location. For output streams, the buffer is flushed before seeking. Seeking past the end of an output stream will pad the difference with zeros once the stream is written to again." $nl
145     "Three methods of seeking are supported:"
146     { $list { $link seek-absolute } { $link seek-relative } { $link seek-end } }
147 }
148 { $notes "Stream seeking is not supported on streams that do not have a known length, e.g. TCP/IP streams." } ;
149
150 HELP: stream-seekable?
151 { $values
152      { "stream" "a stream" } { "?" boolean }
153 }
154 { $description "Returns true if " { $snippet "stream" } " is a seekable stream." }
155 { $notes "Stream seeking is not supported on streams that do not have a known length, e.g. TCP/IP streams." } ;
156
157 HELP: stream-length
158 { $values
159      { "stream" "a stream" } { "n/f" { $maybe integer } }
160 }
161 { $description "Returns the length of the data supplied by " { $snippet "stream" } ", or " { $link f } " if the stream is not seekable or has unknown length." }
162 { $notes "Stream seeking is not supported on streams that do not have a known length, e.g. TCP/IP streams." } ;
163
164 HELP: seek-absolute
165 { $values
166
167      { "value" "a seek singleton" }
168 }
169 { $description "Seeks to an offset from the beginning of the stream." } ;
170
171 HELP: seek-end
172 { $values
173
174      { "value" "a seek singleton" }
175 }
176 { $description "Seeks to an offset from the end of the stream. If the offset puts the stream pointer past the end of the data on an output stream, writing to it will pad the difference with zeros." } ;
177
178 HELP: seek-relative
179 { $values
180
181      { "value" "a seek singleton" }
182 }
183 { $description "Seeks to an offset from the current position of the stream pointer." } ;
184
185 { stream-seek stream-tell stream-seekable? stream-length } related-words
186 { seek-absolute seek-relative seek-end } related-words
187
188 HELP: seek-input
189 { $values
190      { "n" integer } { "seek-type" "a seek singleton" }
191 }
192 { $description "Calls " { $link stream-seek } " on the stream stored in " { $link input-stream } "." } ;
193
194 HELP: seek-output
195 { $values
196      { "n" integer } { "seek-type" "a seek singleton" }
197 }
198 { $description "Calls " { $link stream-seek } " on the stream stored in " { $link output-stream } "." } ;
199
200 HELP: input-stream
201 { $var-description "Holds an input stream for various implicit stream operations. Rebound using " { $link with-input-stream } " and " { $link with-input-stream* } "." } ;
202
203 HELP: output-stream
204 { $var-description "Holds an output stream for various implicit stream operations. Rebound using " { $link with-output-stream } " and " { $link with-output-stream* } "." } ;
205
206 HELP: error-stream
207 { $var-description "Holds an error stream." } ;
208
209 HELP: readln
210 { $values { "str/f" { $maybe string } } }
211 { $description "Reads a line of input from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
212 $io-error ;
213
214 HELP: read1
215 { $values { "elt" { $maybe "an element" } } }
216 { $description "Reads an element from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
217 $io-error ;
218
219 HELP: read
220 { $values { "n" "a non-negative integer" } { "seq" { $or byte-array string f } } }
221 { $description "Reads " { $snippet "n" } " elements from " { $link input-stream } ". If there is no input available, outputs " { $link f } ". If there are less than " { $snippet "n" } " elements available, outputs a sequence shorter than " { $snippet "n" } " in length." }
222 $io-error ;
223
224 HELP: read-until
225 { $values { "seps" string } { "seq" { $or byte-array string f } } { "sep/f" { $maybe "a character" } } }
226 { $contract "Reads elements from " { $link input-stream } " until the first occurrence of a separator, or stream exhaustion. In the former case, the separator character is pushed on the stack, and is not part of the output. In the latter case, the entire stream contents are output, along with " { $link f } "." }
227 $io-error ;
228
229 HELP: read-partial
230 { $values { "n" integer } { "seq" { $or byte-array string f } } }
231 { $description "Reads at most " { $snippet "n" } " elements from " { $link input-stream } " and returns them in a sequence. This word should be used instead of " { $link read } " when processing the entire element a chunk at a time, since on some stream implementations it may be slightly faster." } ;
232
233 HELP: write1
234 { $values { "elt" "an element" } }
235 { $contract "Writes an element to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
236 $io-error ;
237
238 HELP: write
239 { $values { "seq" { $or byte-array string f } } }
240 { $description "Writes a sequence of elements to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
241 $io-error ;
242
243 HELP: flush
244 { $description "Waits for any pending output on " { $link output-stream } " to complete." }
245 $io-error ;
246
247 HELP: nl
248 { $description "Writes a line terminator to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
249 $io-error ;
250
251 HELP: print
252 { $values { "str" string } }
253 { $description "Writes a newline-terminated string to " { $link output-stream } "." }
254 $io-error ;
255
256 HELP: with-input-stream
257 { $values { "stream" "an input stream" } { "quot" quotation } }
258 { $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "stream" } ". The stream is closed if the quotation returns or throws an error." } ;
259
260 HELP: with-output-stream
261 { $values { "stream" "an output stream" } { "quot" quotation } }
262 { $description "Calls the quotation in a new dynamic scope, with " { $link output-stream } " rebound to " { $snippet "stream" } ". The stream is closed if the quotation returns or throws an error." } ;
263
264 HELP: with-streams
265 { $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
266 { $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "input" } " and " { $link output-stream } " rebound to " { $snippet "output" } ". The streams are closed if the quotation returns or throws an error." } ;
267
268 HELP: with-streams*
269 { $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
270 { $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "input" } " and " { $link output-stream } " rebound to " { $snippet "output" } "." }
271 { $notes "This word does not close the streams. Compare with " { $link with-streams } "." } ;
272
273 { with-input-stream with-input-stream* } related-words
274
275 { with-output-stream with-output-stream* } related-words
276
277 HELP: with-input-stream*
278 { $values { "stream" "an input stream" } { "quot" quotation } }
279 { $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "stream" } "." }
280 { $notes "This word does not close the stream. Compare with " { $link with-input-stream } "." } ;
281
282 HELP: with-output-stream*
283 { $values { "stream" "an output stream" } { "quot" quotation } }
284 { $description "Calls the quotation in a new dynamic scope, with " { $link output-stream } " rebound to " { $snippet "stream" } "." }
285 { $examples
286   { $unchecked-example
287     "USING: destructors io io.encodings.utf8 io.files prettyprint ;"
288     "\"/tmp/test.txt\" utf8 <file-writer> dup [ \"Hello!\" write ] with-output-stream* dispose"
289     "\"/tmp/test.txt\" utf8 file-contents ."
290     "\"Hello!\""
291   }
292 }
293 { $notes "This word does not close the stream. Compare with " { $link with-output-stream } "." } ;
294
295 HELP: bl
296 { $description "Outputs a space character (" { $snippet "\" \"" } ") to " { $link output-stream } "." }
297 $io-error ;
298
299 HELP: stream-lines
300 { $values { "stream" "an input stream" } { "seq" { $sequence string } } }
301 { $description "Reads lines of text until the stream is exhausted, collecting them in a sequence of strings." } ;
302
303 HELP: read-lines
304 { $values { "seq" { $sequence string } } }
305 { $description "Reads lines of text until from the " { $link input-stream } " until it is exhausted, collecting them in a sequence of strings." } ;
306
307 HELP: each-line
308 { $values { "quot" { $quotation ( ... line -- ... ) } } }
309 { $description "Calls the quotation with successive lines of text, until the current " { $link input-stream } " is exhausted." } ;
310
311 HELP: each-block
312 { $values { "quot" { $quotation ( ... block -- ... ) } } }
313 { $description "Calls the quotation with successive blocks of data, until the current " { $link input-stream } " is exhausted." } ;
314
315 HELP: stream-contents
316 { $values { "stream" "an input stream" } { "seq" { $or string byte-array } } }
317 { $description "Reads all elements in the given stream until the stream is exhausted. The type of the sequence depends on the stream's element type. The stream is closed after completion." }
318 $io-error ;
319
320 HELP: read-contents
321 { $values { "seq" { $or string byte-array } } }
322 { $description "Reads all elements in the " { $link input-stream } " until the stream is exhausted. The type of the sequence depends on the stream's element type." }
323 $io-error ;
324
325 HELP: tell-input
326 { $values
327         { "n" integer }
328 }
329 { $description "Returns the index of the stream stored in " { $link input-stream } "." } ;
330
331 HELP: tell-output
332 { $values
333         { "n" integer }
334 }
335 { $description "Returns the index of the stream stored in " { $link output-stream } "." } ;
336
337 ARTICLE: "stream-protocol" "Stream protocol"
338 "The stream protocol consists of a large number of generic words, many of which are optional."
339 $nl
340 "Stream protocol words are rarely called directly, since code which only works with one stream at a time should be written to use " { $link "stdio" } " instead, wrapping I/O operations such as " { $link read } " and " { $link write } " in " { $link with-input-stream } " and " { $link with-output-stream } "."
341 $nl
342 "All streams must implement the " { $link dispose } " word in addition to the stream protocol."
343 { $subsections "stream-types" }
344 "These words are required for binary and string input streams:"
345 { $subsections
346     stream-read1
347     stream-read-unsafe
348     stream-read-until
349     stream-read-partial-unsafe
350 }
351 "The " { $link stream-read-unsafe } " and " { $link 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:"
352 { $subsections
353     stream-read
354     stream-read-into
355     stream-read-partial
356     stream-read-partial-into
357 }
358 "This word is only required for string input streams:"
359 { $subsections stream-readln }
360 "These words are required for binary and string output streams:"
361 { $subsections
362     stream-flush
363     stream-write1
364     stream-write
365 }
366 "This word is only required for string output streams:"
367 { $subsections stream-nl }
368 "These words are for seekable streams:"
369 { $subsections
370     stream-seekable?
371     stream-tell
372     stream-seek
373     tell-input
374     tell-output
375     stream-length
376 }
377 { $see-also "io.timeouts" } ;
378
379 ARTICLE: "stdio-motivation" "Motivation for default streams"
380 "Most I/O code only operates on one stream at a time. The " { $link input-stream } " and " { $link output-stream } " variables are implicit parameters used by many I/O words. Using this idiom improves code in three ways:"
381 { $list
382     { "Code becomes simpler because there is no need to keep a stream around on the stack." }
383     { "Code becomes more robust because " { $link with-input-stream } " and " { $link with-output-stream } " automatically close the streams if there is an error." }
384     { "Code becomes more reusable because it can be written to not worry about which stream is being used, and instead the caller can use " { $link with-input-stream } " or " { $link with-output-stream } " to specify the source or destination for I/O operations." }
385 }
386 "For example, here is a program which reads the first line of a file, converts it to an integer, then reads that many characters, and splits them into groups of 16:"
387 { $code
388     "USING: continuations kernel io io.files math.parser splitting ;"
389     "\"data.txt\" utf8 <file-reader>"
390     "dup stream-readln string>number over stream-read 16 group"
391     "swap dispose"
392 }
393 "This code has two problems: it has some unnecessary stack shuffling, and if either " { $link stream-readln } " or " { $link stream-read } " throws an I/O error, the stream is not closed because " { $link dispose } " is never reached. So we can add a call to " { $link with-disposal } " to ensure the stream is always closed:"
394 { $code
395     "USING: continuations kernel io io.files math.parser splitting ;"
396     "\"data.txt\" utf8 <file-reader> ["
397     "    dup stream-readln string>number over stream-read"
398     "    16 group"
399     "] with-disposal"
400 }
401 "This code is robust, however it is more complex than it needs to be. This is where the default stream words come in; using them, the above can be rewritten as follows:"
402 { $code
403     "USING: continuations kernel io io.files math.parser splitting ;"
404     "\"data.txt\" utf8 <file-reader> ["
405     "    readln string>number read 16 group"
406     "] with-input-stream"
407 }
408 "An even better implementation that takes advantage of a utility word:"
409 { $code
410     "USING: continuations kernel io io.files math.parser splitting ;"
411     "\"data.txt\" utf8 ["
412     "    readln string>number read 16 group"
413     "] with-file-reader"
414 } ;
415
416 ARTICLE: "stdio" "Default input and output streams"
417 { $subsections "stdio-motivation" }
418 "The default input stream is stored in a dynamically-scoped variable:"
419 { $subsections input-stream }
420 "Unless rebound in a child namespace, this variable will be set to a console stream for reading input from the user."
421 $nl
422 "Words reading from the default input stream:"
423 { $subsections
424     read1
425     read
426     read-into
427     read-until
428     read-partial
429     read-partial-into
430     readln
431 }
432 "If the default input stream is a character stream (" { $link stream-element-type } " outputs " { $link +character+ } "), lines of text can be read:"
433 { $subsections readln }
434 "Seeking on the default input stream:"
435 { $subsections seek-input }
436 "A pair of combinators for rebinding the " { $link input-stream } " variable:"
437 { $subsections
438     with-input-stream
439     with-input-stream*
440 }
441 "The default output stream is stored in a dynamically-scoped variable:"
442 { $subsections output-stream }
443 "Unless rebound in a child namespace, this variable will be set to a console stream for showing output to the user."
444 $nl
445 "Words writing to the default output stream:"
446 { $subsections
447     flush
448     write1
449     write
450 }
451 "If the default output stream is a character stream (" { $link stream-element-type } " outputs " { $link +character+ } "), lines of text can be written:"
452 { $subsections
453     print
454     nl
455     bl
456 }
457 "Seeking on the default output stream:"
458 { $subsections seek-output }
459 "A pair of combinators for rebinding the " { $link output-stream } " variable:"
460 { $subsections
461     with-output-stream
462     with-output-stream*
463 }
464 "A pair of combinators for rebinding both default streams at once:"
465 { $subsections
466     with-streams
467     with-streams*
468 } ;
469
470 ARTICLE: "stream-utils" "Stream utilities"
471 "There are a few useful stream-related words which are not generic, but merely built up from the stream protocol."
472 $nl
473 "First, a simple composition of " { $link stream-write } " and " { $link stream-nl } ":"
474 { $subsections stream-print }
475 "Processing lines one by one:"
476 { $subsections
477     stream-lines
478     read-lines
479     each-line
480 }
481 "Processing blocks of data:"
482 { $subsections
483     stream-contents
484     read-contents
485     each-block
486 }
487 "Copying the contents of one stream to another:"
488 { $subsections
489     stream-copy*
490     stream-copy
491 } ;
492
493 ARTICLE: "stream-examples" "Stream example"
494 "Ask the user for their age, and print it back:"
495 { $code
496     "USING: io math.parser ;"
497     ""
498     ": ask-age ( -- ) \"How old are you?\" print ;"
499     ""
500     ": read-age ( -- n ) readln string>number ;"
501     ""
502     ": print-age ( n -- )"
503     "    \"You are \" write"
504     "    number>string write"
505     "    \" years old.\" print ;"
506     ": example ( -- ) ask-age read-age print-age ;"
507     ""
508     "example"
509 } ;
510
511 ARTICLE: "streams" "Streams"
512 "Input and output centers on the concept of a " { $emphasis "stream" } ", which is a source or sink of " { $emphasis "elements" } "."
513 { $subsections "stream-examples" }
514 "A stream can either be passed around on the stack or bound to a dynamic variable and used as one of the two implicit " { $emphasis "default streams" } "."
515 { $subsections
516     "stream-protocol"
517     "stdio"
518     "stream-utils"
519 }
520 { $see-also "io.streams.string" "io.streams.plain" "io.streams.duplex" } ;
521
522 ABOUT: "streams"