ring

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 25, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MinRead is the minimum slice size passed to a Read call by
	// Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond
	// what is required to hold the contents of r, ReadFrom will not grow the
	// underlying buffer.
	MinRead = 512
	// DefaultBufferSize is the first-time allocation on a ring-buffer.
	DefaultBufferSize = 1024 // 1KB

)

Variables

View Source
var ErrIsEmpty = errors.New("ring-buffer is empty")

ErrIsEmpty will be returned when trying to read an empty ring-buffer.

Functions

func CeilToPowerOfTwo

func CeilToPowerOfTwo(n int) int

CeilToPowerOfTwo returns n if it is a power-of-two, otherwise the next-highest power-of-two.

func StringToBytes

func StringToBytes(s string) (b []byte)

StringToBytes converts string to a byte slice without memory allocation.

Note it may break if the implementation of string or slice header changes in the future go versions.

Types

type Buffer

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

Buffer is a circular buffer that implement io.ReaderWriter interface.

func New

func New(size int) *Buffer

New returns a new Buffer whose buffer has the given size.

func (*Buffer) Available

func (rb *Buffer) Available() int

Available returns the length of available bytes to write.

func (*Buffer) Buffered

func (rb *Buffer) Buffered() int

Buffered returns the length of available bytes to read.

func (*Buffer) Bytes

func (rb *Buffer) Bytes() []byte

Bytes returns all available read bytes. It does not move the read pointer and only copy the available data.

func (*Buffer) Cap

func (rb *Buffer) Cap() int

Cap returns the size of the underlying buffer.

func (*Buffer) CopyFromSocket

func (rb *Buffer) CopyFromSocket(fd int) (n int, err error)

CopyFromSocket copies data from a socket fd into ring-buffer.

func (*Buffer) Discard

func (rb *Buffer) Discard(n int) (discarded int, err error)

Discard skips the next n bytes by advancing the read pointer.

func (*Buffer) IsEmpty

func (rb *Buffer) IsEmpty() bool

IsEmpty tells if this ring-buffer is empty.

func (*Buffer) IsFull

func (rb *Buffer) IsFull() bool

IsFull tells if this ring-buffer is full.

func (*Buffer) Len

func (rb *Buffer) Len() int

Len returns the length of the underlying buffer.

func (*Buffer) Peek

func (rb *Buffer) Peek(n int) (head []byte, tail []byte)

Peek returns the next n bytes without advancing the read pointer, it returns all bytes when n <= 0.

func (*Buffer) Read

func (rb *Buffer) Read(p []byte) (n int, err error)

Read 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. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more. When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.

func (*Buffer) ReadByte

func (rb *Buffer) ReadByte() (b byte, err error)

ReadByte reads and returns the next byte from the input or ErrIsEmpty.

func (*Buffer) ReadFrom

func (rb *Buffer) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom implements io.ReaderFrom.

func (*Buffer) Reset

func (rb *Buffer) Reset()

Reset the read pointer and write pointer to zero.

func (*Buffer) Rewind

func (rb *Buffer) Rewind() (n int)

Rewind moves the data from its tail to head and rewind its pointers of read and write.

func (*Buffer) Write

func (rb *Buffer) Write(p []byte) (n int, err error)

Write writes len(p) bytes from p to the underlying buf. It returns the number of bytes written from p (n == len(p) > 0) and any error encountered that caused the write to stop early. If the length of p is greater than the writable capacity of this ring-buffer, it will allocate more memory to this ring-buffer. Write must not modify the slice data, even temporarily.

func (*Buffer) WriteByte

func (rb *Buffer) WriteByte(c byte) error

WriteByte writes one byte into buffer.

func (*Buffer) WriteString

func (rb *Buffer) WriteString(s string) (int, error)

WriteString writes the contents of the string s to buffer, which accepts a slice of bytes.

func (*Buffer) WriteTo

func (rb *Buffer) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo.

Jump to

Keyboard shortcuts

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