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
- Variables
- type Buffer
- func (rb *Buffer) Available() int
- func (rb *Buffer) Buffered() int
- func (rb *Buffer) Bytes() []byte
- func (rb *Buffer) Cap() int
- func (rb *Buffer) Discard(n int) (discarded int, err error)
- func (rb *Buffer) IsEmpty() bool
- func (rb *Buffer) IsFull() bool
- func (rb *Buffer) Len() int
- func (rb *Buffer) Peek(n int) (head []byte, tail []byte)
- func (rb *Buffer) Read(p []byte) (n int, err error)
- func (rb *Buffer) ReadByte() (b byte, err error)
- func (rb *Buffer) ReadFrom(r io.Reader) (n int64, err error)
- func (rb *Buffer) Reset()
- func (rb *Buffer) Write(p []byte) (n int, err error)
- func (rb *Buffer) WriteByte(c byte) error
- func (rb *Buffer) WriteString(s string) (int, error)
- func (rb *Buffer) WriteTo(w io.Writer) (int64, error)
Constants ¶
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 ¶
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 ¶
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) Bytes ¶
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) Peek ¶
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 ¶
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) Reset ¶
func (rb *Buffer) Reset()
Reset resets the read pointer and write pointer to zero.
func (*Buffer) Write ¶
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) WriteString ¶
WriteString writes the contents of the string s to the buffer.