bufiox

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 6 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BytesReader

type BytesReader struct {
	// contains filtered or unexported fields
}

func NewBytesReader

func NewBytesReader(buf []byte) *BytesReader

NewBytesReader returns a new DefaultReader that reads from buf[:len(buf)]. Its operation on buf is read-only.

func (*BytesReader) Next added in v0.1.9

func (r *BytesReader) Next(n int) (buf []byte, err error)

func (*BytesReader) Peek added in v0.1.9

func (r *BytesReader) Peek(n int) (buf []byte, err error)

func (*BytesReader) ReadBinary added in v0.1.9

func (r *BytesReader) ReadBinary(bs []byte) (n int, err error)

func (*BytesReader) ReadLen added in v0.1.9

func (r *BytesReader) ReadLen() (n int)

func (*BytesReader) Release added in v0.1.9

func (r *BytesReader) Release(e error) error

func (*BytesReader) Skip added in v0.1.9

func (r *BytesReader) Skip(n int) (err error)

type BytesWriter

type BytesWriter struct {
	// contains filtered or unexported fields
}

BytesWriter implements Writer and builds a []byte result.

It uses a deferred-copy scheme to avoid copying on buffer growth: when the buffer needs to grow, the old buffer is saved to oldBuf and a new buffer is allocated WITHOUT copying the old data. Slices returned by Malloc still point into the old buffer's backing array, so writes to them remain valid. At Flush time, data is reconstructed by copying each oldBuf entry's delta into the final buffer.

BytesWriter can be flushed multiple times; each Flush outputs the accumulated data (including pre-existing data) and resets WrittenLen to 0.

func NewBytesWriter

func NewBytesWriter(buf *[]byte) *BytesWriter

NewBytesWriter returns a new BytesWriter that appends to buf[len(buf):cap(buf)]. Existing data in buf[:len(buf)] is preserved.

func (*BytesWriter) Flush added in v0.1.9

func (w *BytesWriter) Flush() (err error)

func (*BytesWriter) Malloc added in v0.1.9

func (w *BytesWriter) Malloc(n int) (buf []byte, err error)

func (*BytesWriter) WriteBinary added in v0.1.9

func (w *BytesWriter) WriteBinary(bs []byte) (n int, err error)

func (*BytesWriter) WrittenLen added in v0.1.9

func (w *BytesWriter) WrittenLen() int

type DefaultReader

type DefaultReader struct {
	// contains filtered or unexported fields
}

func NewDefaultReader

func NewDefaultReader(rd io.Reader) *DefaultReader

NewDefaultReader returns a new DefaultReader that reads from r.

func (*DefaultReader) Buffered added in v0.1.10

func (r *DefaultReader) Buffered() int

Buffered returns the number of bytes that can be read from the current buffer.

func (*DefaultReader) Next

func (r *DefaultReader) Next(n int) (buf []byte, err error)

func (*DefaultReader) Peek

func (r *DefaultReader) Peek(n int) (buf []byte, err error)

func (*DefaultReader) Read added in v0.1.5

func (r *DefaultReader) Read(bs []byte) (n int, err error)

Read implements io.Reader If some data is available but fewer than len(bs) bytes, Read returns what is available instead of waiting for more, which differs from ReadBinary.

func (*DefaultReader) ReadBinary

func (r *DefaultReader) ReadBinary(bs []byte) (n int, err error)

ReadBinary reads exactly len(bs) bytes to bs, wait for reading from the underlying reader until done, or returns the actual read data length and err if there's no enough data.

func (*DefaultReader) ReadLen

func (r *DefaultReader) ReadLen() (n int)

func (*DefaultReader) Release

func (r *DefaultReader) Release(e error) error

func (*DefaultReader) Skip

func (r *DefaultReader) Skip(n int) (err error)

type DefaultWriter

type DefaultWriter struct {
	// contains filtered or unexported fields
}

func NewDefaultWriter

func NewDefaultWriter(wd io.Writer) *DefaultWriter

NewDefaultWriter returns a new DefaultWriter that writes to w.

func (*DefaultWriter) Flush

func (w *DefaultWriter) Flush() (err error)

func (*DefaultWriter) Malloc

func (w *DefaultWriter) Malloc(n int) (buf []byte, err error)

func (*DefaultWriter) WriteBinary

func (w *DefaultWriter) WriteBinary(bs []byte) (n int, err error)

func (*DefaultWriter) WrittenLen

func (w *DefaultWriter) WrittenLen() int

type Reader

type Reader interface {
	// Next reads the next n bytes sequentially and returns a slice `p` of length `n`,
	// otherwise returns an error if it is unable to read a buffer of n bytes.
	// The returned `p` can be a shallow copy of the original buffer.
	// Must ensure that the data in `p` is not modified before calling Release.
	//
	// Callers cannot use the returned data after calling Release.
	Next(n int) (p []byte, err error)

	// ReadBinary reads up to len(p) bytes into p. It returns the number of bytes
	// read (0 <= n <= len(p)) and any error encountered. Even if Read
	// returns n < len(p), it may use all of p as scratch space during the call.
	//
	// The bs is valid even if it is after calling Release, as it's copy read.
	ReadBinary(bs []byte) (n int, err error)

	// Peek behaves the same as Next, except that it doesn't advance the reader.
	//
	// Callers cannot use the returned data after calling Release.
	Peek(n int) (buf []byte, err error)

	// Skip skips the next n bytes sequentially, otherwise returns an error if it's unable to skip a buffer of n bytes.
	Skip(n int) (err error)

	// ReadLen returns the size that has already been read.
	// ReadBinary / Next / Skip will increase the size. When the Release function is called, ReadLen is set to 0.
	ReadLen() (n int)

	// Release will free the buffer. After release, buffer read by Next/Skip/Peek is invalid.
	// Param e is used when the buffer release depend on error.
	// Release does not close and release the underlying connection, but only releases the user mode buffer that has been read.
	Release(e error) (err error)
}

Reader is a buffer IO interface, which provides a user-space zero-copy method to reduce memory allocation and copy overhead.

type Writer

type Writer interface {
	// Malloc returns a shallow copy of the write buffer with length n,
	// otherwise returns an error if it's unable to get n bytes from the write buffer.
	// Must ensure that the data written by the user to buf can be flushed to the underlying io.Writer.
	//
	// Caller cannot write data to the returned buf after calling Flush.
	Malloc(n int) (buf []byte, err error)

	// WriteBinary writes bs to the buffer, it may be a zero copy write.
	// MUST ensure that bs is not being concurrently written before calling Flush.
	// It returns err if n < len(bs), while n is the number of bytes written.
	WriteBinary(bs []byte) (n int, err error)

	// WrittenLen returns the total length of the buffer written.
	// Malloc / WriteBinary will increase the length. When the Flush function is called, WrittenLen is set to 0.
	WrittenLen() (length int)

	// Flush writes any malloc data to the underlying io.Writer, and reset WrittenLen to zero.
	Flush() (err error)
}

Writer is a buffer IO interface, which provides a user-space zero-copy method to reduce memory allocation and copy overhead.

Jump to

Keyboard shortcuts

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