System.IO
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.
stdin()is the one reader of the standard input the program has: every call answers the sameBuffered, so that bytes one part of the program read ahead are not lost to another. Its buffer holds 8 KiB on a terminal and 64 KiB otherwise.stdout()is the standard output thatprintandwritewrite to, buffered once for the whole process (Turkey.Stdoutsays when it is written out), and safe to share between tasks. Itsflushreports a failed write, whichprintdoes not.stderr()is the standard error, unbuffered: every write is a system call. The standard output is written out before each, so that the two come out in the order the program wrote them.
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.
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.
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.
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.
The lines of a source, as readLine reads them, until its end or its first
error, which is the last item.
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.
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.
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.
Why copy stopped: the source failed, or the sink did.
Everything left in r, written to w through via, which is cleared
before each read: how many bytes were copied. w is not flushed.
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.
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.
AnyRead's counterpart for sinks.
An empty buffer that holds up to capacity bytes.
How many bytes are in the window, not yet consumed.
How many bytes a read may append before the buffer is full.
Empty the window, making the whole capacity room again.
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.
Append one byte. Panics if there is no room.
Append source[from..to). Panics if there is not room for all of it.
The window's bytes, in an array of their own.
A buffer holding a copy of text's bytes, full.
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.
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.
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].
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.
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.
Bytes that are not well-formed UTF-8 where text was wanted.
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.
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.
What b wraps. Reading from it directly skips whatever b holds.
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.
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.
The i-th byte of the window.
The window's bytes, front to back, as the window is when each is asked for.
"open foo.txt: no such file or directory".
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.
A write at least as large as the buffer, made while the buffer is empty, goes straight to the sink.
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.