ringbuf

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Example
var rb RingBuf

buf := make([]byte, 1000)

fmt.Println("writing 5 x 1000 bytes")
fmt.Println(rb.Write(buf))
fmt.Println(rb.Write(buf))
fmt.Println(rb.Write(buf))
fmt.Println(rb.Write(buf))
fmt.Println(rb.Write(buf))

fmt.Println("reading 1000 and then writing 1000 more")
fmt.Println(rb.Read(buf))
fmt.Println(rb.Write(buf))

fmt.Println("reading 5 x 1000 bytes")
fmt.Println(rb.Read(buf))
fmt.Println(rb.Read(buf))
fmt.Println(rb.Read(buf))
fmt.Println(rb.Read(buf))
fmt.Println(rb.Read(buf))

fmt.Println("reading one additional time")
fmt.Println(rb.Read(buf))
Output:


writing 5 x 1000 bytes
1000 <nil>
1000 <nil>
1000 <nil>
1000 <nil>
96 <nil>
reading 1000 and then writing 1000 more
1000 <nil>
1000 <nil>
reading 5 x 1000 bytes
1000 <nil>
1000 <nil>
1000 <nil>
1000 <nil>
96 <nil>
reading one additional time
0 EOF

Index

Examples

Constants

View Source
const (
	// BufferSize is the logical size of the ring buffer.
	BufferSize = 4096
	// SlackSize is reserved for potential slack operations.
	SlackSize = 2048
	// TotalSize is the total size of the underlying array.
	TotalSize = BufferSize + SlackSize
)

Variables

This section is empty.

Functions

This section is empty.

Types

type LimitedReader

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

LimitedReader wraps a pointer to a Reader and limits the total number of bytes that can be read. The underlying Reader and the remaining limit are stored in unexported fields.

func (*LimitedReader) Buffered

func (lr *LimitedReader) Buffered() int

Buffered returns the number of unread bytes currently buffered, capped to the remaining limit.

func (*LimitedReader) Discard

func (lr *LimitedReader) Discard(n int) (discarded int, err error)

Discard skips the next n bytes, returning the number of bytes discarded.

func (*LimitedReader) DiscardUntil

func (lr *LimitedReader) DiscardUntil(c byte) (discarded int, err error)

DiscardUntil discards bytes until (but not including) the first occurrence of c. It returns the number of bytes discarded. If c is not found and no more data is available, it returns io.EOF.

func (*LimitedReader) Peek

func (lr *LimitedReader) Peek(n int) ([]byte, error)

Peek returns the next n unread bytes without advancing the read pointer. If n is greater than the remaining limit, it returns io.EOF.

func (*LimitedReader) Read

func (lr *LimitedReader) Read(p []byte) (n int, err error)

Read implements io.Reader, reading at most lr.n bytes.

func (*LimitedReader) ReadByte

func (lr *LimitedReader) ReadByte() (byte, error)

ReadByte implements io.ByteReader.

func (*LimitedReader) ReadBytes

func (lr *LimitedReader) ReadBytes(n int) ([]byte, error)

ReadBytes reads exactly n bytes from the buffered LimitedReader. If fewer than n bytes are available before reaching the limit, it returns io.EOF.

func (*LimitedReader) Reset

func (r *LimitedReader) Reset(rd io.Reader)

func (*LimitedReader) ResetBytes

func (r *LimitedReader) ResetBytes(b []byte)

type Reader

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

Reader wraps an io.Reader and uses a RingBuf for buffering.

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) Buffered

func (r *Reader) Buffered() int

Buffered returns the number of unread bytes currently buffered.

func (*Reader) Discard

func (r *Reader) Discard(n int) (discarded int, err error)

Discard skips the next n bytes, returning the number of bytes discarded. It fills the buffer as needed.

func (*Reader) DiscardUntil

func (r *Reader) DiscardUntil(c byte) (discarded int, err error)

DiscardUntil discards all bytes until (but not including) the first occurrence of c. It returns the number of bytes discarded. If c is not found and no more data is available, it returns io.EOF.

func (*Reader) LimitReader

func (r *Reader) LimitReader(n int) *LimitedReader

LimitReader returns a pointer to a LimitedReader wrapping the current Reader. It updates the limit if a LimitedReader already exists.

func (*Reader) Peek

func (r *Reader) Peek(n int) ([]byte, error)

Peek returns the next n unread bytes without advancing the read pointer. It refills the ring buffer until at least n bytes are available or an error occurs.

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read implements io.Reader. It first ensures that there is data in the ring buffer by calling fill() as needed.

func (*Reader) ReadByte

func (r *Reader) ReadByte() (byte, error)

ReadByte implements io.ByteReader. It refills the ring buffer if necessary.

func (*Reader) ReadBytes

func (r *Reader) ReadBytes(n int) (b []byte, err error)

ReadBytes reads exactly n bytes from the buffered Reader. It fills the buffer as needed; if fewer than n bytes are available, it returns io.EOF.

func (*Reader) Reset

func (r *Reader) Reset(rd io.Reader)

func (*Reader) ResetBytes

func (r *Reader) ResetBytes(b []byte)

type RingBuf

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

RingBuf is a fixed-size ring buffer that implements io.Reader and io.Writer. It maintains two cursors:

  • read: marks the beginning of the unread region
  • write: marks the end of the unread region (and beginning of free space)

The free region is defined as the region from write to read (cyclically).

func (*RingBuf) FillFrom

func (rb *RingBuf) FillFrom(r io.Reader) (n int64, err error)

FillFrom does one (1) read from an io.Reader into the free buffer.

func (*RingBuf) Peek

func (rb *RingBuf) Peek(n int) (buf []byte, err error)

Peek returns the next n unread bytes from the ring buffer without advancing the read pointer. If the requested data is contiguous, it returns a direct slice into rb.buf. If the data wraps around, it copies the wrapped portion into the slack space and returns a contiguous slice.

func (*RingBuf) Read

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

Read implements io.Reader.

func (*RingBuf) ReadByte

func (rb *RingBuf) ReadByte() (byte, error)

ReadByte implements io.ByteReader.

func (*RingBuf) ReadBytes

func (b *RingBuf) ReadBytes(n int) (r []byte, err error)

func (*RingBuf) ReadFrom

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

ReadFrom implements io.ReaderFrom. It repeatedly calls FillFrom until an error occurs or the buffer is full.

func (*RingBuf) Reset

func (rb *RingBuf) Reset()

func (*RingBuf) Write

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

Write implements io.Writer.

type RingBufferReader

type RingBufferReader interface {
	Buffered() int
	Reset(rd io.Reader)
	ResetBytes(b []byte)
	Read(p []byte) (n int, err error)
	ReadByte() (byte, error)
	Peek(n int) ([]byte, error)
	ReadBytes(n int) (b []byte, err error)
	Discard(n int) (discarded int, err error)
	DiscardUntil(c byte) (discarded int, err error)
}

Jump to

Keyboard shortcuts

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