buf

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: Apache-2.0 Imports: 7 Imported by: 14

README

GoTH-bytebuf

make byte stream operation more easily

HOWTO

create empty byte buffer

    buf := EmptyByteBuf()

write string to byte buffer

    buf.WriteString("abcdef")

write int64 value to byte buffer

    buf.WriteInt64(int64(12345))

make a clone of current readable bytes

    clone := buf.Clone()

read 8 bytes array as int64 value from byte buffer

    buf := EmptyByteBuf()
    buf.WriteInt64(int64(12345)) // readable bytes len = 8
    v := buf.ReadInt64() // v = int64(12345)

skip 2 bytes and read 3 bytes of byte buffer

    bs := buf.Skip(2).ReadBytes(3)

reset byte buffer

    buf.Reset()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCompositeOutOfRange = errors.New("composite byte buf: out of range")

ErrCompositeOutOfRange is raised when a composite read or slice request exceeds the available readable region.

View Source
var ErrInsufficientSize = errors.New("insufficient size")
View Source
var ErrNilObject = errors.New("nil object")
View Source
var ErrRefCountUnderflow = errors.New("refcount underflow")

Functions

func ReleaseByteBuf added in v1.2.0

func ReleaseByteBuf(bb ByteBuf)

ReleaseByteBuf returns bb to its originating pool only when bb carries a valid poolIdx and owns a class-sized backing array. Views created by Slice, Duplicate, or ReadSlice carry poolIdx == -1 and are never pooled. Buffers whose backing array no longer matches the class size are dropped so the pool caches only predictably-sized arrays. A nil or non-*DefaultByteBuf argument is a no-op.

Types

type ByteBuf

type ByteBuf interface {
	io.Writer
	io.Reader
	io.Closer
	io.WriterAt
	ReaderIndex() int
	WriterIndex() int
	MarkReaderIndex() ByteBuf
	ResetReaderIndex() ByteBuf
	MarkWriterIndex() ByteBuf
	ResetWriterIndex() ByteBuf
	Reset() ByteBuf
	Bytes() []byte
	BytesCopy() []byte
	ReadableBytes() int
	Cap() int
	Grow(v int) ByteBuf
	// Compact moves the readable region to the beginning of the buffer and adjusts indices (including marked indices).
	Compact() ByteBuf
	// EnsureCapacity guarantees that at least n bytes of writable space
	// are available. It compacts when that alone suffices; otherwise it
	// doubles the capacity to the smallest power-of-two that fits the
	// required size and compacts the readable region to index 0.
	EnsureCapacity(n int) ByteBuf
	Skip(v int) ByteBuf
	Clone() ByteBuf
	// AppendByte appends a single byte and returns the ByteBuf for chaining
	AppendByte(c byte) ByteBuf
	WriteBytes(bs []byte) ByteBuf
	WriteString(s string) ByteBuf
	WriteByteBuf(buf ByteBuf) ByteBuf
	WriteReader(reader io.Reader) ByteBuf
	WriteInt16(v int16) ByteBuf
	WriteInt32(v int32) ByteBuf
	WriteInt64(v int64) ByteBuf
	WriteUInt16(v uint16) ByteBuf
	WriteUInt32(v uint32) ByteBuf
	WriteUInt64(v uint64) ByteBuf
	WriteFloat32(v float32) ByteBuf
	WriteFloat64(v float64) ByteBuf
	WriteInt16LE(v int16) ByteBuf
	WriteInt32LE(v int32) ByteBuf
	WriteInt64LE(v int64) ByteBuf
	WriteUInt16LE(v uint16) ByteBuf
	WriteUInt32LE(v uint32) ByteBuf
	WriteUInt64LE(v uint64) ByteBuf
	WriteFloat32LE(v float32) ByteBuf
	WriteFloat64LE(v float64) ByteBuf
	// Standard Go io.ByteWriter interface
	WriteByte(c byte) error
	// MustReadByte reads a single byte and panics if insufficient data (existing behavior)
	MustReadByte() byte
	// Standard Go io.ByteReader interface
	ReadByte() (byte, error)
	ReadBytes(len int) []byte
	ReadByteBuf(len int) ByteBuf
	ReadWriter(writer io.Writer) ByteBuf
	ReadInt16() int16
	ReadInt32() int32
	ReadInt64() int64
	ReadUInt16() uint16
	ReadUInt32() uint32
	ReadUInt64() uint64
	ReadFloat32() float32
	ReadFloat64() float64
	ReadInt16LE() int16
	ReadInt32LE() int32
	ReadInt64LE() int64
	ReadUInt16LE() uint16
	ReadUInt32LE() uint32
	ReadUInt64LE() uint64
	ReadFloat32LE() float32
	ReadFloat64LE() float64
}

