bufferedwriter

package
v0.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package bufferedwriter provides functionality to wrap an io.Writer while keep maintaining the underlying capability to write at specific bytes such as when it's implementing io.WriterAt or io.WriteSeeker. This package differs from bufio.Writer which encapsulates the writer's implementation details as io.Writer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BufferedWriter

type BufferedWriter interface {
	io.Writer
	// Flush writes any buffered data to the underlying io.Writer.
	Flush() error
}

BufferedWriter is a writter with buffer, it supports io.Writer, io.WriterAt and io.WriteSeeker.

func New

func New(w io.Writer) BufferedWriter

New is shorthand for NewSize(w, 4096), a 4KB Buffer. See NewSize() for details.

func NewSize

func NewSize(w io.Writer, size int) BufferedWriter

NewSize creates a buffered writer with a specified size while taking into account the underlying capabilities of the writer w, which might implement either io.WriterAt or io.WriteSeeker. This allows the buffered writer to maintain the ability to write at a specific byte position.

Use-case scenario:

  • An *os.File may be passed to a function receiving an io.Writer, while that function may assert the original ability of the value to write at a specific byte position if possible to enable a faster processing path. Nonetheless, directly working with *os.File for frequent writing small byte segments can affect performance due to numerous syscalls. To alleviate this issue, incorporating a buffered writer for the *os.File becomes essential. Unlike bufio.Writer that encapsulates everything as an io.Writer, this approach preserves the inherent capabilities of io.WriterAt and io.WriteSeeker.

Just like any other buffered writer, the Flush() method should be called after the process is completed to write the unwritten buffered data.

type WriteSeeker

type WriteSeeker struct {
	*bufio.Writer
	// contains filtered or unexported fields
}

WriteSeeker is an io.WriteSeeker buffer wrapper.

func (*WriteSeeker) Seek

func (w *WriteSeeker) Seek(offset int64, whence int) (n int64, err error)

type WriterAt

type WriterAt struct {
	*bufio.Writer
	// contains filtered or unexported fields
}

WriterAt is an io.WriterAt buffer wrapper.

func (*WriterAt) WriteAt

func (w *WriterAt) WriteAt(p []byte, offset int64) (n int, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL