BufferedReader
Mojo struct 🡭
BufferedReader
@memory_only
struct BufferedReaderBufferedReader for readying lines and bytes from a file in a buffered way.
Example
from extramojo.io.buffered import BufferedReader
def read_bytes(imm file: String) raises -> List[UInt8]:
var fh = open(file, "r")
var reader = BufferedReader(fh^, buffer_capacity=50)
var buffer = List[UInt8](capacity=125)
for _ in range(0, 125):
buffer.append(0)
var found_file = List[UInt8]()
# Read bytes from the buf reader, copy to found
var bytes_read = 0
while True:
bytes_read = reader.read_bytes(buffer)
if bytes_read == 0:
break
found_file.extend(buffer[0:bytes_read])
return found_file^Fields
- fh (
FileHandle): The internal filehandle to read from. - buffer (
Pointer[UInt8, MutUntrackedOrigin]): The internal buffer. - file_offset (
Int): Current offset into the file. - buffer_offset (
Int): Current offset into the buffer. - buffer_capacity (
Int): Total capacity of the buffer. - buffer_len (
Int): Total filled capacity of the buffer.
Implemented traits
AnyType, Deinitable, Movable
Methods
➕ ➖__init__
def __init__(out self, var fh: FileHandle, buffer_capacity: Int = Int(131072))Create a
Args:BufferedReader.
- fh (
FileHandle): The filehandle to read from. - buffer_capacity (
Int): The size of the buffer to use. - self (
Self)
Returns:
Self
Raises:
def __init__(out self, *, deinit move: Self)Details
Args:
- move (
Self) - self (
Self)
Returns:
Self
__deinit__
def __deinit__(deinit self)Details
Args:
- self (
Self)
__enter__
def __enter__(var self) -> SelfDetails
Args:
- self (
Self)
Returns:
Self
read_bytes
def read_bytes(mut self, mut buffer: List[UInt8]) -> IntRead up to
Args:len(buffer) bytes.
- self (
Self) - buffer (
List[UInt8]): The buffer to read into. Thelenof thebufferdetermines how many bytes will be read.
Returns:
Int: This returns the number of bytes read.
If the number of bytes read is less then len(buffer) then EOF has been reached.
Raises:
read_until
def read_until(mut self, mut buffer: List[UInt8], char: UInt = UInt(10)) -> IntFill the given
Note: the callee is responsible for clearing (or not) the buffer between calls to line_buffer until the given char is hit, or EOF.read_until.
Args:
- self (
Self) - buffer (
List[UInt8]): The buffer to filled with any bytes found beforecharis hit. - char (
UInt): The character to use as the terminator.
Returns:
Int: The number of bytes read.
Raises: