Documentation ¶
Index ¶
- Variables
- func GetReader(bs []byte) *bytes.Reader
- func MaxSize() int
- func MinSize() int
- func Put(bb *Buffer)
- func PutReader(r *bytes.Reader)
- func Release(bb *Buffer) (ok bool)
- func RuntimeStats() map[string]uint64
- func SetCapacity(minSize, maxSize int)
- type Buffer
- func Clone(bb *Buffer) *Buffer
- func Get(capacity ...int) *Buffer
- func Make(capacity int) *Buffer
- func Make64(capacity uint64) *Buffer
- func MakeMax() *Buffer
- func MakeMin() *Buffer
- func New(size int) *Buffer
- func NewBuffer(buf []byte) *Buffer
- func NewBytes(bs []byte) *Buffer
- func NewString(s string) *Buffer
- func (bb *Buffer) Append(p []byte)
- func (bb *Buffer) AppendByte(c byte)
- func (bb *Buffer) AppendString(s string)
- func (bb *Buffer) Bytes() []byte
- func (bb *Buffer) Cap() int
- func (bb *Buffer) Clone() *Buffer
- func (bb *Buffer) Close() error
- func (bb *Buffer) Copy() []byte
- func (bb *Buffer) CopyTo(p []byte) int
- func (bb *Buffer) GetReader() *bytes.Reader
- func (bb *Buffer) Grow(n int)
- func (bb *Buffer) Guarantee(n int)
- func (bb *Buffer) Len() int
- func (bb *Buffer) Put()
- func (bb *Buffer) PutAll(r ...*bytes.Reader)
- func (bb *Buffer) PutReader(r *bytes.Reader) bool
- func (bb *Buffer) ReadFrom(r io.Reader) (int64, error)
- func (bb *Buffer) RefAdd(delta int64)
- func (bb *Buffer) RefDec()
- func (bb *Buffer) RefInc()
- func (bb *Buffer) RefReset()
- func (bb *Buffer) RefStore(val int64)
- func (bb *Buffer) RefSwapDec() (c int64)
- func (bb *Buffer) RefValue() int64
- func (bb *Buffer) Release() bool
- func (bb *Buffer) Reset()
- func (bb *Buffer) Set(p []byte)
- func (bb *Buffer) SetString(s string)
- func (bb *Buffer) String() string
- func (bb *Buffer) Truncate(n int)
- func (bb *Buffer) UnsafeString() string
- func (bb *Buffer) Write(p []byte) (int, error)
- func (bb *Buffer) WriteByte(c byte) error
- func (bb *Buffer) WriteString(s string) (int, error)
- func (bb *Buffer) WriteTo(w io.Writer) (int64, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrTooLarge = errors.New("buffer: too large") ErrTruncation = errors.New("buffer: truncation out of range") ErrGrow = errors.New("buffer: negative count") ErrClose = errors.New("buffer: failed to add it to the pool") )
var DefaultBufferSize = 64
DefaultBufferSize is an initial allocation minimal capacity.
Functions ¶
func Release ¶
Release put B back into the byte pool of the corresponding scale, and put the Buffer back into the buffer pool. Buffers smaller than the minimum capacity or larger than the maximum capacity are discarded.
func RuntimeStats ¶ added in v1.3.0
func SetCapacity ¶
func SetCapacity(minSize, maxSize int)
SetCapacity initialize to the default byte slice pool. Divide into multiple pools according to the capacity scale. Maximum range of byte slice pool: [2,math.MaxInt32]
Types ¶
type Buffer ¶
type Buffer struct { B []byte // contains filtered or unexported fields }
Buffer similar to bytes.Buffer, but provides finer-grained multiplexing of underlying byte slices. The zero value for Buffer is an empty buffer ready to use, but capacity will be 0. It is recommended to use pool to initialize a Buffer: e.g.::
bb := buffer.Get() // The initial capacity is 64 (DefaultBufferSize) bb := buffer.Make(8) // The initial capacity is 8
After use, put it back in the pool:
bb.Put() bb.Release()
Example ¶
package main import ( "fmt" "github.com/fufuok/bytespool/buffer" ) func main() { bb := buffer.Get() bb.SetString("1") _, _ = bb.WriteString("22") _, _ = bb.Write([]byte("333")) _ = bb.WriteByte('x') bb.Truncate(6) fmt.Printf("result=%q", bb.String()) // After use, put Buffer back in the pool. buffer.Put(bb) }
Output: result="122333"
func Make ¶
Make return a Buffer with a byte slice of length 0. Capacity will not be 0, max(capacity, defaultPools.bs.MinSize())
func New ¶ added in v1.2.1
New return byte slice of the specified size. Warning: may contain old data. Warning: returned buf is never equal to nil
func NewBuffer ¶ added in v1.2.1
NewBuffer similar to bytes.NewBuffer(buf []byte) Creates and initializes a new Buffer using buf as its initial contents. The new Buffer takes ownership of buf, and the caller should not use buf after this call. NewBuffer is intended to prepare a Buffer to read existing data. It can also be used to set the initial size of the internal buffer for writing.
func (*Buffer) AppendByte ¶ added in v1.2.3
AppendByte appends the byte c to Buffer.B.
func (*Buffer) AppendString ¶ added in v1.2.3
AppendString appends the s to Buffer.B.
func (*Buffer) Clone ¶
Clone returns a copy of the Buffer.B. Atomically reset the reference count to 0.
func (*Buffer) Grow ¶
Grow grows the internal buffer such that 'n' bytes can be written without reallocating. If n is negative, Grow will panic. If the buffer can't grow it will panic with ErrTooLarge.
func (*Buffer) Guarantee ¶ added in v1.2.1
Guarantee buffer will be guaranteed to have at least 'n' remaining capacity. If n is negative, Grow will panic. If the buffer can't grow it will panic with ErrTooLarge.
func (*Buffer) PutAll ¶ added in v1.2.2
PutAll put io.Reader and Buffer into the pool. The Buffer must be placed in the pool.
func (*Buffer) ReadFrom ¶
ReadFrom implements io.ReaderFrom.
The function appends all the data read from r to Buffer.B.
func (*Buffer) RefDec ¶
func (bb *Buffer) RefDec()
RefDec atomically decrement the reference count by 1.
func (*Buffer) RefInc ¶
func (bb *Buffer) RefInc()
RefInc atomically increment the reference count by 1.
func (*Buffer) RefReset ¶
func (bb *Buffer) RefReset()
RefReset atomically reset the reference count to 0.
func (*Buffer) RefSwapDec ¶
RefSwapDec atomically decrement the reference count by 1 and return the old value.
func (*Buffer) Release ¶
Release put B back into the byte pool of the corresponding scale, and put the Buffer back into the buffer pool. Buffers smaller than the minimum capacity or larger than the maximum capacity are discarded.
func (*Buffer) String ¶
String implements print.Stringer.
if the Buffer is a nil pointer, it returns "" instead of "<nil>"
func (*Buffer) Truncate ¶
Truncate buffer data, keep data of specified length. It panics if n is negative or greater than the length of the buffer.
func (*Buffer) UnsafeString ¶ added in v1.2.2
UnsafeString not immutable.
func (*Buffer) Write ¶
Write implements io.Writer.
The function appends all the data in p to Buffer.B. The returned error is always nil.
func (*Buffer) WriteByte ¶
WriteByte implements io.ByteWriter.
The function appends the byte c to Buffer.B. The returned error is always nil.
func (*Buffer) WriteString ¶
WriteString implements io.StringWriter.
The function appends the s to Buffer.B. The returned error is always nil.