buffer

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

buffer

支持内存大小端读写的固定数据缓冲区 A fixed data buffer that supports reads and writes at the memory size end

用于高并发下的[]Byte缓存池复用 Used for []Byte cache pool multiplexing under high concurrency

Example

var buf = NewByteBuffer(1024, binary.LittleEndian)

buf.WriteString("123456!@#$%^asdfgh")
buf.WriteInt32(0)
buf.WriteFloat32(3.1415926)
buf.WriteInt32(12345678)

buf.SetPosition(0)

buf.ReadString(-1)
buf.ReadInt32()
buf.ReadFloat32()
buf.ReadInt32()

buf.Reset()
buf.Reset()

API

NewByteBuffer(int, binary.ByteOrder) *ByteBuffer

Base Method

ByteBuffer::Cap() int
ByteBuffer::Length() int
ByteBuffer::Buffer() []byte
ByteBuffer::Data() []byte
ByteBuffer::Reset()
ByteBuffer::SetOrder()
ByteBuffer::SetPosition(value int) error
ByteBuffer::Position() int

Write

ByteBuffer::PutUInt16(offset int, value uint16) error
ByteBuffer::PutUInt32(offset int, value uint32) error
ByteBuffer::PutUInt64(offset int, value uint64) error

Read

ByteBuffer::GetUInt16(offset int) (value uint16, err error)
ByteBuffer::GetUInt32(offset int) (value uint32, err error)
ByteBuffer::GetUInt64(offset int) (value uint64, err error)

Writer API

ByteBuffer::WriteInt8(value byte) error
ByteBuffer::WriteBoolean(value bool) error
ByteBuffer::WriteString(value string) (wlen int, err error)
ByteBuffer::WriteBytes(value []byte) (int, error)
ByteBuffer::WriteInt16(value int16) error
ByteBuffer::WriteUInt16(value uint16) error
ByteBuffer::WriteInt32(value int32) error
ByteBuffer::WriteUInt32(value uint32) error
ByteBuffer::WriteInt64(value int64) error
ByteBuffer::WriteUInt64(value uint64) error
ByteBuffer::WriteFloat32(value float32) error
ByteBuffer::WriteFloat64(value float64) error

Reader API

ByteBuffer::ReadInt8() (byte, error)
ByteBuffer::ReadBoolean() (bool, error)
ByteBuffer::ReadString(length int) (string, error)
ByteBuffer::ReadBytes(value []byte, length int) (int, error)
ByteBuffer::ReadInt16() (int16, error)
ByteBuffer::ReadUInt16() (uint16, error)
ByteBuffer::ReadInt32() (int32, error)
ByteBuffer::ReadUInt32() (uint32, error)
ByteBuffer::ReadInt64() (int64, error)
ByteBuffer::ReadUInt64() (uint64, error)
ByteBuffer::ReadFloat32() (float32, error)
ByteBuffer::ReadFloat64() (float64, error)

Documentation

Index

Constants

View Source
const (
	TRUE  = 1
	FALSE = 0
)
View Source
const (
	SIZEOF_1_BYTE = 1
	SIZEOF_2_BYTE = 2
	SIZEOF_4_BYTE = 4
	SIZEOF_8_BYTE = 8
)

Variables

View Source
var ErrorPositionOverflow = errors.New("Invalid data access pointer “offset”")

ErrorPositionOverflow Invalid data access pointer “offset”

View Source
var ErrorReadOverflow = errors.New("The data read pointer is out of bounds")

ErrorReadOverflow The data read pointer is out of bounds

View Source
var ErrorWriteOverflow = errors.New("The data write pointer is out of bounds")

ErrorWriteOverflow The data write pointer is out of bounds

Functions

This section is empty.

Types

type ByteBuffer

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

ByteBuffer A fixed-size buffer of read-write data

func NewByteBuffer

func NewByteBuffer(cap int, order binary.ByteOrder) *ByteBuffer

NewByteBuffer Create a fixed-size memory buffer order to determine how the data is written, should you use either binary.LittleEndian or binary.BigEndian

func (*ByteBuffer) Buffer

func (buf *ByteBuffer) Buffer() []byte

Buffer Gets the buffer raw byte array

func (*ByteBuffer) Cap

func (buf *ByteBuffer) Cap() int

Cap Get buffer size

func (*ByteBuffer) Data

func (buf *ByteBuffer) Data() []byte

Data Gets valid data in the buffer

func (*ByteBuffer) GetUInt16

func (buf *ByteBuffer) GetUInt16(offset int) (value uint16, err error)

GetUInt16 Reads a uint16 value from the specified position offset This operation does not change the location of the read/write index

func (*ByteBuffer) GetUInt32

func (buf *ByteBuffer) GetUInt32(offset int) (value uint32, err error)

GetUInt32 Reads a uint32 type value from the specified position offset This operation does not change the location of the read/write index

func (*ByteBuffer) GetUInt64

func (buf *ByteBuffer) GetUInt64(offset int) (value uint64, err error)

GetUInt64 Reads a value of type uint64 from the specified position offset This operation does not change the location of the read/write index

func (*ByteBuffer) Length

func (buf *ByteBuffer) Length() int

Length Gets the buffer effective data length

func (*ByteBuffer) Position

func (buf *ByteBuffer) Position() int

Position Get the read/write offset

func (*ByteBuffer) PutUInt16

func (buf *ByteBuffer) PutUInt16(offset int, value uint16) error