ByteBuf defines a byte buffer interface that is NOT concurrent-safe.

Notes: - Unless specified otherwise, out-of-bound or invalid operations will panic (preserving the original semantics). - Bytes returns a mutable view; writing to the returned slice will mutate the internal buffer. - Use BytesCopy to obtain an immutable copy if you need isolation from internal mutations.

func AcquireByteBuf added in v1.2.0

func AcquireByteBuf(minCap int) ByteBuf

AcquireByteBuf returns a buffer with Cap() >= minCap. The buffer has refcount 1, zeroed indices, and an unspecified readable content. Buffers obtained via AcquireByteBuf must be returned with ReleaseByteBuf when the caller is done.

func EmptyByteBuf

func EmptyByteBuf() ByteBuf

func NewByteBuf

func NewByteBuf(bs []byte) ByteBuf

func NewByteBufString

func NewByteBufString(str string) ByteBuf

func NewSharedByteBuf added in v1.2.0

func NewSharedByteBuf(bs []byte) ByteBuf

NewSharedByteBuf wraps bs without copying. Writes that fit cap(bs) mutate the original backing array; writes that exceed it detach into a freshly allocated array and leave bs untouched.

type CompositeByteBuf added in v1.2.0

type CompositeByteBuf interface {
	ByteBuf
	Slicer
	RefCounted
	io.WriterTo

	// AddComponent appends a component that aliases the readable region of
	// bb. The composite does not Retain bb; the caller keeps ownership of
	// bb's lifecycle. Mutations to bb's readable region (within its
	// original capacity) remain visible through the composite.
	AddComponent(bb ByteBuf) CompositeByteBuf

	// AddComponents is the variadic form of AddComponent.
	AddComponents(bbs ...ByteBuf) CompositeByteBuf
}

CompositeByteBuf is a zero-copy ByteBuf that composes a sequence of component byte slices. Reads walk the components without copying; writes land in an internal writable tail so earlier components stay untouched. Components alias their sources, so mutations through the source (within the original capacity) remain visible through the composite.

CompositeByteBuf is NOT goroutine-safe.

func NewCompositeByteBuf added in v1.2.0

func NewCompositeByteBuf(bbs ...ByteBuf) CompositeByteBuf

NewCompositeByteBuf creates an empty composite with refcount 1 and appends the provided components.

type DefaultByteBuf

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

func (*DefaultByteBuf) AppendByte added in v1.1.0

func (b *DefaultByteBuf) AppendByte(c byte) ByteBuf

AppendByte appends a single byte and returns the ByteBuf for chaining.

func (*DefaultByteBuf) Bytes

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

func (*DefaultByteBuf) BytesCopy

func (b *DefaultByteBuf) BytesCopy() []byte

func (*DefaultByteBuf) Cap

func (b *DefaultByteBuf) Cap() int

func (*DefaultByteBuf) Clone

func (b *DefaultByteBuf) Clone() ByteBuf

func (*DefaultByteBuf) Close

func (b *DefaultByteBuf) Close() error

Close drops the backing array and clears all indices so the buffer holds no storage. Cap() returns 0 afterwards.

func (*DefaultByteBuf) Compact

func (b *DefaultByteBuf) Compact() ByteBuf

Compact moves the readable region to the beginning of the buffer and adjusts indices (including marked indices).

func (*DefaultByteBuf) Duplicate added in v1.2.0

func (b *DefaultByteBuf) Duplicate() ByteBuf

Duplicate returns a ByteBuf that shares b's backing array but keeps its own reader, writer, and mark indices. Writes that fit within the shared capacity are visible to b; writes that grow detach the duplicate.

func (*DefaultByteBuf) EnsureCapacity

func (b *DefaultByteBuf) EnsureCapacity(n int) ByteBuf

EnsureCapacity guarantees that at least n bytes of writable space are available. It compacts when that alone suffices; otherwise it doubles the capacity to the smallest power-of-two that fits the required size and compacts the readable region to index 0.

func (*DefaultByteBuf) Grow

func (b *DefaultByteBuf) Grow(v int) ByteBuf

func (*DefaultByteBuf) MarkReaderIndex

func (b *DefaultByteBuf) MarkReaderIndex() ByteBuf

