std / bufio

bufio

import "std:bufio"

Provides buffered I/O wrappers around io.Reader and io.Writer for improved performance when doing many small reads or writes. Includes Reader (buffered reading with ReadByte, ReadLine, ReadBytes, Peek), Writer (buffered writing with WriteByte, WriteBytes, WriteString, Flush), and Scanner (convenient line-by-line scanning).

View source on Codeberg →

Types

#
type Error int

Error represents a bufio error code. Possible values are OK, EOF, ErrIO, and ErrFull.

#
const (
    OK   Error = iota
    EOF
    ErrIO
    ErrFull
)

Error constants: OK indicates success, EOF indicates end of stream, ErrIO indicates an underlying I/O error, and ErrFull indicates the buffer is full.

#
type Reader struct { /* unexported fields */ }

Reader wraps an io.Reader with an internal buffer for efficient reading. Small reads are served from the buffer, reducing the number of underlying I/O calls.

#
type Writer struct { /* unexported fields */ }

Writer wraps an io.Writer with an internal buffer for efficient writing. Small writes are accumulated in the buffer and flushed in larger batches.

#
type Scanner struct { /* unexported fields */ }

Scanner provides a convenient interface for reading line-by-line from an io.Reader. It wraps a Reader and splits input on newline boundaries.

Constants

#
const DefaultBufSize = 4096

DefaultBufSize is the default buffer size in bytes used by NewReader, NewWriter, and NewScanner when no explicit size is provided.

Reader Construction

#
func NewReader(r io.Reader) Reader

Creates a new buffered Reader wrapping the given reader with the default buffer size (DefaultBufSize).

#
func NewReaderSize(r io.Reader, size int) Reader

Creates a new buffered Reader wrapping the given reader with the specified buffer size in bytes.

Reader Operations

#
func Buffered(rd *Reader) int

Returns the number of bytes currently available to read from the buffer without triggering an underlying I/O operation.

#
func ReadByte(rd *Reader) (byte, Error)

Reads and returns a single byte from the buffered reader. Returns EOF if no more data is available.

#
func ReadLine(rd *Reader) (string, Error)

Reads a single line from the buffered reader, excluding the trailing newline character. Returns the line as a string and an Error indicating success, EOF, or failure.

#
func ReadBytes(rd *Reader, delim byte) (list[byte], Error)

Reads until the first occurrence of the delimiter byte, returning a dynamic byte array including the delimiter. The caller is responsible for freeing the returned array.

#
func Read(rd *Reader, p []byte) (int, Error)

Reads up to len(p) bytes into p from the buffered reader. Returns the number of bytes read and an Error. Reads from the internal buffer first, refilling from the underlying stream as needed.

#
func Peek(rd *Reader, n int) ([]byte, Error)

Returns the next n bytes from the buffer without advancing the reader. The returned slice is only valid until the next read call. Returns ErrFull if n exceeds the buffer size.

#
func UnreadByte(rd *Reader) Error

Pushes the last byte read back into the buffer so it will be returned by the next read operation. Only one byte can be unread at a time.

#
func FreeReader(rd *Reader)

Releases the internal buffer memory associated with the Reader. The Reader must not be used after calling FreeReader.

Writer Construction

#
func NewWriter(w io.Writer) Writer

Creates a new buffered Writer wrapping the given writer with the default buffer size (DefaultBufSize).

#
func NewWriterSize(w io.Writer, size int) Writer

Creates a new buffered Writer wrapping the given writer with the specified buffer size in bytes.

Writer Operations

#
func Available(wr *Writer) int

Returns the number of bytes that can be written to the buffer without triggering a flush to the underlying stream.

#
func WriterBuffered(wr *Writer) int

Returns the number of bytes that have been written to the buffer but not yet flushed to the underlying stream.

#
func Flush(wr *Writer) Error

Writes any buffered data to the underlying stream. Returns an Error indicating success or failure.

#
func WriteByte(wr *Writer, b byte) Error

Writes a single byte to the buffered writer. The byte is stored in the internal buffer and flushed to the stream when the buffer is full or Flush is called.

#
func WriteBytes(wr *Writer, data []byte) (int, Error)

Writes a byte slice to the buffered writer. Returns the number of bytes written and an Error. Data is buffered internally and flushed as needed.

#
func WriteString(wr *Writer, s string) (int, Error)

Writes a string to the buffered writer. Returns the number of bytes written and an Error. The string bytes are buffered internally and flushed as needed.

#
func FreeWriter(wr *Writer)

Releases the internal buffer memory associated with the Writer. Any unflushed data is discarded. The Writer must not be used after calling FreeWriter.

Scanner

#
func NewScanner(r io.Reader) Scanner

Creates a new Scanner that reads from the given reader. The scanner splits input on newline boundaries for convenient line-by-line processing.

#
func Scan(sc *Scanner) bool

Advances the scanner to the next line. Returns true if a line was successfully read, or false when the input is exhausted or an error occurs. After Scan returns true, the line text is available via Text.

#
func Text(sc *Scanner) string

Returns the text of the current line from the most recent Scan call, excluding the trailing newline.

#
func ScanErr(sc *Scanner) Error

Returns the first non-EOF error encountered during scanning. Returns OK if no error occurred (normal EOF is not reported as an error).

#
func FreeScanner(sc *Scanner)

Releases the internal buffer memory associated with the Scanner. The Scanner must not be used after calling FreeScanner.

Adapters

#
func ToIOReader(rd *Reader) io.Reader

Wraps a bufio.Reader as an io.Reader.

#
func ToIOWriter(wr *Writer) io.Writer

Wraps a bufio.Writer as an io.Writer.