BufferedReader
Mojo struct 🡭
BufferedReader
Section titled “BufferedReader”@memory_onlystruct 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
Parameters
Section titled “Parameters”- R (
Reader)
Fields
Section titled “Fields”- source (
R)
Implemented traits
Section titled “Implemented traits”AnyType, ImplicitlyDestructible, Movable, Sized, Writable
Methods
Section titled “Methods”__init__
Section titled “__init__”fn __init__(out self, var reader: R, capacity: 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:
__getitem__
Section titled “__getitem__”fn __getitem__(self, index: Int) -> ByteIndex 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 __getitem__(ref self, sl: ContiguousSlice) -> Span[UInt8, MutExternalOrigin]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, MutExternalOrigin]
available
Section titled “available”fn available(self) -> IntCurrent number of bytes in the buffer (same as __len__).
Args:
- self (
Self)
Returns:
Int
consume
Section titled “consume”fn consume(mut self, size: Int) -> IntAdvance 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
unconsume
Section titled “unconsume”fn 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)
stream_position
Section titled “stream_position”fn stream_position(self) -> IntCurrent logical position in the stream that parser is reading. Use for error messages: “Parse error at byte N”.
Args:
- self (
Self)
Returns:
Int
buffer_position
Section titled “buffer_position”fn buffer_position(self) -> IntCurrent read offset in the buffer (for parser compact_from).
Args:
- self (
Self)
Returns:
Int
is_eof
Section titled “is_eof”fn is_eof(self) -> BoolTrue when underlying read returned no more data.
Args:
- self (
Self)
Returns:
Bool
capacity
Section titled “capacity”fn capacity(self) -> IntReturn the buffer capacity in bytes.
Args:
- self (
Self)
Returns:
Int
grow_buffer
Section titled “grow_buffer”fn 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:
resize_buffer
Section titled “resize_buffer”fn 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 view(ref self) -> Span[UInt8, MutExternalOrigin]View of all unconsumed bytes. Valid until next mutating call.
Args:
- self (
Self)
Returns:
Span[UInt8, MutExternalOrigin]
write_to
Section titled “write_to”fn write_to[w: Writer](self, mut writer: w)Parameters:
- w (
Writer)
Args:
- self (
Self) - writer (
w)
fn peek(ref self, amt: Int) -> Span[UInt8, MutExternalOrigin]Peek at the next amt bytes in the buffer without consuming them.
Args:
- self (
Self) - amt (
Int)
Returns:
Span[UInt8, MutExternalOrigin]
compact_and_fill
Section titled “compact_and_fill”fn compact_and_fill(mut self) -> UInt64Compact 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:
__len__
Section titled “__len__”fn __len__(self) -> IntArgs:
- self (
Self)
Returns:
Int