Documentation ¶
Index ¶
- Variables
- func PutInPool(b *RingBuffer)
- type Pool
- type RingBuffer
- func (r *RingBuffer) Bytes() (buf []byte)
- func (r *RingBuffer) Capacity() int
- func (r *RingBuffer) IsEmpty() bool
- func (r *RingBuffer) IsFull() bool
- func (r *RingBuffer) Length() int
- func (r *RingBuffer) Peek(len int) (first []byte, end []byte)
- func (r *RingBuffer) PeekAll() (first []byte, end []byte)
- func (r *RingBuffer) PeekUint16() uint16
- func (r *RingBuffer) PeekUint32() uint32
- func (r *RingBuffer) PeekUint64() uint64
- func (r *RingBuffer) PeekUint8() uint8
- func (r *RingBuffer) Read(p []byte) (n int, err error)
- func (r *RingBuffer) ReadByte() (b byte, err error)
- func (r *RingBuffer) Reset()
- func (r *RingBuffer) Retrieve(len int)
- func (r *RingBuffer) RetrieveAll()
- func (r *RingBuffer) String() string
- func (r *RingBuffer) VirtualFlush()
- func (r *RingBuffer) VirtualLength() int
- func (r *RingBuffer) VirtualRead(p []byte) (n int, err error)
- func (r *RingBuffer) VirtualRevert()
- func (r *RingBuffer) WithData(data []byte)
- func (r *RingBuffer) Write(p []byte) (n int, err error)
- func (r *RingBuffer) WriteByte(c byte) error
- func (r *RingBuffer) WriteString(s string) (n int, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrIsEmpty = errors.New("ring buffer is empty")
ErrIsEmpty 缓冲区为空
Functions ¶
func PutInPool ¶ added in v0.0.7
func PutInPool(b *RingBuffer)
PutInPool returns byte buffer to the pool.
ByteBuffer.B mustn't be touched after returning it to the pool. Otherwise data races will occur.
Types ¶
type Pool ¶ added in v0.0.7
type Pool struct {
// contains filtered or unexported fields
}
Pool represents byte buffer pool.
Distinct pools may be used for distinct types of byte buffers. Properly determined byte buffer types with their own pools may help reducing memory waste.
func (*Pool) Get ¶ added in v0.0.7
func (p *Pool) Get() *RingBuffer
Get returns new byte buffer with zero length.
The byte buffer may be returned to the pool via Put after the use in order to minimize GC overhead.
func (*Pool) Put ¶ added in v0.0.7
func (p *Pool) Put(b *RingBuffer)
Put releases byte buffer obtained via Get to the pool.
The buffer mustn't be accessed after returning to the pool.
type RingBuffer ¶
type RingBuffer struct {
// contains filtered or unexported fields
}
RingBuffer 自动扩容循环缓冲区
Example ¶
rb := New(1024) _, _ = rb.Write([]byte("abcd")) fmt.Println(rb.Length()) fmt.Println(rb.free()) buf := make([]byte, 4) _, _ = rb.Read(buf) fmt.Println(string(buf)) rb.Write([]byte("1234567890")) rb.VirtualRead(buf) fmt.Println(string(buf)) fmt.Println(rb.Length()) fmt.Println(rb.VirtualLength()) rb.VirtualFlush() fmt.Println(rb.Length()) fmt.Println(rb.VirtualLength()) rb.VirtualRead(buf) fmt.Println(string(buf)) fmt.Println(rb.Length()) fmt.Println(rb.VirtualLength()) rb.VirtualRevert() fmt.Println(rb.Length()) fmt.Println(rb.VirtualLength())
Output: 4 1020 abcd 1234 10 6 6 6 5678 6 2 6 6
func GetFromPool ¶ added in v0.0.7
func GetFromPool() *RingBuffer
GetFromPool returns an empty byte buffer from the pool.
Got byte buffer may be returned to the pool via Put call. This reduces the number of memory allocations required for byte buffer management.
func NewWithData ¶
func NewWithData(data []byte) *RingBuffer
NewWithData 特殊场景使用,RingBuffer 会持有data,不会自己申请内存去拷贝
func (*RingBuffer) Bytes ¶
func (r *RingBuffer) Bytes() (buf []byte)
Bytes 返回所有可读数据,此操作不会移动读指针,仅仅是拷贝全部数据
func (*RingBuffer) Capacity ¶
func (r *RingBuffer) Capacity() int
func (*RingBuffer) IsEmpty ¶
func (r *RingBuffer) IsEmpty() bool
func (*RingBuffer) IsFull ¶
func (r *RingBuffer) IsFull() bool
func (*RingBuffer) Length ¶
func (r *RingBuffer) Length() int
func (*RingBuffer) PeekAll ¶
func (r *RingBuffer) PeekAll() (first []byte, end []byte)
func (*RingBuffer) PeekUint16 ¶ added in v0.0.5
func (r *RingBuffer) PeekUint16() uint16
func (*RingBuffer) PeekUint32 ¶ added in v0.0.5
func (r *RingBuffer) PeekUint32() uint32
func (*RingBuffer) PeekUint64 ¶ added in v0.0.5
func (r *RingBuffer) PeekUint64() uint64
func (*RingBuffer) PeekUint8 ¶ added in v0.0.5
func (r *RingBuffer) PeekUint8() uint8
func (*RingBuffer) ReadByte ¶
func (r *RingBuffer) ReadByte() (b byte, err error)
func (*RingBuffer) Reset ¶
func (r *RingBuffer) Reset()
func (*RingBuffer) Retrieve ¶
func (r *RingBuffer) Retrieve(len int)
func (*RingBuffer) RetrieveAll ¶
func (r *RingBuffer) RetrieveAll()
func (*RingBuffer) String ¶
func (r *RingBuffer) String() string
func (*RingBuffer) VirtualFlush ¶
func (r *RingBuffer) VirtualFlush()
VirtualFlush 刷新虚读指针 VirtualXXX 系列配合使用
func (*RingBuffer) VirtualLength ¶
func (r *RingBuffer) VirtualLength() int
VirtualLength 虚拟长度,虚读后剩余可读数据长度 VirtualXXX 系列配合使用
func (*RingBuffer) VirtualRead ¶
func (r *RingBuffer) VirtualRead(p []byte) (n int, err error)
VirtualRead 虚读,不移动 read 指针,需要配合 VirtualFlush 和 VirtualRevert 使用 VirtualXXX 系列配合使用
func (*RingBuffer) VirtualRevert ¶
func (r *RingBuffer) VirtualRevert()
VirtualRevert 还原虚读指针 VirtualXXX 系列配合使用
func (*RingBuffer) WithData ¶ added in v0.0.8
func (r *RingBuffer) WithData(data []byte)
func (*RingBuffer) WriteByte ¶
func (r *RingBuffer) WriteByte(c byte) error
func (*RingBuffer) WriteString ¶
func (r *RingBuffer) WriteString(s string) (n int, err error)