mirror of
https://github.com/valyala/bytebufferpool.git
synced 2026-06-15 13:36:59 +03:00
bf51c4698b
* Move some methods to pool.go * Use method instead of manual resetting of buffer * Fix typo
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package bytebufferpool
|
|
|
|
// ByteBuffer provides byte buffer, which can be used for minimizing
|
|
// memory allocations.
|
|
//
|
|
// ByteBuffer 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.
|
|
type ByteBuffer struct {
|
|
|
|
// B is a byte buffer to use in append-like workloads.
|
|
// See example code for details.
|
|
B []byte
|
|
}
|
|
|
|
// Bytes returns b.B, i.e. all the bytes accumulated in the buffer.
|
|
//
|
|
// The purpose of this function is bytes.Buffer compatibility.
|
|
func (b *ByteBuffer) Bytes() []byte {
|
|
return b.B
|
|
}
|
|
|
|
// Write implements io.Writer - it appends p to ByteBuffer.B
|
|
func (b *ByteBuffer) Write(p []byte) (int, error) {
|
|
b.B = append(b.B, p...)
|
|
return len(p), nil
|
|
}
|
|
|
|
// WriteByte appends the byte c to the buffer.
|
|
//
|
|
// The purpose of this function is bytes.Buffer compatibility.
|
|
//
|
|
// The function always returns nil.
|
|
func (b *ByteBuffer) WriteByte(c byte) error {
|
|
b.B = append(b.B, c)
|
|
return nil
|
|
}
|
|
|
|
// WriteString appends s to ByteBuffer.B
|
|
func (b *ByteBuffer) WriteString(s string) (int, error) {
|
|
b.B = append(b.B, s...)
|
|
return len(s), nil
|
|
}
|
|
|
|
// Set sets ByteBuffer.B to p
|
|
func (b *ByteBuffer) Set(p []byte) {
|
|
b.B = append(b.B[:0], p...)
|
|
}
|
|
|
|
// SetString sets ByteBuffer.B to s
|
|
func (b *ByteBuffer) SetString(s string) {
|
|
b.B = append(b.B[:0], s...)
|
|
}
|
|
|
|
// Reset makes ByteBuffer.B empty.
|
|
func (b *ByteBuffer) Reset() {
|
|
b.B = b.B[:0]
|
|
}
|