ring

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ring implements a memory-efficient circular buffer that implements the io.Reader and io.Writer interfaces.

Derived from gnet/pkg/buffer/ring (https://github.com/panjf2000/gnet), licensed under MIT.

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 initial allocation size for a ring buffer.
	DefaultBufferSize = 1024 // 1KB

)

Variables

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

ErrIsEmpty is returned when trying to read from an empty ring buffer.

Functions

This section is empty.

Types

type Buffer

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

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

func New

func New(size int) *Buffer

New returns a new Buffer whose buffer has the given size. The actual capacity is rounded up to the nearest power of two.

func (*Buffer) Available

func (rb *Buffer) Available() int

Available returns the number of bytes available to write.

func (*Buffer) Buffered

func (rb *Buffer) Buffered() int

Buffered returns the number of bytes available 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 copies the available data into a new slice.

func (*Buffer) Cap

func (rb *Buffer) Cap() int

Cap returns the capacity of the underlying 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 reports whether the ring buffer is empty.

func (*Buffer) IsFull

func (rb *Buffer) IsFull() bool

IsFull reports whether the 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. The returned slices may point to two separate regions (head and tail) when the data wraps around the end of the buffer.

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.

If the buffer is empty, Read returns 0, ErrIsEmpty.

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 resets the read pointer and write pointer to zero.

func (*Buffer) Write

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

Write writes len(p) bytes from p to the underlying buffer. It returns the number of bytes written (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, the buffer will grow to accommodate the data.

func (*Buffer) WriteByte

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

WriteByte writes one byte into the buffer.

func (*Buffer) WriteString

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

WriteString writes the contents of the string s to the buffer.

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