buf

package
v2.0.3-0...-26c9a2f Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: Apache-2.0 Imports: 4 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Byte2Int

func Byte2Int(data []byte) int

Byte2Int byte array to int value using big order

func Byte2Int64

func Byte2Int64(data []byte) int64

Byte2Int64 byte array to int64 value using big order

func Byte2Uint16

func Byte2Uint16(data []byte) uint16

Byte2Uint16 byte array to uint16 value using big order

func Byte2Uint32

func Byte2Uint32(data []byte) uint32

Byte2Uint32 byte array to uint32 value using big order

func Byte2Uint64

func Byte2Uint64(data []byte) uint64

Byte2Uint64 byte array to int64 value using big order

func Int2Bytes

func Int2Bytes(v int) []byte

Int2Bytes int value to bytes array using big order

func Int2BytesTo

func Int2BytesTo(v int, ret []byte)

Int2BytesTo int value to bytes array using big order

func Int64ToBytes

func Int64ToBytes(v int64) []byte

Int64ToBytes int64 value to bytes array using big order

func Int64ToBytesTo

func Int64ToBytesTo(v int64, ret []byte)

Int64ToBytesTo int64 value to bytes array using big order

func Uint16ToBytes

func Uint16ToBytes(v uint16) []byte

Uint16ToBytes uint16 value to bytes array using big order

func Uint16ToBytesTo

func Uint16ToBytesTo(v uint16, ret []byte)

Uint16ToBytesTo uint16 value to bytes array using big order

func Uint32ToBytes

func Uint32ToBytes(v uint32) []byte

Uint32ToBytes uint32 value to bytes array using big order

func Uint32ToBytesTo

func Uint32ToBytesTo(v uint32, ret []byte)

Uint32ToBytesTo uint32 value to bytes array using big order

func Uint64ToBytes

func Uint64ToBytes(v uint64) []byte

Uint64ToBytes uint64 value to bytes array using big order

func Uint64ToBytesTo

func Uint64ToBytesTo(v uint64, ret []byte)

Uint64ToBytesTo uint64 value to bytes array using big order

func WriteTo

func WriteTo(data []byte, conn io.Writer, copyBuffer int) error

WriteTo write data to io.Writer, copyBuffer used to control how much data will written at a time.

Types

type Allocator

type Allocator interface {
	// Alloc allocate a []byte with len(data) >= size, and the returned []byte cannot
	// be expanded in use.
	Alloc(capacity int) []byte
	// Free free the allocated memory
	Free([]byte)
}

Allocator memory allocation for ByteBuf

type ByteBuf

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

ByteBuf is a reusable buffer that holds an internal []byte and maintains 2 indexes for read and write data.

| discardable bytes | readable bytes | writeable bytes | | | | | | | | | 0 <= readerIndex <= writerIndex <= capacity

The ByteBuf implemented io.Reader, io.Writer, io.WriterTo, io.ReaderFrom interface

func NewByteBuf

func NewByteBuf(capacity int, opts ...Option) *ByteBuf

NewByteBuf create bytebuf with options

func (*ByteBuf) ClearMark

func (b *ByteBuf) ClearMark()

ClearMark clear mark index

func (*ByteBuf) Close

func (b *ByteBuf) Close()

Close close the ByteBuf

func (*ByteBuf) GetMarkIndex

func (b *ByteBuf) GetMarkIndex() int

GetMarkIndex returns the markIndex.

func (*ByteBuf) GetMarkedDataLen

func (b *ByteBuf) GetMarkedDataLen() int

GetMarkedDataLen returns len of marked data

func (*ByteBuf) GetReadIndex

func (b *ByteBuf) GetReadIndex() int

GetReadIndex returns the read index

func (*ByteBuf) GetWriteIndex

func (b *ByteBuf) GetWriteIndex() int

GetWriteIndex get the write index

func (*ByteBuf) GetWriteOffset

func (b *ByteBuf) GetWriteOffset() int

GetWriteOffset returns the offset of the current writeIndex relative to the ReadIndex. GetWriteIndex returns an absolute position, which will fail when the underlying buf is expanded.

func (*ByteBuf) Grow

func (b *ByteBuf) Grow(n int)

Grow grow buf size

func (*ByteBuf) MustReadByte

func (b *ByteBuf) MustReadByte() byte

MustReadByte is similar to ReadByte, buf panic if error retrurned

func (*ByteBuf) MustWrite

func (b *ByteBuf) MustWrite(value []byte)

MustWrite is similar to Write, but panic if encounter an error.

func (*ByteBuf) MustWriteByte

func (b *ByteBuf) MustWriteByte(v byte)

MustWriteByte is similar to WriteByte, but panic if has any error

func (*ByteBuf) PeekInt

func (b *ByteBuf) PeekInt(offset int) int

PeekInt is similar to ReadInt, but keep readIndex not changed.

func (*ByteBuf) PeekN

func (b *ByteBuf) PeekN(offset, bytes int) []byte

PeekN is similar to ReadBytes, but keep readIndex not changed.

func (*ByteBuf) RawBuf

func (b *ByteBuf) RawBuf() []byte

RawBuf returns raw buf. This method requires special care, as the ByteBuf may free the internal []byte after the data is written again, causing the slice to fail.

func (*ByteBuf) RawSlice

func (b *ByteBuf) RawSlice(from, to int) []byte

RawSlice returns raw buf in range [from, to). This method requires special care, as the ByteBuf may free the internal []byte after the data is written again, causing the slice to fail.

func (*ByteBuf) Read

func (b *ByteBuf) Read(dst []byte) (int, error)

