buffer

package
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package buffer implements a buffer for serialization, consisting of a chain of []byte-s to reduce copying and to allow reuse of individual chunks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(cfg PoolConfig)

Init sets up a non-default pooling and allocation strategy. Should be run before serialization is done.

Types

type Buffer

type Buffer struct {

	// Buf is the current chunk that can be used for serialization.
	Buf []byte
	// contains filtered or unexported fields
}

Buffer is a buffer optimized for serialization without extra copying.

func (*Buffer) AppendByte

func (b *Buffer) AppendByte(data byte)

AppendByte appends a single byte to buffer.

func (*Buffer) AppendBytes

func (b *Buffer) AppendBytes(data []byte)

AppendBytes appends a byte slice to buffer.

func (*Buffer) AppendString

func (b *Buffer) AppendString(data string)

AppendString appends a string to buffer.

func (*Buffer) BuildBytes

func (b *Buffer) BuildBytes(reuse ...[]byte) []byte

BuildBytes creates a single byte slice with all the contents of the buffer. Data is copied if it does not fit in a single chunk. You can optionally provide one byte slice as argument that it will try to reuse.

func (*Buffer) Bytes added in v0.0.3

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

func (*Buffer) DumpTo

func (b *Buffer) DumpTo(w io.Writer) (written int, err error)

DumpTo outputs the contents of a buffer to a writer and resets the buffer.

func (*Buffer) EnsureSpace

func (b *Buffer) EnsureSpace(s int)

EnsureSpace makes sure that the current chunk contains at least s free bytes, possibly creating a new chunk.

func (*Buffer) ReadCloser

func (b *Buffer) ReadCloser() io.ReadCloser

ReadCloser creates an io.ReadCloser with all the contents of the buffer.

func (*Buffer) ReadFrom

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

ReadFrom

func (*Buffer) RecyclableReader added in v0.0.6

func (b *Buffer) RecyclableReader() RecyclableReader

ReadCloser creates an io.ReadCloser with all the contents of the buffer.

func (*Buffer) Size

func (b *Buffer) Size() int

Size computes the size of a buffer by adding sizes of every chunk.

func (*Buffer) String added in v0.0.3

func (b *Buffer) String() string

func (*Buffer) Write

func (b *Buffer) Write(data []byte) (n int, err error)

AppendBytes appends a byte slice to buffer.

func (*Buffer) WriteString

func (b *Buffer) WriteString(data string) (n int, err error)

AppendBytes appends a byte slice to buffer.

func (*Buffer) WriteTo

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

WriteTo outputs the contents of a buffer to a writer and resets the buffer.

type PoolConfig

type PoolConfig struct {
	StartSize  int // Minimum chunk size that is allocated.
	PooledSize int // Minimum chunk size that is reused, reusing chunks too small will result in overhead.
	MaxSize    int // Maximum chunk size that will be allocated.
}

PoolConfig contains configuration for the allocation and reuse strategy.

type RecyclableReader added in v1.0.0

type RecyclableReader interface {
	io.ReadCloser
	WriteTo(w io.Writer) (n int64, err error)
	Clone() RecyclableReader
	Recycle()
	Len() int
	Bytes() []byte
	String() string
	Reset()
	BuildBytes(reuse []byte) []byte
}

Jump to

Keyboard shortcuts

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