std.io
Input and output: import std.io { println }. The module holds one error type (IoError with
its IoErrorKind); the interfaces Reader, Writer and Seek with their defaults; copy;
two streams over memory (ByteReader, ByteBuffer); the buffers BufReader and BufWriter
and the text streams TextReader and TextWriter; the console's output — print, println,
eprint, eprintln, flush — and its streams stdin(), stdout(), stderr(); and the I/O
pool that std.fs's calls run on. A reader's and a writer's call may throw Cancelled where it
waits without bound.
Examples
import std.io { println, print, ByteBuffer, IoError };
fn main(): void throws IoError {
print("one, ");
println("two");
let buffer = ByteBuffer.new();
try buffer.write("written to memory".bytes());
println(buffer.length());
}
one, two
17
Types
stdin() reads through its buffer.seek moves it and returns where it then stands, counted from the start.Writer that appends, a Reader that takes from the front what was written and not yet read.Reader with a buffer of its own: it reads its inner reader 8 KiB at a time, so that small reads do not each reach it.Writer with a buffer of its own: what it is handed waits there until 8 KiB are together, a flush() or a close().Functions
pub fn print<T :: [Display]>(v: T): void
Writes v's text on the standard output.
pub fn println<T :: [Display]>(v: T): void
Writes v's text and a newline on the standard output.
pub fn eprint<T :: [Display]>(v: T): void
Writes v's text on the standard error.
pub fn eprintln<T :: [Display]>(v: T): void
Writes v's text and a newline on the standard error.
pub fn flush(): void
Writes out what the console holds now: the standard output's, then the standard error's — for before a prompt waits for its answer, or where the program's next lines take long.
pub fn stdin(): Stdin
Returns the program's standard input: one for the program, made at the first call. Its reads
go through a buffer of its own, so readLine, lines and read take turns on it without a
byte lost — on the I/O pool where a scheduler runs.
pub fn stdout(): ConsoleWriter
Returns the standard output as a Writer: the buffer print writes into, so the two keep their
order.
pub fn stderr(): ConsoleWriter
Returns the standard error as a Writer: the buffer eprint writes into.
pub fn copy<R :: [Reader], W :: [Writer]>(r: R, w: W): int throws [IoError, Cancelled]
Copies everything r holds into w, through a buffer of its own, and returns how many bytes
it copied. The two are read and written as the values handed in — the handles and the
streams here are classes, so that is the object itself; a reader that is a struct is read
through a copy, and its position does not move for the caller.
Errors
Throws what r's read and w's write throw.
Extensions
extend<R :: [Reader]> R
pub mut fn readExact(into: Slice<uint8>): void throws [IoError, Cancelled]
Fills into whole, reading as often as it takes.
Errors
Throws UnexpectedEof where the source ends before, the bytes that came in the detail,
and what the reader's read throws.
pub mut fn readToEnd(): uint8[] throws [IoError, Cancelled]
Reads everything to the end and returns it in one new array.
Errors
Throws what the reader's read throws.
pub mut fn readToString(): string throws [IoError, Cancelled]
Reads everything to the end and returns it as UTF-8 text.
Errors
Throws InvalidData for bytes that are no UTF-8, with the decoder's error as the cause,
and what the reader's read throws.
extend<W :: [Writer]> W
pub mut fn writeAll(from: Slice<uint8>): void throws [IoError, Cancelled]
Writes all of from, writing as often as it takes.
Errors
Throws Other { code = 0 } where the writer takes no byte of a slice that has some (it
would be asked forever), and what the writer's write throws.
pub mut fn writeString(s: StringView): void throws [IoError, Cancelled]
Writes all the bytes of s (its UTF-8).
Errors
Throws what writeAll throws.