System.IO

lib/System/IO.gob

Streams of bytes, and the standard streams.

Streams. A source of bytes is a Read and a sink a Write, each with its own error type, and bytes move between them through a Buffer, a window over a byte array that a read appends to and a write empties from. Buffered adds a buffer to any source or sink, and with it the line operations. System.File gives files their instances, and opens them.

The standard streams.

Descriptors the program inherited keep their blocking mode: changing it would change it for every other process sharing them.

Written in Turkey over Turkey.IO's system calls, with no runtime support of its own. The Prelude does not import this module, so it may use the Prelude, and only the programs that import it compile it.

type Line e = Got | Partial | End | Failed(e)

What readLineInto did. One word, with no allocation, when e is held at a pointer, as IOError and DecodeError are: it is answered once per line, and an Either with a value in each arm would be a heap object each time.

  • Got: a whole line was appended, without its newline. The last line of an input that does not end in a newline is a line.
  • Partial: the output buffer filled before the line ended. What was appended is the line's start; the next call appends more of it.
  • End: the input has ended, and nothing was appended.
  • Failed(e): the source failed.
fun readLineInto : [Read r] fun(Buffered r, Buffer) -> System.IO.Line (ReadError r)

Append the next line's bytes, without the newline, to out, which the caller reuses from line to line: nothing is allocated, and nothing is checked, so it is the path for work on bytes -- counting lines, splitting fields, parsing numbers. A carriage return before the newline is kept.

A line longer than b's buffer is copied across a buffer at a time; one longer than out's room comes back Partial.

fun readLine : [Read r] fun(Buffered r) -> Either (Either InvalidUTF8 (ReadError r)) (Option String)

The next line as a String, without its newline or a carriage return before it, or None at the end of the input. One string per line, checked as UTF-8.

A line that does not fit in the window is moved to the front of the buffer and the buffer refilled behind it; one longer than the whole buffer is gathered in a buffer of its own.

type Lines r

The lines of a source, as readLine reads them, until its end or its first error, which is the last item.

fun lines : fun(Buffered r) -> Lines r
type ExactError e = Either EndedEarly e

Why readExact read fewer bytes than it was asked for: the input ended first, on the Left -- including before any of them, since a caller expecting a record wants one answer for "it is not there" -- or the source failed, on the Right.

type EndedEarly = EndedEarly
fun readExact : [Read r] fun(r, Buffer, Int) -> Either (Either EndedEarly (ReadError r)) Unit

Append exactly n bytes from r to into, for binary formats, and not one byte more: each read is given room for only what is still missing. Panics if into has less room than n.

fun readAll : [Read r] fun(r) -> Either (ReadError r) Buffer

Everything left in r, in a buffer as large as it needs, read 256 KiB at a time: more per call costs nothing, since the bytes are not scanned, and fewer costs system calls.

type CopyError a b = Reading(a) | Writing(b)

Why copy stopped: the source failed, or the sink did.

fun copy : [Read r, Write w] fun(r, w, Buffer) -> Either (CopyError (ReadError r) (WriteError w)) Int

Everything left in r, written to w through via, which is cleared before each read: how many bytes were copied. w is not flushed.

fun writeText : [Write w] fun(w, String) -> Either (WriteError w) Unit

text's bytes, written to w. They are copied, a buffer at a time, since a Buffer can be written into and a String must not be.

type AnyRead e

Any source whose errors can be made es: one type for "stdin or this file", decided at run time. A function that does not decide its source at run time takes [Read r] instead, which costs no indirect call; this one costs one per readInto, which is once per buffer, not per byte.

fun anyRead : [Read r] fun(r, fun(ReadError r) -> e) -> AnyRead e
type AnyWrite e

AnyRead's counterpart for sinks.

fun anyWrite : [Write w] fun(w, fun(WriteError w) -> e) -> AnyWrite e
type Buffer
fun buffer : fun(Int) -> Buffer

An empty buffer that holds up to capacity bytes.

fun capacity : fun(Buffer) -> Int
fun filled : fun(Buffer) -> Int

How many bytes are in the window, not yet consumed.

fun room : fun(Buffer) -> Int

How many bytes a read may append before the buffer is full.

fun clear : fun(Buffer) -> Unit

Empty the window, making the whole capacity room again.

fun consume : fun(Buffer, Int) -> Unit

Drop n bytes from the front of the window. A window emptied this way starts again at the front, so that the room behind it is not lost.

fun push : fun(Buffer, Byte) -> Unit

Append one byte. Panics if there is no room.

fun append : fun(Buffer, Array Byte, Int, Int) -> Unit

Append source[from..to). Panics if there is not room for all of it.

fun bytes : fun(Buffer) -> Array Byte

The window's bytes, in an array of their own.

fun bytesOf : fun(String) -> Buffer

A buffer holding a copy of text's bytes, full.

type IOError = IOError { kind : ErrorKind, op : String, path : Option String }

