flix

0.78.0

BufReader

struct BufReader[a: Type, t: Type, r: Eff]Source
buffer: Array[a, r]mut cursor: Int32mut size: Int32reader: t

Type of buffered readers.

A BufReader is a Readable implementation that wraps an underlying Readable instance and stores the items read from the underlying instance in an internal buffer.

A call to read will read directly from the buffer if items are available, and will trigger a read from the underlying reader when the buffer is empty.

This has the effect of reducing the overall number of reads to the underlying Readable.

A call to read might read more than the requested number of items from the underlying reader, in order to fill the entire buffer.

A call to read might not read from the underlying reader at all, if there are enough items in the buffer.

buffer: the underlying buffer. cursor: the index in buffer of the first element logically considered to be in the buffer. size: the number of elements (starting at cursor) logically considered to be in the buffer. reader: the underlying Readable being read from.

Type of buffered readers, and its functions.

Instances

instance Peekable[BufReader[a, t, r]] with Readable[t] where Readable.Elm[t] ~ aSource
instance Readable[BufReader[a, t, rr]] with Readable[t] where Readable.Elm[t] ~ aSource
type Elm = atype Aef = rr + Aef[t]

Definitions

def peek(bufReader: BufReader[a, t, r]): Result[IoError, Option[Elm[t]]] \ Aef[t] + r with Readable[t] where Readable.Elm[t] ~ a§ Source

Returns Ok(Some(x)) where x is the next value to be read, or Ok(None) if the bufReader is empty. Does not advance the bufReader.

Returns Err(err) if there is an IO error.

def read(dest: Array[Elm[t], rb], bufReader: BufReader[a, t, rr]): Result[IoError, Int32] \ rb + rr + Aef[t] with Readable[t] where Readable.Elm[t] ~ a§ Source

Reads k items from the underlying Readable into dest, storing items read into the internal buffer.

Returns Ok(k) to signify that k items were successfully read and written to dest.

Guarantees that 0 <= k <= length(dest).

Returns Err(e) if some underlying error occurs.

def readWhile(p: a -> Bool, bufReader: BufReader[a, t, r]): Result[IoError, Vector[a]] \ Aef[t] + r with Readable[t] where Readable.Elm[t] ~ a§ Source

Reads from reader until an element is reached that does not satisfy predicate p.

Returns Ok(vec) where vec is a vector of the first k elements where k is the index of the first element that does not satisfy p. This element is not included in the vector, and will be read on the next read.

All elements of vec are guaranteed to satisfy p.

Returns Err(err) if there is an IO error.

def skip(n: Int32, bufReader: BufReader[a, t, r]): Result[IoError, Int32] \ Aef[t] + r with Readable[t] where Readable.Elm[t] ~ a§ Source

Advances the reader by n, skipping over the elements without reading them.

Returns Ok(k) where k is the number of items skipped.

Guarantees that 0 <= k <= max(n, 0). If k < n, then EOF has been reached.

Returns Err(err) if there is an IO error.

def withCapacity(rc: Region[r], capacity: Int32, reader: t): BufReader[a, t, r] \ r with Readable[t]§ Source

Constructs a BufReader wrapping reader with the specified capacity in items.

If capacity <= 0, the BufReader will delegate directly to reader without buffering.

def withDefaultCapacity(rc: Region[r], reader: t): BufReader[a, t, r] \ r with Readable[t]§ Source

Constructs a BufReader wrapping reader with a default capacity of 4096 items.