Documentation ¶
Index ¶
- Constants
- Variables
- func GetBytes(size int) *[]byte
- func Put(b *Buffer)
- func PutBytes(buf *[]byte)
- func PutIoBuffer(buf IoBuffer) error
- type Buffer
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Cap() int
- func (b *Buffer) Len() int
- func (b *Buffer) ReadFrom(r io.Reader) (int64, error)
- func (b *Buffer) Reset()
- func (b *Buffer) Set(p []byte)
- func (b *Buffer) SetString(s string)
- func (b *Buffer) String() string
- func (b *Buffer) TrimNewline()
- func (b *Buffer) Write(p []byte) (int, error)
- func (b *Buffer) WriteBool(v bool)
- func (b *Buffer) WriteByte(c byte) error
- func (b *Buffer) WriteFloat(f float64, bitSize int)
- func (b *Buffer) WriteInt(n int64)
- func (b *Buffer) WriteString(s string) (int, error)
- func (b *Buffer) WriteTo(w io.Writer) (int64, error)
- func (b *Buffer) WriteUint(n uint64)
- type IoBuffer
- type IoBufferPool
- type Pool
Examples ¶
Constants ¶
const DefaultSize = 1 << 4
const MaxRead = 1 << 17
const MinRead = 1 << 9
const ResetOffMark = -1
Variables ¶
Functions ¶
func Put ¶
func Put(b *Buffer)
Put returns byte buffer to the pool.
Buffer.B mustn't be touched after returning it to the pool. Otherwise data races will occur.
func PutIoBuffer ¶ added in v0.0.2
PutIoBuffer returns IoBuffer to pool
Types ¶
type Buffer ¶
type Buffer struct { // B is a byte buffer to use in append-like workloads. // See example code for details. B []byte }
Buffer provides byte buffer, which can be used for minimizing memory allocations.
Buffer may be used with functions appending data to the given []byte slice. See example code for details.
Use Get for obtaining an empty byte buffer.
Example ¶
bb := Get() bb.WriteString("first line\n") bb.Write([]byte("second line\n")) bb.B = append(bb.B, "third line\n"...) fmt.Printf("bytebuffer contents=%q", bb.B) // It is safe to release byte buffer now, since it is // no longer used. Put(bb)
Output:
func Get ¶
func Get() *Buffer
Get 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 (*Buffer) Bytes ¶
Bytes returns b.B, i.e. all the bytes accumulated in the buffer.
The purpose of this function is bytes.Buffer compatibility.
func (*Buffer) ReadFrom ¶
ReadFrom implements io.ReaderFrom.
The function appends all the data read from r to b.
func (*Buffer) TrimNewline ¶
func (b *Buffer) TrimNewline()
TrimNewline trims any final "\n" byte from the end of the buffer.
func (*Buffer) WriteByte ¶
WriteByte appends the byte c to the buffer.
The purpose of this function is bytes.Buffer compatibility.
The function always returns nil.
func (*Buffer) WriteFloat ¶
func (*Buffer) WriteString ¶
WriteString appends s to Buffer.B.
type IoBuffer ¶ added in v0.0.2
type IoBuffer interface { //Read reads next len(buf) bytes from the buffer, util the buffer is drained Read(p []byte) (int, error) ReadFrom(r io.Reader) (int64, error) ReadOnce(r io.Reader, duration time.Duration) (int64, error) Write(b []byte) (int, error) WriteString(s string) (n int, err error) WriteTo(w io.Writer) (n int64, err error) Peek(n int) []byte Bytes() []byte Drain(offset int) Alloc(int) Free() Len() int Cap() int Reset() Clone() IoBuffer String() string Count(int32) int32 EOF() bool SetEOF(eof bool) }
func GetIoBuffer ¶ added in v0.0.2
GetIoBuffer returns IoBuffer from pool
func NewIoBuffer ¶ added in v0.0.2
func NewIoBufferBytes ¶ added in v0.0.2
func NewIoBufferEOF ¶ added in v0.0.2
func NewIoBufferEOF() IoBuffer
func NewIoBufferString ¶ added in v0.0.2
type IoBufferPool ¶ added in v0.0.2
type IoBufferPool struct {
// contains filtered or unexported fields
}
IoBufferPool is Iobuffer Pool
type Pool ¶
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.