What failed (kind), in which operation (op, such as "open"), and on which path, if the operation had one. The path is kept for the message: an error that says "no such file or directory" without saying which file is the shape to avoid.

type ErrorKind = NotFound | PermissionDenied | AlreadyExists | IsDirectory | NotDirectory | NotEmpty | BrokenPipe | NoSpace | NotSeekable | Closed | TimedOut | HostNotFound | Resolver(Int) | ConnectionRefused | ConnectionReset | Unreachable | AddressInUse | Forbidden | Other(Int)

A closed sum, so that a program can recover from each case it cares about and be told when it has not considered one. NotSeekable is a positional read or write on something that has no positions: a FIFO, a pipe, a terminal. NotEmpty is removing a directory that still has entries. Closed is an operation on a file after close. TimedOut is an operation given a deadline that passed first. HostNotFound is a name the system's resolver has no address for (Net), and Resolver keeps the resolver's own code for any other failure of a lookup: it is not an errno, and differs by system. The network's own: ConnectionRefused, nothing listening at the address; ConnectionReset, the peer ended the connection abruptly; Unreachable, no route to the network or the host; AddressInUse, a port another socket holds; and Forbidden, a name whose every address the connect policy refused (Net.resolve). Other keeps the errno of anything else.

class Read r

A source of bytes. readInto appends to the buffer's room and answers how many bytes it appended: at least 1, or 0 only at the end of the input, and possibly fewer than there was room for. End of input is never an error. The buffer must have room; a call without any is a bug in the caller, and the library's own instances panic at it.

ReadError is the source's own error: IOError for a file or a pipe, DecodeError of the underlying source's for a layer that decodes. A function that wants one concrete error rather than lifting asks for it with an equality, [Read r, ReadError r ~ IOError].

fun readInto : [Read r] fun(r, Buffer) -> Either (ReadError r) Int
type ReadError r
class Write w

A sink of bytes. writeFrom writes the buffer's whole window, or fails; it leaves the buffer as it was, so the caller decides when to clear it. flush pushes on whatever the sink holds back, through every layer.

fun writeFrom : [Write w] fun(w, Buffer) -> Either (WriteError w) Unit
fun flush : [Write w] fun(w) -> Either (WriteError w) Unit
type WriteError w
type DecodeError e = Either InvalidUTF8 e

What a decoding layer over a source fails with: bytes that are not UTF-8 on the Left, or the source's own error on the Right. An Error when e is one, as every Either of two errors is.

type InvalidUTF8 = InvalidUTF8

Bytes that are not well-formed UTF-8 where text was wanted.

type Buffered r

A source or a sink with a buffer of its own, so that many small reads or writes cost one system call per buffer. It is a Read when what it wraps is, and a Write when what it wraps is.

Not to be shared between tasks running at once without a lock of the program's own (System.Sync.Mutex). Sharing one is memory-safe, since every index into the buffer is checked, but the tasks can see each other's half-done reads. What held holds is raw input not yet consumed, so !Debug keeps it out of a derived Debug, as the HTTP types keep theirs.

let defaultCapacity : Int

What buffered is given for a file or a pipe: large enough that a system call is rare, small enough that the window a line scan reads stays in the cache.

fun buffered : fun(r, Int) -> Buffered r
fun underlying : fun(Buffered r) -> r

What b wraps. Reading from it directly skips whatever b holds.

type Stdout

What System.IO.stdout() answers: the tasks waiting for the lock. The rest of the state is the block's. Its state is read and written only under its protocol, by tasks that may run in parallel, so !Debug keeps a derived Debug from reading it.

fun stdout : fun() -> Stdout
fun print : [Show a] fun(a) -> Unit
fun write : [Show a] fun(a) -> Unit
instance Iterator (Lines r) : Read r
instance Error EndedEarly
instance Show (CopyError a b) : Show a, Show b
instance Error (CopyError a b) : Error a, Error b
instance Read (AnyRead e)
instance Write (AnyWrite e)
instance Debug Buffer

The window start..end, the bytes held, as an array literal. The rest of storage is what was consumed or never written, and a buffer that is reused may still hold another message's bytes there, so it is not read.

instance Index Buffer

The i-th byte of the window.

instance Length Buffer
instance Iterator Buffer

The window's bytes, front to back, as the window is when each is asked for.

instance Error IOError

"open foo.txt: no such file or directory".

instance Show IOError
instance Error InvalidUTF8
instance Read (Buffered r) : Read r

A read at least as large as the buffer, made while the buffer is empty, goes straight into the caller's buffer: copying it through b's would only add a copy.

instance Write (Buffered w) : Write w

A write at least as large as the buffer, made while the buffer is empty, goes straight to the sink.

instance Write Stdout

A write is appended to the one standard output buffer, under its lock, so it lands between whole lines of print's. A failure to write the buffer out is not answered by the write that filled it, which may be another task's: the next flush reports it.

stderr : fun() -> File WriteOnly -- from System.File.stderr
stdin : fun() -> Buffered (File ReadOnly) -- from System.File.stdin