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 ¶
- Constants
- type LimitedReader
- func (lr *LimitedReader) Buffered() int
- func (lr *LimitedReader) Discard(n int) (discarded int, err error)
- func (lr *LimitedReader) DiscardUntil(c byte) (discarded int, err error)
- func (r *LimitedReader) Flush()
- func (lr *LimitedReader) Peek(n int) ([]byte, error)
- func (lr *LimitedReader) Read(p []byte) (n int, err error)
- func (lr *LimitedReader) ReadByte() (byte, error)
- func (lr *LimitedReader) ReadBytes(n int) ([]byte, error)
- func (r *LimitedReader) Reset(rd io.Reader)
- func (r *LimitedReader) ResetBytes(b []byte)
- func (r *LimitedReader) Rewind()
- func (r *LimitedReader) RingReader() *Reader
- func (r *LimitedReader) SetManualFlush(v bool)
- type Reader
- func (r *Reader) Buffered() int
- func (r *Reader) DebugDump(w io.Writer)
- func (r *Reader) Discard(n int) (discarded int, err error)
- func (r *Reader) DiscardUntil(c byte) (discarded int, err error)
- func (r *Reader) Flush()
- func (r *Reader) LimitReader(n int) *LimitedReader
- func (r *Reader) Peek(n int) ([]byte, error)
- func (r *Reader) Read(p []byte) (n int, err error)
- func (r *Reader) ReadByte() (byte, error)
- func (r *Reader) ReadBytes(n int) (b []byte, err error)
- func (r *Reader) Reader() io.Reader
- func (r *Reader) Reset(rd io.Reader)
- func (r *Reader) ResetBytes(b []byte)
- func (r *Reader) Rewind()
- func (r *Reader) RingReader() *Reader
- func (r *Reader) SetManualFlush(v bool)
- func (r *Reader) TotalRead() int
- type RingBuf
- func (rb *RingBuf) DebugDump(wr io.Writer)
- func (rb *RingBuf) FillFrom(r io.Reader) (n int64, err error)
- func (rb *RingBuf) Flush()
- func (rb *RingBuf) Peek(n int) (buf []byte, err error)
- func (rb *RingBuf) Read(p []byte) (n int, err error)
- func (rb *RingBuf) ReadByte() (byte, error)
- func (rb *RingBuf) ReadBytes(n int) (r []byte, err error)
- func (rb *RingBuf) ReadFrom(r io.Reader) (n int64, err error)
- func (rb *RingBuf) Reset()
- func (rb *RingBuf) Rewind()
- func (rb *RingBuf) SetManualFlush(v bool)
- func (rb *RingBuf) Write(p []byte) (n int, err error)
- type RingBufferReader
Examples ¶
Constants ¶
const ( // BufferSize is the logical size of the ring buffer. BufferSize = 4096 // SlackSize is reserved for potential slack operations. SlackSize = 4096 // 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) Flush ¶ added in v0.16.0
func (r *LimitedReader) Flush()
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)
func (*LimitedReader) Rewind ¶ added in v0.16.0
func (r *LimitedReader) Rewind()
func (*LimitedReader) RingReader ¶ added in v0.16.0
func (r *LimitedReader) RingReader() *Reader
func (*LimitedReader) SetManualFlush ¶ added in v0.16.0
func (r *LimitedReader) SetManualFlush(v bool)
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader wraps an io.Reader and uses a RingBuf for buffering.
func (*Reader) Discard ¶
Discard skips the next n bytes, returning the number of bytes discarded. It fills the buffer as needed.
func (*Reader) DiscardUntil ¶
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 ¶
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 ¶
Read implements io.Reader. It first ensures that there is data in the ring buffer by calling fill() as needed.
func (*Reader) ReadByte ¶
ReadByte implements io.ByteReader. It refills the ring buffer if necessary.
func (*Reader) ReadBytes ¶
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) ResetBytes ¶
func (*Reader) RingReader ¶ added in v0.16.0
func (*Reader) SetManualFlush ¶ added in v0.16.0
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) Flush ¶ added in v0.16.0
func (rb *RingBuf) Flush()
Flushes the read region, so that it can be overwritten.
func (*RingBuf) Peek ¶
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) ReadFrom ¶
ReadFrom implements io.ReaderFrom. It repeatedly calls FillFrom until an error occurs or the buffer is full.
func (*RingBuf) Rewind ¶ added in v0.16.0
func (rb *RingBuf) Rewind()
Rewinds the read data back to start.
func (*RingBuf) SetManualFlush ¶ added in v0.16.0
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)
RingReader() *Reader
}