ring

package
v0.13.0-beta2 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2021 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package ring provides an efficient ring buffer for bytes.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBufferFull is the error returned by Buffer if a storage operation
	// can't be completed due to a lack of space in the buffer.
	ErrBufferFull = errors.New("buffer full")
)

Functions

This section is empty.

Types

type Buffer

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

Buffer is a fixed-size ring buffer for storing bytes. Its behavior is designed to match that of bytes.Buffer as closely as possible. The zero value for Buffer is a buffer with zero capacity.

func NewBuffer

func NewBuffer(size int) *Buffer

NewBuffer creates a new ring buffer with the specified size. If size is less than or equal to 0, then a buffer with zero capacity is created.

func (*Buffer) Free

func (b *Buffer) Free() int

Free returns the unused buffer capacity.

func (*Buffer) Read

func (b *Buffer) Read(buffer []byte) (int, error)

Read implements io.Reader.Read.

func (*Buffer) ReadByte

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

ReadByte implements io.ByteReader.ReadByte.

func (*Buffer) ReadNFrom

func (b *Buffer) ReadNFrom(reader io.Reader, n int) (int, error)

ReadNFrom is similar to using io.ReaderFrom.ReadFrom with io.LimitedReader, but it is designed to support a limited-capacity buffer, which can't reliably detect EOF without potentially wasting data from the stream. In particular, Buffer can't reliably detect the case that EOF is reached right as its storage is filled because io.Reader is not required to return io.EOF until the next call, and most implementations (including io.LimitedReader) will only return io.EOF on a subsequent call. Moreover, io.Reader isn't required to return an EOF indication on a zero-length read, so even a follow-up zero-length read can't be used to reliably detect EOF. As such, this method provides a more explicit definition of the number of bytes to read, and it will return io.EOF if encountered, unless it occurs simultaneously with request completion.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset clears all data within the buffer.

func (*Buffer) Size

func (b *Buffer) Size() int

Size returns the size of the buffer.

func (*Buffer) Used

func (b *Buffer) Used() int

Used returns how many bytes currently reside in the buffer.

func (*Buffer) Write

func (b *Buffer) Write(data []byte) (int, error)

Write implements io.Writer.Write.

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(value byte) error

WriteByte implements io.ByteWriter.WriteByte.

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(writer io.Writer) (int64, error)

WriteTo implements io.WriterTo.WriteTo.

Jump to

Keyboard shortcuts

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