buffer

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 6 Imported by: 1

README

buffer

import "github.com/fufuok/bytespool/buffer"

Index

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
)

func MaxSize

func MaxSize() int

func MinSize

func MinSize() int

func Put

func Put(bb *Buffer)

Put is the same as b.Release.

func Release

func Release(bb *Buffer) (ok bool)

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]

type Buffer

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()

type Buffer struct {
    B []byte
    // contains filtered or unexported fields
}
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 Clone
func Clone(bb *Buffer) *Buffer

Clone returns a copy of the Buffer.B. Atomically reset the reference count to 0.

func Get
func Get(capacity ...int) *Buffer
func Make
func Make(capacity int) *Buffer

Make return a Buffer with a byte slice of length 0. Capacity will not be 0, max(capacity, defaultPools.bs.MinSize())

func Make64
func Make64(capacity uint64) *Buffer
func MakeMax
func MakeMax() *Buffer
func MakeMin
func MakeMin() *Buffer
func New
func New(size int) *Buffer

New return byte slice of the specified size. Warning: may contain old data. Warning: returned buf is never equal to nil

func NewBuffer
func NewBuffer(buf []byte) *Buffer

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 NewBytes
func NewBytes(bs []byte) *Buffer

NewBytes returns a byte slice of the specified content.

func NewString
func NewString(s string) *Buffer

NewString returns a byte slice of the specified content.

func (*Buffer) Bytes
func (bb *Buffer) Bytes() []byte
func (*Buffer) Cap
func (bb *Buffer) Cap() int
func (*Buffer) Clone
func (bb *Buffer) Clone() *Buffer

Clone returns a copy of the Buffer.B. Atomically reset the reference count to 0.

func (*Buffer) Close
func (bb *Buffer) Close() error

Close implements io.Closer.

func (*Buffer) Copy
func (bb *Buffer) Copy() []byte
func (*Buffer) Grow
func (bb *Buffer) Grow(n int)

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
func (bb *Buffer) Guarantee(n int)

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) Len
func (bb *Buffer) Len() int
func (*Buffer) Put
func (bb *Buffer) Put()

Put is the same as b.Release.

func (*Buffer) Read
func (bb *Buffer) Read(p []byte) (n int, err error)

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
func (bb *Buffer) ReadFrom(r io.Reader) (int64, error)

ReadFrom implements io.ReaderFrom.

The function appends all the data read from r to Buffer.B.

func (*Buffer) RefAdd
func (bb *Buffer) RefAdd(delta int64)
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) RefStore
func (bb *Buffer) RefStore(val int64)

RefStore atomically stores val into the reference count.

func (*Buffer) RefSwapDec
func (bb *Buffer) RefSwapDec() (c int64)

RefSwapDec atomically decrement the reference count by 1 and return the old value.

func (*Buffer) RefValue
func (bb *Buffer) RefValue() int64

RefValue atomically loads the reference count.

func (*Buffer) Release
func (bb *Buffer) Release() bool

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) Reset
func (bb *Buffer) Reset()

Reset is the same as Truncate(0).

func (*Buffer) Set
func (bb *Buffer) Set(p []byte)

Set sets Buffer.B to p.

func (*Buffer) SetString
func (bb *Buffer) SetString(s string)

SetString sets Buffer.B to s.

func (*Buffer) String
func (bb *Buffer) String() string

String implements print.Stringer.

if the Buffer is a nil pointer, it returns "" instead of "<nil>"

func (*Buffer) Truncate
func (bb *Buffer) Truncate(n int)

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
func (bb *Buffer) Write(p []byte) (int, error)

Write implements io.Writer.

The function appends all the data in p to Buffer.B. The returned error is always nil.

func (*Buffer) WriteByte
func (bb *Buffer) WriteByte(c byte) error

WriteByte implements io.ByteWriter.

The function appends the byte c to Buffer.B. The returned error is always nil.

func (*Buffer) WriteString
func (bb *Buffer) WriteString(s string) (int, error)

WriteString implements io.StringWriter.

The function appends the s to Buffer.B. The returned error is always nil.

