buffer

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is equivalent of bytes.Buffer without the ability to read. It is NOT thread-safe.

MaxSize can be set to limit the memory usage.

func Load added in v1.8.0

func Load(source Source) *Buffer

func NewBuffer

func NewBuffer(source Source, capacity int) *Buffer

func (*Buffer) Allocate

func (b *Buffer) Allocate(n int) []byte

Allocate is a way to get a slice of size n back from the buffer. This slice can be directly written to. Warning: Allocate is not thread-safe. The byte slice returned MUST be used before further calls to Buffer.

func (*Buffer) AllocateOffset

func (b *Buffer) AllocateOffset(n int) int

AllocateOffset works the same way as allocate, but instead of returning a byte slice, it returns the offset of the allocation.

func (*Buffer) Bytes

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

Bytes would return all the written bytes as a slice.

func (*Buffer) Data

func (b *Buffer) Data(offset int) []byte

func (*Buffer) Grow

func (b *Buffer) Grow(n int)

Grow would grow the buffer to have at least n more bytes. In case the buffer is at capacity, it would reallocate twice the size of current capacity + n, to ensure n bytes can be written to the buffer without further allocation. In UseMmap mode, this might result in underlying file expansion.

func (*Buffer) IsEmpty

func (b *Buffer) IsEmpty() bool

func (*Buffer) LenNoPadding

func (b *Buffer) LenNoPadding() int

LenNoPadding would return the number of bytes written to the buffer so far (without the padding).

func (*Buffer) LenWithPadding

func (b *Buffer) LenWithPadding() int

LenWithPadding would return the number of bytes written to the buffer so far plus the padding at the start of the buffer.

func (*Buffer) Release

func (b *Buffer) Release() error

Release would free up the memory allocated by the buffer. Once the usage of buffer is done, it is important to call Release, otherwise a memory leak can happen.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset would reset the buffer to be reused.

func (*Buffer) SliceAllocate

func (b *Buffer) SliceAllocate(sz int) []byte

SliceAllocate would encode the size provided into the buffer, followed by a call to Allocate, hence returning the slice of size sz. This can be used to allocate a lot of small buffers into this big buffer. Note that SliceAllocate should NOT be mixed with normal calls to Write.

func (*Buffer) StartOffset

func (b *Buffer) StartOffset() int

func (*Buffer) WithMaxSize

func (b *Buffer) WithMaxSize(size int) *Buffer

func (*Buffer) Write

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

Write would write p bytes to the buffer.

func (*Buffer) WriteSlice

func (b *Buffer) WriteSlice(slice []byte)

type Slice added in v1.7.0

type Slice struct {
	// contains filtered or unexported fields
}

func NewSlice added in v1.8.0

func NewSlice(data []byte) *Slice

func (*Slice) Data added in v1.7.0

func (s *Slice) Data() []byte

func (*Slice) Grow added in v1.7.0

func (s *Slice) Grow(size int) []byte

func (*Slice) Release added in v1.7.0

func (s *Slice) Release() error

type Source added in v1.7.0

type Source interface {
	Data() []byte
	Grow(size int) []byte
	Release() error
}

Jump to

Keyboard shortcuts

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