PutUInt16 Fill in a uint16 value of type at the specified position offset This operation does not change the location of the read/write index

func (*ByteBuffer) PutUInt32

func (buf *ByteBuffer) PutUInt32(offset int, value uint32) error

PutUInt32 Enter a uint32 type value at the specified position offset This operation does not change the location of the read/write index

func (*ByteBuffer) PutUInt64

func (buf *ByteBuffer) PutUInt64(offset int, value uint64) error

PutUInt64 Enter a value of type uint64 at the specified position offset This operation does not change the location of the read/write index

func (*ByteBuffer) ReadBoolean

func (buf *ByteBuffer) ReadBoolean() (bool, error)

ReadBoolean Reads a 1-byte Boolean variable from the current location

func (*ByteBuffer) ReadBytes

func (buf *ByteBuffer) ReadBytes(value []byte, length int) (int, error)

ReadBytes Success is returned when length is 0, but not read Reads a byte array from the current location When length is less than 0, the length is the length of the array "value" Returns the length of the data actually read

func (*ByteBuffer) ReadFloat32

func (buf *ByteBuffer) ReadFloat32() (float32, error)

ReadFloat32 Reads a value of type Float32 from the current position

func (*ByteBuffer) ReadFloat64

func (buf *ByteBuffer) ReadFloat64() (float64, error)

ReadFloat64 Reads a value of type Float64 from the current position

func (*ByteBuffer) ReadInt8

func (buf *ByteBuffer) ReadInt8() (byte, error)

ReadInt8 Reads a value of type int8 from the current position

func (*ByteBuffer) ReadInt16

func (buf *ByteBuffer) ReadInt16() (int16, error)

ReadInt16 Reads a value of type int16 from the current position

func (*ByteBuffer) ReadInt32

func (buf *ByteBuffer) ReadInt32() (int32, error)

ReadInt32 Reads a value of type Int32 from the current position

func (*ByteBuffer) ReadInt64

func (buf *ByteBuffer) ReadInt64() (int64, error)

ReadInt64 Reads a value of type Int64 from the current position

func (*ByteBuffer) ReadString

func (buf *ByteBuffer) ReadString(length int) (string, error)

ReadString Reads a String from the current position Success is returned when length is 0, but not read When length is less than 0, it reads from the current position until it reaches "0" and returns the result, otherwise all subsequent data is returned

func (*ByteBuffer) ReadUInt16

func (buf *ByteBuffer) ReadUInt16() (uint16, error)

ReadUInt16 Reads a value of type UInt16 from the current position

func (*ByteBuffer) ReadUInt32

func (buf *ByteBuffer) ReadUInt32() (uint32, error)

ReadUInt32 Reads a value of type UInt32 from the current position

func (*ByteBuffer) ReadUInt64

func (buf *ByteBuffer) ReadUInt64() (uint64, error)

ReadUInt64 Reads a value of type UInt64 from the current position

func (*ByteBuffer) Reset

func (buf *ByteBuffer) Reset()

Reset Clear the read/write index and data in the buffer

func (*ByteBuffer) SetOrder

func (buf *ByteBuffer) SetOrder(order binary.ByteOrder)

SetOrder Set the read and write mode of the buffer Used binary.LittleEndian or binary.BigEndian

func (*ByteBuffer) SetPosition

func (buf *ByteBuffer) SetPosition(asbOffset int) error

SetPosition Set the read/write offset

func (*ByteBuffer) WriteBoolean

func (buf *ByteBuffer) WriteBoolean(value bool) error

WriteBoolean Writes a 1-byte Boolean variable at the current location

func (*ByteBuffer) WriteBytes

func (buf *ByteBuffer) WriteBytes(value []byte) (int, error)

WriteBytes Writes a byte array at the current location and returns the length of the data successfully written

func (*ByteBuffer) WriteFloat32

func (buf *ByteBuffer) WriteFloat32(value float32) error

WriteFloat32 Writes an Float32 type value to the current location

func (*ByteBuffer) WriteFloat64

func (buf *ByteBuffer) WriteFloat64(value float64) error

WriteFloat64 Writes an Float64 type value to the current location

func (*ByteBuffer) WriteInt8

func (buf *ByteBuffer) WriteInt8(value byte) error

WriteInt8 Writes an int8 type value to the current location

func (*ByteBuffer) WriteInt16

func (buf *ByteBuffer) WriteInt16(value int16) error

WriteInt16 Writes an int16 type value to the current location

func (*ByteBuffer) WriteInt32

func (buf *ByteBuffer) WriteInt32(value int32) error

WriteInt32 Writes an int32 type value to the current location

func (*ByteBuffer) WriteInt64

func (buf *ByteBuffer) WriteInt64(value int64) error

WriteInt64 Writes an int64 type value to the current location

func (*ByteBuffer) WriteString

func (buf *ByteBuffer) WriteString(value string) (wlen int, err error)

WriteString Writes a String at the current location

func (*ByteBuffer) WriteUInt16

func (buf *ByteBuffer) WriteUInt16(value uint16) error

WriteUInt16 Writes a uint16 value at the current location

func (*ByteBuffer) WriteUInt32

func (buf *ByteBuffer) WriteUInt32(value uint32) error

WriteUInt32 Writes an UInt32 type value to the current location

func (*ByteBuffer) WriteUInt64

func (buf *ByteBuffer) WriteUInt64(value uint64) error

WriteUInt64 Writes an UInt64 type value to the current location

Jump to

Keyboard shortcuts

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