func (*DefaultByteBuf) MarkWriterIndex

func (b *DefaultByteBuf) MarkWriterIndex() ByteBuf

func (*DefaultByteBuf) MustReadByte added in v1.1.0

func (b *DefaultByteBuf) MustReadByte() byte

MustReadByte reads a single byte and panics if insufficient data (original ReadByte behavior)

func (*DefaultByteBuf) Read

func (b *DefaultByteBuf) Read(p []byte) (n int, err error)

func (*DefaultByteBuf) ReadByte

func (b *DefaultByteBuf) ReadByte() (byte, error)

ReadByte implements io.ByteReader interface with error return

func (*DefaultByteBuf) ReadByteBuf

func (b *DefaultByteBuf) ReadByteBuf(len int) ByteBuf

func (*DefaultByteBuf) ReadBytes

func (b *DefaultByteBuf) ReadBytes(len int) []byte

func (*DefaultByteBuf) ReadFloat32

func (b *DefaultByteBuf) ReadFloat32() float32

func (*DefaultByteBuf) ReadFloat32LE

func (b *DefaultByteBuf) ReadFloat32LE() float32

func (*DefaultByteBuf) ReadFloat64

func (b *DefaultByteBuf) ReadFloat64() float64

func (*DefaultByteBuf) ReadFloat64LE

func (b *DefaultByteBuf) ReadFloat64LE() float64

func (*DefaultByteBuf) ReadInt16

func (b *DefaultByteBuf) ReadInt16() int16

func (*DefaultByteBuf) ReadInt16LE

func (b *DefaultByteBuf) ReadInt16LE() int16

func (*DefaultByteBuf) ReadInt32

func (b *DefaultByteBuf) ReadInt32() int32

func (*DefaultByteBuf) ReadInt32LE

func (b *DefaultByteBuf) ReadInt32LE() int32

func (*DefaultByteBuf) ReadInt64

func (b *DefaultByteBuf) ReadInt64() int64

func (*DefaultByteBuf) ReadInt64LE

func (b *DefaultByteBuf) ReadInt64LE() int64

func (*DefaultByteBuf) ReadSlice added in v1.2.0

func (b *DefaultByteBuf) ReadSlice(n int) ByteBuf

ReadSlice advances readerIndex by n and returns a zero-copy view over the consumed bytes. The returned view shares b's backing array.

func (*DefaultByteBuf) ReadUInt16

func (b *DefaultByteBuf) ReadUInt16() uint16

func (*DefaultByteBuf) ReadUInt16LE

func (b *DefaultByteBuf) ReadUInt16LE() uint16

func (*DefaultByteBuf) ReadUInt32

func (b *DefaultByteBuf) ReadUInt32() uint32

func (*DefaultByteBuf) ReadUInt32LE

func (b *DefaultByteBuf) ReadUInt32LE() uint32

func (*DefaultByteBuf) ReadUInt64

func (b *DefaultByteBuf) ReadUInt64() uint64

func (*DefaultByteBuf) ReadUInt64LE

func (b *DefaultByteBuf) ReadUInt64LE() uint64

func (*DefaultByteBuf) ReadWriter

func (b *DefaultByteBuf) ReadWriter(writer io.Writer) ByteBuf

func (*DefaultByteBuf) ReadableBytes

func (b *DefaultByteBuf) ReadableBytes() int

func (*DefaultByteBuf) ReaderIndex

func (b *DefaultByteBuf) ReaderIndex() int

func (*DefaultByteBuf) RefCnt added in v1.2.0

func (b *DefaultByteBuf) RefCnt() int32

RefCnt returns the current reference count.

func (*DefaultByteBuf) Release added in v1.2.0

func (b *DefaultByteBuf) Release() bool

Release decrements the reference count. It returns true when the counter reaches zero, at which point the caller owns the final drop. Panics on underflow.

func (*DefaultByteBuf) Reset

func (b *DefaultByteBuf) Reset() ByteBuf

Reset clears all indices (reader, writer, and marks) while keeping the backing array. Cap() is unchanged. Use Close to release the backing array.

func (*DefaultByteBuf) ResetReaderIndex

func (b *DefaultByteBuf) ResetReaderIndex() ByteBuf

func (*DefaultByteBuf) ResetWriterIndex

func (b *DefaultByteBuf) ResetWriterIndex() ByteBuf

func (*DefaultByteBuf) Retain added in v1.2.0

func (b *DefaultByteBuf) Retain() ByteBuf

