bufiox

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: Apache-2.0 Imports: 4 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BytesReader

type BytesReader struct {
	DefaultReader
	// 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.

type BytesWriter

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

func NewBytesWriter

func NewBytesWriter(buf *[]byte) *BytesWriter

NewBytesWriter returns a new DefaultWriter that writes to buf[len(buf):cap(buf)]. The WrittenLen is set to len(buf) before the first write.

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) 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) ReadBinary

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

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.
	// For example, usually the write buffer will be released inside flush,
	// but if flush error happen, write buffer may need to be released explicitly.
	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