Documentation
¶
Index ¶
- Variables
- func MaxSize() int
- func MinSize() int
- func Put(bb *Buffer)
- func Release(bb *Buffer) (ok bool)
- func SetCapacity(minSize, maxSize int)
- type Buffer
- func (bb *Buffer) Bytes() []byte
- func (bb *Buffer) Cap() int
- func (bb *Buffer) Clone() *Buffer
- func (bb *Buffer) Close() error
- func (bb *Buffer) Grow(n int)
- func (bb *Buffer) Len() int
- func (bb *Buffer) Put()
- func (bb *Buffer) Read(p []byte) (n int, err error)
- 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) 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 is an initial allocation minimal capacity.
DefaultBufferSize = 64
)
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 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,1<<31]
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 (*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) Read ¶
Read implements io.Reader.
The function copies data from Buffer.B to p. The return value n is the number of bytes read, error is always nil.
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) 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.