Retain increments the reference count and returns the buffer for chaining.

func (*DefaultByteBuf) Skip

func (b *DefaultByteBuf) Skip(v int) ByteBuf

Skip advances readerIndex by v bytes using only index arithmetic. Panics on a negative v or when v exceeds ReadableBytes.

func (*DefaultByteBuf) Slice added in v1.2.0

func (b *DefaultByteBuf) Slice(from, length int) ByteBuf

Slice returns a view that shares the backing array with b. The view starts at the given offset within b's readable region and covers length bytes. Mutations through the view within its capacity are visible to b. A write that needs to grow beyond the initial capacity allocates a fresh backing array inside the view and detaches it from b.

func (*DefaultByteBuf) Write

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

func (*DefaultByteBuf) WriteAt

func (b *DefaultByteBuf) WriteAt(p []byte, offset int64) (n int, err error)

func (*DefaultByteBuf) WriteByte

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

WriteByte implements io.ByteWriter interface with error return

func (*DefaultByteBuf) WriteByteBuf

func (b *DefaultByteBuf) WriteByteBuf(buf ByteBuf) ByteBuf

func (*DefaultByteBuf) WriteBytes

func (b *DefaultByteBuf) WriteBytes(bs []byte) ByteBuf

func (*DefaultByteBuf) WriteFloat32

func (b *DefaultByteBuf) WriteFloat32(v float32) ByteBuf

func (*DefaultByteBuf) WriteFloat32LE

func (b *DefaultByteBuf) WriteFloat32LE(v float32) ByteBuf

func (*DefaultByteBuf) WriteFloat64

func (b *DefaultByteBuf) WriteFloat64(v float64) ByteBuf

func (*DefaultByteBuf) WriteFloat64LE

func (b *DefaultByteBuf) WriteFloat64LE(v float64) ByteBuf

func (*DefaultByteBuf) WriteInt16

func (b *DefaultByteBuf) WriteInt16(v int16) ByteBuf

func (*DefaultByteBuf) WriteInt16LE

func (b *DefaultByteBuf) WriteInt16LE(v int16) ByteBuf

func (*DefaultByteBuf) WriteInt32

func (b *DefaultByteBuf) WriteInt32(v int32) ByteBuf

func (*DefaultByteBuf) WriteInt32LE

func (b *DefaultByteBuf) WriteInt32LE(v int32) ByteBuf

func (*DefaultByteBuf) WriteInt64

func (b *DefaultByteBuf) WriteInt64(v int64) ByteBuf

func (*DefaultByteBuf) WriteInt64LE

func (b *DefaultByteBuf) WriteInt64LE(v int64) ByteBuf

func (*DefaultByteBuf) WriteReader

func (b *DefaultByteBuf) WriteReader(reader io.Reader) ByteBuf

func (*DefaultByteBuf) WriteString

func (b *DefaultByteBuf) WriteString(s string) ByteBuf

func (*DefaultByteBuf) WriteUInt16

func (b *DefaultByteBuf) WriteUInt16(v uint16) ByteBuf

func (*DefaultByteBuf) WriteUInt16LE

func (b *DefaultByteBuf) WriteUInt16LE(v uint16) ByteBuf

func (*DefaultByteBuf) WriteUInt32

func (b *DefaultByteBuf) WriteUInt32(v uint32) ByteBuf

func (*DefaultByteBuf) WriteUInt32LE

func (b *DefaultByteBuf) WriteUInt32LE(v uint32) ByteBuf

func (*DefaultByteBuf) WriteUInt64

func (b *DefaultByteBuf) WriteUInt64(v uint64) ByteBuf

func (*DefaultByteBuf) WriteUInt64LE

func (b *DefaultByteBuf) WriteUInt64LE(v uint64) ByteBuf

func (*DefaultByteBuf) WriterIndex

func (b *DefaultByteBuf) WriterIndex() int

type RefCounted added in v1.2.0

type RefCounted interface {
	Retain() ByteBuf
	Release() bool
	RefCnt() int32
}

RefCounted is implemented by ByteBufs that participate in reference counted lifecycle management. Fresh buffers start at refcount 1.

type Slicer added in v1.2.0

type Slicer interface {
	Slice(from, length int) ByteBuf
	Duplicate() ByteBuf
	ReadSlice(n int) ByteBuf
}

Slicer is implemented by ByteBufs that expose zero-copy view APIs sharing the same backing storage as the parent.

Jump to

Keyboard shortcuts

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