bytes

package
v0.0.0-...-242f3d8 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2020 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoEnoughData represents no enough data to return in a buffer.
	ErrNoEnoughData = errors.New("bytes.ReadOnlyBuffer: no engough data")
)
View Source
var (
	// ErrNoEnoughHeader represents no enough space for header to store data in a buffer.
	ErrNoEnoughHeader = errors.New("bytes.WriteOnlyBuffer: no enough header space to write")
)
View Source
var (
	// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
	ErrTooLarge = errors.New("bytes.WriteOnlyBuffer: too large")
)

Functions

This section is empty.

Types

type ReadOnlyBuffer

type ReadOnlyBuffer interface {
	// Read reads n bytes start with offset from buffer.
	Read(offset, n int) ([]byte, error)

	// ReadFrom reads reader's data to buffer
	ReadFrom(reader io.Reader) (int, error)

	// Seek detects bytes of n from buffer.
	Seek(n int) ([]byte, error)

	// Len returns the number of bytes of the unread portion of the buffer;
	// b.Len() == len(b.Bytes()).
	Len() int

	// FreeBytes returns all the free space in the buffer and won't mark the slice as read.
	FreeBytes() []byte

	// Discard marks the data of n from off as readed.
	Discard(n int)

	// Bytes returns a slice of length len(b.buf) holding the unread portion of the buffer.
	// The slice is valid for use only until the next buffer modification.
	// The slice aliases the buffer content at least until the next buffer modification,
	// so immediate changes to the slice will affect the result of future reads.
	Bytes() []byte

	// Reset resets the buffer to be empty,
	// but it retains the underlying storage for use by future writes.
	Reset()
}

ReadOnlyBuffer defines a buffer only used for reading data.

func NewReadOnlyBuffer

func NewReadOnlyBuffer(n int) ReadOnlyBuffer

NewReadOnlyBuffer creates new ReadOnlyBuffer instance with buffer of n.

func NewReadOnlyBufferWithBytes

func NewReadOnlyBufferWithBytes(buf []byte) ReadOnlyBuffer

NewReadOnlyBufferWithBytes creates new instance of ReadOnlyBuffer with buf.

type WriteOnlyBuffer

type WriteOnlyBuffer interface {
	// WriteHeader writes slice p to fixed length header. The return value n is the length of
	// writting in to the header. If the length of p > the free space of header,
	// WriteHeader will return with ErrNoEnoughHeader
	WriteHeader(p []byte) (n int, err error)

	// TakeFreeHeader returns the free slice of n in header and mark as used.
	// For high performance, thinking of using TakeFreeHeader instead of WriteHeader method as much as possible.
	TakeFreeHeader(n int) ([]byte, error)

	// WriteTail appends the contents of p to the tail of the buffer, growing the buffer as
	// needed. The return value n is the length of p; err is always nil. If the
	// buffer becomes too large, WriteTail will panic with ErrTooLarge.
	WriteTail(p []byte) (n int, err error)

	// FreeTail returns the free memory of tail.
	FreeTail() ([]byte, int)

	// Bytes returns a slice of length len(b.buf) holding the unread portion of the buffer.
	// The slice is valid for use only until the next buffer modification (that is,
	// only until the next call to a method like WriteTail).
	// The slice aliases the buffer content at least until the next buffer modification,
	// so immediate changes to the slice will affect the result of future reads.
	Bytes() []byte

	Len() int

	// Reset resets the buffer to be empty,
	// but it retains the underlying storage for use by future writes.
	Reset()
}

WriteOnlyBuffer defines a buffer only used for easy-write and full-read. For write header, when create a new WriteOnlyBuffer, a header size need to be assigned.

func NewWriteOnlyBuffer

func NewWriteOnlyBuffer(hl uint) WriteOnlyBuffer

NewWriteOnlyBuffer creates a new WriteOnlyBuffer with header length with hl, and initial tail length with tl.

func NewWriteOnlyBufferWithBytes

func NewWriteOnlyBufferWithBytes(head uint, buf []byte) WriteOnlyBuffer

Jump to

Keyboard shortcuts

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