Documentation
¶
Index ¶
- Variables
- func GetBytes(length int) []byte
- func PutBuffer(buffer *Buffer)
- func PutBytes(bytes []byte)
- type Buffer
- func (buffer *Buffer) Bytes() []byte
- func (buffer *Buffer) Cap() int
- func (buffer *Buffer) Grow(n int)
- func (buffer *Buffer) Len() int
- func (buffer *Buffer) Read(p []byte) (int, error)
- func (buffer *Buffer) ReadFrom(r io.Reader) (int64, error)
- func (buffer *Buffer) Reset()
- func (buffer *Buffer) Write(p []byte) (int, error)
- func (buffer *Buffer) WriteString(str string) (int, error)
- func (buffer *Buffer) WriteTo(w io.Writer) (int64, error)
- type BytesPool
- type Pool
- type SizedBytesPool
- type SizedBytesPoolFactory
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultBufferMinGrowLength is the default value of Buffer.MinGrowLength. DefaultBufferMinGrowLength = 64 // DefaultBufferReadBuffLength is the default value of Buffer.ReadBuffLength. DefaultBufferReadBuffLength = 512 // DefaultBufferReserveLength is the default value of Buffer.. DefaultBufferReserveLength = 1024 // BufferPool is the pool of Buffer instance. BufferPool Pool = &sync.Pool{ New: func() interface{} { return new(Buffer) }, } )
var ( // DefaultBytesPool is the default instance of BytesPool. DefaultBytesPool = &BytesPool{} // EmptyBytes represents empty bytes EmptyBytes = make([]byte, 0) // DefaultSizedBytesPoolFactory is a default factory for producing SizedBytesPool instance. DefaultSizedBytesPoolFactory = func(size int) Pool { return &sync.Pool{ New: func() interface{} { return make([]byte, 0, size) }, } } )
Functions ¶
Types ¶
type Buffer ¶
type Buffer struct {
// BytesPool is a pool of bytes of buffer.
// default is DefaultBytesPool.
BytesPool SizedBytesPool
// MinGrowLength is a minimum length of the buffer growth.
// default is BufferDefaultMinGrowLength.
MinGrowLength int
// ReadBuffLength is a buff length of the Buffer.ReadFrom.
// default value is BufferDefaultReadBuffLength.
ReadBuffLength int
// ReserveLength represents the buffer will reserve the bytes
// if the capacity of the bytes is equal to less than this value.
// default is DefaultBufferReserveLength.
ReserveLength int
// contains filtered or unexported fields
}
Buffer get bytes from pool and put idle bytes to pool.
Example ¶
package main
import (
"fmt"
"github.com/wencan/bytespool"
)
func main() {
buffer := bytespool.GetBuffer()
defer bytespool.PutBuffer(buffer)
_, err := buffer.Write([]byte{0, 1, 2, 3})
_ = err
fmt.Printf("len: %d, cap: %d\n", buffer.Len(), buffer.Cap())
// auto growth
_, err = buffer.Write([]byte{4, 5, 6, 7, 8, 9})
_ = err
fmt.Printf("len: %d, cap: %d\n", buffer.Len(), buffer.Cap())
// read
buff := bytespool.GetBytes(10)
defer bytespool.PutBytes(buff)
nRead, err := buffer.Read(buff)
_ = err
buff = buff[:nRead]
fmt.Printf("data: %X", buff)
}
Output: len: 4, cap: 64 len: 10, cap: 64 data: 00010203040506070809
func (*Buffer) Grow ¶
Grow grows the buffer's capacity. After Grow(n), at least n bytes can be written to the buffer without another allocation.
func (*Buffer) Read ¶
Read reads the next len(p) bytes from the buffer or until the buffer is drained. The return first value is the number of bytes read. If the buffer has no data to return, return 0, io.EOF.
func (*Buffer) ReadFrom ¶
ReadFrom reads data from r until EOF and appends it to the buffer, growing the buffer as needed.
func (*Buffer) Reset ¶
func (buffer *Buffer) Reset()
Reset release bytes and reset the buffer status.
func (*Buffer) WriteString ¶
WriteString appends the contents of str to the buffer, growing the buffer as needed. The return first value is the length of str.
type BytesPool ¶
type BytesPool struct {
// SizedPoolFactory is a factory for producing SizedBytesPool instance.
// default is DefaultSizedBytesPoolFactory.
SizedPoolFactory SizedBytesPoolFactory
// contains filtered or unexported fields
}
BytesPool represents bytes pool
type Pool ¶
type Pool interface {
Get() interface{}
Put(x interface{})
}
Pool is the interface of the universal pool.
type SizedBytesPool ¶
SizedBytesPool is a interface that represents a pool of sized bytes.
type SizedBytesPoolFactory ¶
SizedBytesPoolFactory creates the pool for the bytes that length is fixed.