1 // Package bufpool is a freelist for bytes.Buffer objects.
9 var pool = &sync.Pool{New: func() interface{} { return bytes.NewBuffer(nil) }}
11 // Get returns an initialized bytes.Buffer object.
12 // It is like new(bytes.Buffer) except it uses the free list.
13 // The caller should call Put when finished with the returned object.
14 // Since Buffer.Bytes() returns the buffer's underlying slice,
15 // it is not safe for that slice to escape the caller.
16 // If the bytes need to escape, CopyBytes should be used.
17 func Get() *bytes.Buffer {
18 return pool.Get().(*bytes.Buffer)
21 // Put resets the buffer and adds it to the freelist.
22 func Put(b *bytes.Buffer) {
27 // CopyBytes returns a copy of the bytes contained in the buffer.
28 // This slice is safe from updates in the underlying buffer,
29 // allowing the buffer to be placed back in the free list.
30 func CopyBytes(buf *bytes.Buffer) []byte {
32 b2 := make([]byte, len(b))