io::Stream class

The Stream class represents a generic stream of character data. Stream provides a high-level buffered interface for reading and writing strings, but does not implement the actual writing or reading. Stream subclasses must provide _write and/or _read methods for actually processing data.

class Stream(...)
Construct a Stream object. The arguments may include some of the options Input and Output (Input is the default if none are specified), and one of the buffering options Buffered, LineBuffered or Unbuffered (buffering is enabled by default). Finally, the arguments may include Narrow to indicate a narrow (8-bit) stream that only accepts character codes less than 256. By default, any 16-bit character codes are supported.

Stream methods

write(...)
Write the arguments to the stream. Non-string arguments will be first converted to strings using the std::Str constructor.
writeLn(...)
Write the arguments to the stream, followed by a line break. Non-string arguments will be first converted to strings using the std::Str constructor.
read([size])
Read up to a specified number of characters from the stream and return them as a string. Read less than size characters only if the end of stream was encountered. If the number of characters is not specified, read until the end of the stream. Return an empty string at the end of stream.
readLn()
Read a line from the stream, and return the line without the line ending. Raise IoError if called at the end of stream.
readLines()
Read the rest of the stream as lines, close the stream and return an array containing the lines. Line endings are not included in the lines. The stream will be closed even if there is an IO error.
flush()
Flush the output buffer of the stream by writing its contents. If the buffer is empty or does not exist, do nothing. This method can only be used on streams that support writing.
eof()
Return a boolean indicating whether there are any characters left in the stream before the end of stream. This method can only be used on streams that support reading.
close()
Close the stream by flushing the output buffers. Subclasses of Stream should also release any operating system resources that might be allocated for the object. Any stream operations other than close made to a closed stream object will fail. Calling close on a closed stream does nothing.
peek()
Return the contents of the input buffer as a string. If the input buffer is empty or does not exist, return an empty string. This method can only be used on streams that support reading.
iterator()
Return an iterator that iterates over all the lines in the stream. Line endings are not included in the lines.

Stream operations

for line in stream
The lines in a stream can be enumerated with a for loop. Line endings are not included in the lines.

Stream subclass methods

At least one of these methods must be provided by subclasses of Stream (_write for output streams and _read for input streams). Stream does not implement these and therefore is an abstract class.

_write(...)
Write the arguments directly to the stream without buffering. The arguments are strings.
_read(size)
Read up to the specified number of characters from the stream, directly without buffering. Return nil if at the end of the stream.