Read implemented io.Reader interface. return n, nil or 0, io.EOF is successful

func (*ByteBuf) ReadAll

func (b *ByteBuf) ReadAll() (readed int, data []byte)

ReadAll read all readable bytes.

func (*ByteBuf) ReadByte

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

ReadByte read a byte from buf

func (*ByteBuf) ReadBytes

func (b *ByteBuf) ReadBytes(n int) (readed int, data []byte)

ReadBytes read bytes from buf. It's will copy the data to a new byte array.

func (*ByteBuf) ReadFrom

func (b *ByteBuf) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom implemented io.ReaderFrom interface

func (*ByteBuf) ReadInt

func (b *ByteBuf) ReadInt() int

ReadInt get int value from buf

func (*ByteBuf) ReadInt64

func (b *ByteBuf) ReadInt64() int64

ReadInt64 get int64 value from buf

func (*ByteBuf) ReadMarkedData

func (b *ByteBuf) ReadMarkedData() []byte

ReadMarkedData returns [readIndex, markIndex) data

func (*ByteBuf) ReadUint16

func (b *ByteBuf) ReadUint16() uint16

ReadUint16 get uint16 value from buf

func (*ByteBuf) ReadUint32

func (b *ByteBuf) ReadUint32() uint32

ReadUint32 get uint32 value from buf

func (*ByteBuf) ReadUint64

func (b *ByteBuf) ReadUint64() uint64

ReadUint64 get uint64 value from buf

func (*ByteBuf) Readable

func (b *ByteBuf) Readable() int

Readable return the number of bytes that can be read.

func (*ByteBuf) Reset

func (b *ByteBuf) Reset()

Reset reset to reuse.

func (*ByteBuf) SetMarkIndex

func (b *ByteBuf) SetMarkIndex(markIndex int)

SetMarkIndex mark data in range [readIndex, markIndex)

func (*ByteBuf) SetReadIndex

func (b *ByteBuf) SetReadIndex(readIndex int)

SetReadIndex set the reader index. The data in the [readIndex, writeIndex] that can be read.

func (*ByteBuf) SetWriteIndex

func (b *ByteBuf) SetWriteIndex(writeIndex int)

SetWriteIndex set the write index. The data can write into range [writeIndex, len(buf)). Note, since the underlying buf will expand, the previously held writeIndex will become invalid, and in most cases this method should use SetWriteIndexByOffset instead.

func (*ByteBuf) SetWriteIndexByOffset

func (b *ByteBuf) SetWriteIndexByOffset(writeOffset int)

SetWriteIndexByOffset Use writeOffset to reset writeIndex, since offset is a relative position to readIndex, so it won't affect correctness when the underlying buf is expanded.

func (*ByteBuf) Skip

func (b *ByteBuf) Skip(n int)

Skip skip [readIndex, readIndex+n).

func (*ByteBuf) Slice

func (b *ByteBuf) Slice(from, to int) Slice

Slice returns a read only bytebuf slice. ByteBuf may be continuously written to, causing the internal buf to reapply, thus invalidating the sliced data in buf[s:e]. Slice only records the starting location of the data, and it is safe to read the data when it is certain that the ByteBuf will not be written to.

func (*ByteBuf) Write

func (b *ByteBuf) Write(src []byte) (int, error)

Write implemented io.Writer interface

func (*ByteBuf) WriteByte

func (b *ByteBuf) WriteByte(v byte) error

WriteByte write a byte value into buf.

func (*ByteBuf) WriteInt

func (b *ByteBuf) WriteInt(v int)

WriteInt write int into buf

func (*ByteBuf) WriteInt64

func (b *ByteBuf) WriteInt64(v int64)

WriteInt64 write int64 into buf

func (*ByteBuf) WriteString

func (b *ByteBuf) WriteString(v string)

WriteString write a string value to buf

func (*ByteBuf) WriteTo

func (b *ByteBuf) WriteTo(dst io.Writer) (int64, error)

WriteTo implemented io.WriterTo interface

func (*ByteBuf) WriteUint16

func (b *ByteBuf) WriteUint16(v uint16)

WriteUint16 write uint16 into buf

func (*ByteBuf) WriteUint32

func (b *ByteBuf) WriteUint32(v uint32)

WriteUint32 write uint32 into buf

func (*ByteBuf) WriteUint64

func (b *ByteBuf) WriteUint64(v uint64)

WriteUint64 write uint64 into buf

func (*ByteBuf) Writeable

func (b *ByteBuf) Writeable() int

Writeable return how many bytes can be wirte into buf

type Option

type Option func(*ByteBuf)

Option bytebuf option

func WithDisableCompactAfterGrow

func WithDisableCompactAfterGrow(value bool) Option

disableCompactAfterGrow set Set whether the buffer should be compressed, if it is grow, it will reset the reader and writer index. Default is true.

func WithIOCopyBufferSize

func WithIOCopyBufferSize(value int) Option

WithIOCopyBufferSize set io copy buffer used to control how much data will written at a time.

func WithMemAllocator

func WithMemAllocator(alloc Allocator) Option

WithMemAllocator Set the memory allocator, when Bytebuf is initialized, it needs to allocate a []byte of the size specified by capacity from memory. When ByteBuf.Release is called, the memory will be freed back to the allocator.

func WithMinGowSize

func WithMinGowSize(minGrowSize int) Option

WithMinGowSize set minimum Grow size. When there is not enough space left in the Bytebuf, write data needs to be expanded.

type Slice

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

Slice the slice of byte buf

func (Slice) Data

func (s Slice) Data() []byte

Data data

Jump to

Keyboard shortcuts

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