buffer

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2019 License: BSD-3-Clause Imports: 9 Imported by: 5

README

buffer

Documentation

Index

Examples

Constants

View Source
const DefaultSize = 1 << 4
View Source
const MaxRead = 1 << 17
View Source
const MinRead = 1 << 9
View Source
const ResetOffMark = -1

Variables

View Source
var (
	EOF                  = errors.New("EOF")
	ErrTooLarge          = errors.New("io buffer: too large")
	ErrNegativeCount     = errors.New("io buffer: negative count")
	ErrInvalidWriteCount = errors.New("io buffer: invalid write count")
)

Functions

func GetBytes added in v0.0.2

func GetBytes(size int) *[]byte

GetBytes returns *[]byte from byteBufferPool

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 PutBytes added in v0.0.2

func PutBytes(buf *[]byte)

PutBytes Put *[]byte to byteBufferPool

func PutIoBuffer added in v0.0.2

func PutIoBuffer(buf IoBuffer) error

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

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

Bytes returns b.B, i.e. all the bytes accumulated in the buffer.

The purpose of this function is bytes.Buffer compatibility.

func (*Buffer) Cap

func (b *Buffer) Cap() int

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the size of the byte buffer.

func (*Buffer) ReadFrom

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

ReadFrom implements io.ReaderFrom.

The function appends all the data read from r to b.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset makes Buffer.B empty.

func (*Buffer) Set

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

Set sets Buffer.B to p.

func (*Buffer) SetString

func (b *Buffer) SetString(s string)

SetString sets Buffer.B to s.

func (*Buffer) String

func (b *Buffer) String() string

String returns string representation of Buffer.B.

func (*Buffer) TrimNewline

func (b *Buffer) TrimNewline()

TrimNewline trims any final "\n" byte from the end of the buffer.

func (*Buffer) Write

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

Write implements io.Writer - it appends p to Buffer.B

func (*Buffer) WriteBool

func (b *Buffer) WriteBool(v bool)

func (*Buffer) WriteByte

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

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 (b *Buffer) WriteFloat(f float64, bitSize int)

func (*Buffer) WriteInt

func (b *Buffer) WriteInt(n int64)

func (*Buffer) WriteString

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

WriteString appends s to Buffer.B.

func (*Buffer) WriteTo

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

WriteTo implements io.WriterTo.

func (*Buffer) WriteUint

func (b *Buffer) WriteUint(n uint64)

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

func GetIoBuffer(size int) IoBuffer

GetIoBuffer returns IoBuffer from pool

func NewIoBuffer added in v0.0.2

func NewIoBuffer(capacity int) IoBuffer

func NewIoBufferBytes added in v0.0.2

func NewIoBufferBytes(bytes []byte) IoBuffer

func NewIoBufferEOF added in v0.0.2

func NewIoBufferEOF() IoBuffer

func NewIoBufferString added in v0.0.2

func NewIoBufferString(s string) IoBuffer

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.

func (*Pool) Get

func (p *Pool) Get() *Buffer

Get returns new byte buffer with zero length.

The byte buffer may be returned to the pool via Put after the use in order to minimize GC overhead.

func (*Pool) Put

func (p *Pool) Put(b *Buffer)

Put releases byte buffer obtained via Get to the pool.

The buffer mustn't be accessed after returning to the pool.

Jump to

Keyboard shortcuts

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