func (*Buffer) WriteTo
func (bb *Buffer) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo.

Generated by gomarkdoc

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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")
)
View Source
var (
	// DefaultBufferSize is an initial allocation minimal capacity.
	DefaultBufferSize = 64
)

Functions

func MaxSize

func MaxSize() int

func MinSize

func MinSize() int

func Put

func Put(bb *Buffer)

Put is the same as b.Release.

func Release

func Release(bb *Buffer) (ok bool)

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 Clone

func Clone(bb *Buffer) *Buffer

Clone returns a copy of the Buffer.B. Atomically reset the reference count to 0.

func Get

func Get(capacity ...int) *Buffer

func Make

func Make(capacity int) *Buffer

Make return a Buffer with a byte slice of length 0. Capacity will not be 0, max(capacity, defaultPools.bs.MinSize())

func Make64

func Make64(capacity uint64) *Buffer

func MakeMax

func MakeMax() *Buffer

func MakeMin

func MakeMin() *Buffer

func New added in v1.2.1

func New(size int) *Buffer

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

func NewBuffer(buf []byte) *Buffer

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 NewBytes

func NewBytes(bs []byte) *Buffer

NewBytes returns a byte slice of the specified content.

func NewString

func NewString(s string) *Buffer

NewString returns a byte slice of the specified content.

func (*Buffer) Bytes

func (bb *Buffer) Bytes() []byte

func (*Buffer) Cap

func (bb *Buffer) Cap() int

func (*Buffer) Clone

func (bb *Buffer) Clone() *Buffer

Clone returns a copy of the Buffer.B. Atomically reset the reference count to 0.

func (*Buffer) Close

func (bb *Buffer) Close() error

Close implements io.Closer.

func (*Buffer) Copy added in v1.2.1

func (bb *Buffer) Copy() []byte

func (*Buffer) Grow

func (bb *Buffer) Grow(n int)

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

func (bb *Buffer) Guarantee(n int)

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) Len

func (bb *Buffer) Len() int

func (*Buffer) Put

func (bb *Buffer) Put()

Put is the same as b.Release.

func (*Buffer) Read

func (bb *Buffer) Read(p []byte) (n int, err error)

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

func (bb *Buffer) ReadFrom(r io.Reader) (int64, error)

ReadFrom implements io.ReaderFrom.

The function appends all the data read from r to Buffer.B.

func (*Buffer) RefAdd

func (bb *Buffer) RefAdd(delta int64)

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) RefStore

func (bb *Buffer) RefStore(val int64)

RefStore atomically stores val into the reference count.

func (*Buffer) RefSwapDec

func (bb *Buffer) RefSwapDec() (c int64)

RefSwapDec atomically decrement the reference count by 1 and return the old value.

func (*Buffer) RefValue

func (bb *Buffer) RefValue() int64

RefValue atomically loads the reference count.

func (*Buffer) Release

func (bb *Buffer) Release() bool

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) Reset

func (bb *Buffer) Reset()

Reset is the same as Truncate(0).

func (*Buffer) Set

func (bb *Buffer) Set(p []byte)

Set sets Buffer.B to p.

func (*Buffer) SetString

func (bb *Buffer) SetString(s string)

SetString sets Buffer.B to s.

func (*Buffer) String

func (bb *Buffer) String() string

String implements print.Stringer.

if the Buffer is a nil pointer, it returns "" instead of "<nil>"

func (*Buffer) Truncate

func (bb *Buffer) Truncate(n int)

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

func (bb *Buffer) Write(p []byte) (int, error)

Write implements io.Writer.

The function appends all the data in p to Buffer.B. The returned error is always nil.

func (*Buffer) WriteByte

func (bb *Buffer) WriteByte(c byte) error

WriteByte implements io.ByteWriter.

The function appends the byte c to Buffer.B. The returned error is always nil.

func (*Buffer) WriteString

func (bb *Buffer) WriteString(s string) (int, error)

WriteString implements io.StringWriter.

The function appends the s to Buffer.B. The returned error is always nil.

func (*Buffer) WriteTo

func (bb *Buffer) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL