io
BufferedWriter

BufferedWriter

Mojo struct 🡭

BufferedWriter

@memory_only
struct BufferedWriter[W: Movable & Writer]

A BufferedWriter.

## Example

```mojo
from extramojo.io.buffered import BufferedWriter
fn write_to_file(read file: String, read expected_lines: List[String]) raises:
    var fh = BufferedWriter(open(String(file), "w"), buffer_capacity=128)
    for i in range(len(expected_lines)):
        fh.write_bytes(expected_lines[i].as_bytes())
        fh.write_bytes("

“.as_bytes()) fh.flush() fh.close() ```

Parameters

  • W (Movable & Writer)

Fields

  • inner (W): The inner file handle to write to.
  • buffer (List[SIMD[uint8, 1]]): The inner buffer.
  • buffer_capacity (Int): The capacity of the inner buffer.
  • buffer_len (Int): The number of bytes currently stored in the inner buffer.

Implemented traits

AnyType, Movable, UnknownDestructibility, Writer

Methods

 

__init__

fn __init__(out self, var writer: W, buffer_capacity: Int = 131072)

Create a BufferedReader. Args:

  • writer (W): The writer to write to.
  • buffer_capacity (Int): The capacity of the inner buffer to use.
  • self (Self)

Returns:

Self

Raises:

__moveinit__

@staticmethod
fn __moveinit__(out self, var existing: Self)

Details Args:

  • existing (Self)
  • self (Self)

Returns:

Self

__del__

fn __del__(var self)

Details Args:

  • self (Self)

__enter__

fn __enter__(var self) -> Self

Details Args:

  • self (Self)

Returns:

Self

close

fn close(mut self)

Details Args:

  • self (Self)

Raises:

write_bytes

fn write_bytes(mut self, bytes: Span[SIMD[uint8, 1], origin])

Write bytes to this writer. Args:

  • self (Self)
  • bytes (Span): The bytes that will be written to the underlying buffer.

write

fn write[*Ts: Writable](mut self, *args: *Ts)

Implement write. Parameters:

  • *Ts (Writable)

Args:

  • self (Self)
  • *args (*Ts): Any Writable values that will be written to the writer.

flush

fn flush(mut self)

Write any remaining bytes in the current buffer, then clear the buffer. Args:

  • self (Self)