BufferedWriter

BufferedWriter

Mojo struct 🡭

BufferedWriter

@memory_only
struct BufferedWriter[W: Writer & Deinitable & Movable]

A BufferedWriter.

## Example

```mojo
from extramojo.io.buffered import BufferedWriter
def write_to_file(imm file: String, imm 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 (Writer & Deinitable & Movable)

Fields

  • inner (W): The inner file handle to write to.
  • buffer (List[UInt8]): 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, Deinitable, Movable, Writer

Methods

 

__init__

def __init__(out self, var writer: W, buffer_capacity: Int = 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:

def __init__(out self, *, deinit move: Self)

DetailsArgs:

  • move (Self)
  • self (Self)

Returns:

Self

__deinit__

def __deinit__(deinit self)

DetailsArgs:

  • self (Self)

__enter__

def __enter__(var self) -> Self

DetailsArgs:

  • self (Self)

Returns:

Self

close

def close(mut self)

DetailsArgs:

  • self (Self)

Raises:

write_string

def write_string(mut self, string: StringSpan)

DetailsArgs:

  • self (Self)
  • string (StringSpan)

write_bytes

def write_bytes(mut self, bytes: Span[UInt8])

Write bytes to this writer.Args:

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

write

def write[*Ts: Writable](mut self, *args: *Ts.values)

Implement write.Parameters:

  • *Ts (Writable)

Args:

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

flush

def flush(mut self)

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

  • self (Self)