Documentation
¶
Overview ¶
Package iobuf provides a buffered reading interface with seeking capabilities.
For small amounts of data, it uses an in-memory buffer (bytes.Buffer) to store read bytes. When the amount of data exceeds a specified threshold, it switches to disk-based buffering using a temporary file. This approach balances memory usage and performance, allowing efficient handling of both small and large data streams.
Index ¶
- type BufferedReadSeeker
- func (br *BufferedReadSeeker) Close() error
- func (br *BufferedReadSeeker) Read(out []byte) (int, error)
- func (br *BufferedReadSeeker) ReadAt(out []byte, offset int64) (int, error)
- func (br *BufferedReadSeeker) Seek(offset int64, whence int) (int64, error)
- func (br *BufferedReadSeeker) Size() (int64, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BufferedReadSeeker ¶
type BufferedReadSeeker struct {
// contains filtered or unexported fields
}
BufferedReadSeeker provides a buffered reading interface with seeking capabilities. It wraps an io.Reader and optionally an io.Seeker, allowing for efficient reading and seeking operations, even on non-seekable underlying readers.
For small amounts of data, it uses an in-memory buffer (bytes.Buffer) to store read bytes. When the amount of data exceeds a specified threshold, it switches to disk-based buffering using a temporary file. This approach balances memory usage and performance, allowing efficient handling of both small and large data streams.
The struct manages the transition between in-memory and disk-based buffering transparently, providing a seamless reading and seeking experience regardless of the underlying data size or the seekability of the original reader.
If the underlying reader is seekable, direct seeking operations are performed on it. For non-seekable readers, seeking is emulated using the buffer or temporary file.
The caller MUST call Close() when done using the BufferedReadSeeker to clean up resources like temporary files and buffers.
func NewBufferedReaderSeeker ¶
func NewBufferedReaderSeeker(r io.Reader) *BufferedReadSeeker
NewBufferedReaderSeeker creates and initializes a BufferedReadSeeker. It takes an io.Reader and checks if it supports seeking. If the reader supports seeking, it is stored in the seeker field.
The caller MUST call Close() when done using the returned BufferedReadSeeker to clean up resources like temporary files and buffers.
func (*BufferedReadSeeker) Close ¶ added in v3.80.2
func (br *BufferedReadSeeker) Close() error
Close closes the BufferedReadSeeker and releases any resources used. It closes the temporary file if one was created and removes it from disk and returns the buffer to the pool.
func (*BufferedReadSeeker) Read ¶
func (br *BufferedReadSeeker) Read(out []byte) (int, error)
Read reads len(out) bytes from the reader starting at the current index. It handles both seekable and non-seekable underlying readers efficiently.
func (*BufferedReadSeeker) ReadAt ¶
func (br *BufferedReadSeeker) ReadAt(out []byte, offset int64) (int, error)
ReadAt reads len(out) bytes into out starting at offset off in the underlying input source. It uses Seek and Read to implement random access reading.
func (*BufferedReadSeeker) Seek ¶
func (br *BufferedReadSeeker) Seek(offset int64, whence int) (int64, error)
Seek sets the offset for the next Read or Write to offset. It supports both seekable and non-seekable underlying readers.
func (*BufferedReadSeeker) Size ¶ added in v3.82.3
func (br *BufferedReadSeeker) Size() (int64, error)
Size returns the total size of the reader.