Skip to content

BufferedReader

Mojo struct 🡭

@memory_only
struct BufferedReader[R: Reader]
Buffered reader over a Reader. Fills an internal buffer and exposes `view()`, `consume()`, `stream_position()`, etc. Used by `LineIterator` and thus by FastqParser. Supports buffer growth for long lines when enabled in LineIterator.
Unsafe, low-level building block: callers must uphold preconditions.
Misuse (e.g. capacity <= 0, out-of-bounds index, or reading past EOF)
can lead to undefined behavior.

o

  • R (Reader)
  • source (R)

AnyType, Deinitable, Movable, Sized, Writable

fn def __init__(out self, var reader: R, capacity: Int = Int(262144))

Wrap a Reader with a buffer of given capacity. Reads once to fill buffer.

Args:

  • reader (R)
  • capacity (Int)
  • self (Self)

Returns:

Self

Raises:

fn def __getitem__(self, index: Int) -> Byte

Index into unconsumed bytes; index is relative to current position (0 = first unconsumed). Indices are not validated; out-of-bounds access is undefined behavior.

Args:

  • self (Self)
  • index (Int)

Returns:

Byte

fn def __getitem__(ref self, sl: ContiguousSlice) -> Span[UInt8, MutUntrackedOrigin]

Slice unconsumed bytes. Indices are relative to current position: buf[0] = first unconsumed byte, buf[:] = all unconsumed, buf[0:n] = first n unconsumed.

Args:

  • self (Self)
  • sl (ContiguousSlice)

Returns:

Span[UInt8, MutUntrackedOrigin]

fn def available(self) -> Int

Current number of bytes in the buffer (same as __len__).

Args:

  • self (Self)

Returns:

Int

fn def consume(mut self, size: Int) -> Int

Advance read position by size. Must not exceed available(). Does not compact; caller must call compact_from() when needed.

Args:

  • self (Self)
  • size (Int)

Returns:

Int

fn def unconsume(mut self, size: Int)

Unconsume size bytes. _head tracks position relative to compacted data; must not unconsume past the start (size <= _head). Use debug build with ASSERT=all to catch violations.

Args:

  • self (Self)
  • size (Int)
fn def stream_position(self) -> Int

Current logical position in the stream that parser is reading. Use for error messages: “Parse error at byte N”.

Args:

  • self (Self)

Returns:

Int

fn def buffer_position(self) -> Int

Current read offset in the buffer (for parser compact_from).

Args:

  • self (Self)

Returns:

Int

fn def is_eof(self) -> Bool

True when underlying read returned no more data.

Args:

  • self (Self)

Returns:

Bool

fn def capacity(self) -> Int

Return the buffer capacity in bytes.

Args:

  • self (Self)

Returns:

Int

fn def grow_buffer(mut self, additional: Int, max_capacity: Int)

Grow buffer by additional bytes, not exceeding max_capacity. Compacts first to maximize usable space and avoid unnecessary growth. Use case: Parser encounters line longer than buffer, needs more space.

Args:

  • self (Self)
  • additional (Int)
  • max_capacity (Int)

Raises:

fn def resize_buffer(mut self, additional: Int, max_capacity: Int)

Resize buffer by additional bytes, not exceeding max_capacity. Does nott compact the buffer before resizing.

Args:

  • self (Self)
  • additional (Int)
  • max_capacity (Int)

Raises:

fn def view(ref self) -> Span[UInt8, MutUntrackedOrigin]

View of all unconsumed bytes. Valid until next mutating call.

Args:

  • self (Self)

Returns:

Span[UInt8, MutUntrackedOrigin]

fn def write_to[w: Writer](self, mut writer: w)

Parameters:

  • w (Writer)

Args:

  • self (Self)
  • writer (w)
fn def peek(ref self, amt: Int) -> Span[UInt8, MutUntrackedOrigin]

Peek at the next amt bytes in the buffer without consuming them.

Args:

  • self (Self)
  • amt (Int)

Returns:

Span[UInt8, MutUntrackedOrigin]

fn def compact_and_fill(mut self) -> UInt64

Compact the buffer by discarding consumed bytes and shifting remaining data to the start, then fill the buffer with more data from the source. Returns the number of bytes read from the source.

Args:

  • self (Self)

Returns:

UInt64

Raises:

fn def __len__(self) -> Int

Args:

  • self (Self)

Returns:

Int