bytex

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2022 License: MIT Imports: 5 Imported by: 0

README

bytex

go内置的bytes.Buffer功能很丰富,但它更多面向的是bytes这样的数据类型,bytex封装了bytes.Buffer,丰富了更多的基本类型操作。

在参考了MT协议情况下,实现了序列化io接口IBufferX。

从io的角度来说,bytes缓冲区分为read io与write io。

//创建一个可读的缓冲区,data是可读的数据,大部分情况来自网络数据,RPC数据,数据库或文件数据
func NewReadableBufferX(data []byte) *BufferX {
    var buffer = bytes.NewBuffer(data)
    var bufferX = &BufferX{buffer: buffer}
    return bufferX
}

//创建一个可写的缓冲区大小为1K,如果写入内容超过1K,此缓冲区会自动增长
func NewBufferX() *BufferX {
    var data = make([]byte, defaultByteBuff)
    var buffer = bytes.NewBuffer(data)
    buffer.Reset()
    var bufferX = &BufferX{buffer: buffer}
    return bufferX
}

//创建一个可写的缓冲区,大小为传入的size,如果写入内容超过此大小,此缓冲区会自动增长
//从效率上来说,传入的size应该与要写入的数据大小匹配,这样不会浪费内存,也不会因为
//在缓冲区不够时新开内存影响效率。
func NewSizedBufferX(size int) *BufferX {
    var data = make([]byte, size)
    var buffer = bytes.NewBuffer(data)
    buffer.Reset()
    var bufferX = &BufferX{buffer: buffer}
    return bufferX
}
//IBufferX buffer interface
type IBufferX interface {
    //Len : length
    Len() int
    //Read specific p, if length is not enough, return error
    Read(p []byte) error
    //ReadN n bytes, if length is not enough, return error
    ReadN(n int) ([]byte, error)
    //Write to buffer
    Write(p []byte)
    //Bytes left bytes
    Bytes() []byte
    //Reset reset cursor
    Reset()

    //ReWrite a buffer
    //重写缓冲区指定位置的数据,最常用的case是有些数据头部可能需要记录整段数据大小。
    //但最开始写入时,大小并不确定,可以先用0填充头部,在写完整段内容有了大小后在重写头部位置的数据。
    ReWrite(pos int, p []byte)
    //ReWriteU32 rewrite with a specific pos
    ReWriteU32(pos int, v uint32)

    //ReadU8 read byte
    ReadU8() (byte, error)
    //WriteU8 write byte
    WriteU8(byte)

    //ReadBool read bool
    ReadBool() (bool, error)
    //WriteBool write bool
    WriteBool(bool)

    //ReadLimitString read string
    ReadLimitString(limit uint32) (string, error)
    //WriteLimitString write string
    WriteLimitString(limit uint32, val string) error

    //ReadString read string
    ReadString() (string, error)
    //WriteString write string
    WriteString(val string)

    //ReadU16 read uint16
    ReadU16() (uint16, error)
    //WriteU16 write uint16
    WriteU16(uint16)

    //ReadI16 read int16
    ReadI16() (int16, error)
    //WriteI16 write int16
    WriteI16(int16)

    //ReadU32 read uint32
    ReadU32() (uint32, error)
    //WriteU32 write uint32
    WriteU32(uint32)

    //ReadI32 read int32
    ReadI32() (int32, error)
    //WriteI32 write int32
    WriteI32(int32)

    //ReadU64 read uint64
    ReadU64() (uint64, error)
    //WriteU64 write uint64
    WriteU64(uint64)

    //ReadI64 read int64
    ReadI64() (int64, error)
    //WriteI64 write int64
    WriteI64(int64)

    //ReadF64 read float64
    ReadF64() (float64, error)
    //WriteF64 write float64
    WriteF64(float64)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrByteBufferEmpty = errors.New("byte.buffer.empty")
	ErrReadWrongNum    = errors.New("byte.buffer.wrong.num")
	ErrSizeLimit       = errors.New("byte.buffer.size.limit")
)

Functions

This section is empty.

Types

type BufferX

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

BufferX buffer implement

func NewBufferX

func NewBufferX() *BufferX

NewBufferX new buffer with a default size as unpack

func NewReadableBufferX

func NewReadableBufferX(data []byte) *BufferX

NewReadableBufferX new buffer from existed bytes to read. Use existed bytes to fill buffer, the buffer is always used as read stream.

func NewSizedBufferX

func NewSizedBufferX(size int) *BufferX

NewSizedBufferX : new buffer with specific size

func (*BufferX) Bytes

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

Bytes all buffer bytes

func (*BufferX) Len

func (b *BufferX) Len() int

Len : buffer len

func (*BufferX) ReWrite

func (b *BufferX) ReWrite(pos int, p []byte)

ReWrite buffer

func (*BufferX) ReWriteU32

func (b *BufferX) ReWriteU32(pos int, v uint32)

ReWriteU32 rewrite uint32

func (*BufferX) Read

func (b *BufferX) Read(p []byte) error

read to a buffer

func (*BufferX) ReadBool

func (b *BufferX) ReadBool() (bool, error)

ReadBool read a bool

func (*BufferX) ReadF64

func (b *BufferX) ReadF64() (float64, error)

ReadF64 read float64

func (*BufferX) ReadI16

func (b *BufferX) ReadI16() (int16, error)

ReadI16 read int16

func (*BufferX) ReadI32

func (b *BufferX) ReadI32() (int32, error)

ReadI32 read int32

func (*BufferX) ReadI64

func (b *BufferX) ReadI64() (int64, error)

ReadI64 read int64

func (*BufferX) ReadLimitString

func (b *BufferX) ReadLimitString(limit uint32) (string, error)

ReadLimitString read limit size string

func (*BufferX) ReadN

func (b *BufferX) ReadN(n int) ([]byte, error)

ReadN read n length buffer

func (*BufferX) ReadString

func (b *BufferX) ReadString() (string, error)

ReadString read string

func (*BufferX) ReadU16

func (b *BufferX) ReadU16() (uint16, error)

ReadU16 read uint16

func (*BufferX) ReadU32

func (b *BufferX) ReadU32() (uint32, error)

ReadU32 read uint32

func (*BufferX) ReadU64

func (b *BufferX) ReadU64() (uint64, error)

ReadU64 read uint64

func (*BufferX) ReadU8

func (b *BufferX) ReadU8() (byte, error)

ReadU8 read a byte

func (*BufferX) ReadVarI32

func (b *BufferX) ReadVarI32() (int32, error)

ReadVarI32 read variant int32

func (*BufferX) ReadVarI64

func (b *BufferX) ReadVarI64() (int64, error)

ReadVarI64 read variant int64

func (*BufferX) ReadVarU32

func (b *BufferX) ReadVarU32() (uint32, error)

ReadVarU32 read variant uint32

func (*BufferX) ReadVarU64

func (b *BufferX) ReadVarU64() (uint64, error)

ReadVarU64 read variant uint64

func (*BufferX) Reset

func (b *BufferX) Reset()

Reset clear all buffer bytes

func (*BufferX) Write

func (b *BufferX) Write(p []byte)

write to a buffer

func (*BufferX) WriteBool

func (b *BufferX) WriteBool(v bool)

WriteBool write a bool

func (*BufferX) WriteF64

func (b *BufferX) WriteF64(v float64)

WriteF64 write float64

func (*BufferX) WriteI16

func (b *BufferX) WriteI16(v int16)

WriteI16 write int16

func (*BufferX) WriteI32

func (b *BufferX) WriteI32(v int32)

WriteI32 write int32

func (*BufferX) WriteI64

func (b *BufferX) WriteI64(v int64)

WriteI64 write int64

func (*BufferX) WriteLimitString

func (b *BufferX) WriteLimitString(limit uint32, val string) error

WriteLimitString write limit size string

func (*BufferX) WriteString

func (b *BufferX) WriteString(val string)

WriteString write string

func (*BufferX) WriteU16

func (b *BufferX) WriteU16(v uint16)

WriteU16 write uint16

func (*BufferX) WriteU32

func (b *BufferX) WriteU32(v uint32)

WriteU32 write uint32

func (*BufferX) WriteU64

func (b *BufferX) WriteU64(v uint64)

WriteU64 write uint64

func (*BufferX) WriteU8

func (b *BufferX) WriteU8(v byte)

WriteU8 write a byte

func (*BufferX) WriteVarI32

func (b *BufferX) WriteVarI32(v int32)

WriteVarI32 write variant int32

func (*BufferX) WriteVarI64

func (b *BufferX) WriteVarI64(v int64)

WriteVarI64 write variant int64

func (*BufferX) WriteVarU32

func (b *BufferX) WriteVarU32(v uint32)

WriteVarU32 write variant uint32

func (*BufferX) WriteVarU64

func (b *BufferX) WriteVarU64(v uint64)

WriteVarU64 write variant uint64

func (*BufferX) ZReadN

func (b *BufferX) ZReadN(n int) ([]byte, error)

ZReadN read n length buffer - no copy

type IBufferX

type IBufferX interface {
	//Len : length
	Len() int
	//Read specific p, if length is not enough, return error
	Read(p []byte) error
	//ReadN n bytes, if length is not enough, return error
	ReadN(n int) ([]byte, error)
	//Write to buffer
	Write(p []byte)
	//Bytes left bytes
	Bytes() []byte
	//Reset reset cursor
	Reset()

	//ReWrite a buffer
	ReWrite(pos int, p []byte)
	//ReWriteU32 rewrite with a specific pos
	ReWriteU32(pos int, v uint32)

	//ReadU8 read byte
	ReadU8() (byte, error)
	//WriteU8 write byte
	WriteU8(byte)

	//ReadBool read bool
	ReadBool() (bool, error)
	//WriteBool write bool
	WriteBool(bool)

	//ReadLimitString read string
	ReadLimitString(limit uint32) (string, error)
	//WriteLimitString write string
	WriteLimitString(limit uint32, val string) error

	//ReadString read string
	ReadString() (string, error)
	//WriteString write string
	WriteString(val string)

	//ReadU16 read uint16
	ReadU16() (uint16, error)
	//WriteU16 write uint16
	WriteU16(uint16)

	//ReadI16 read int16
	ReadI16() (int16, error)
	//WriteI16 write int16
	WriteI16(int16)

	//ReadU32 read uint32
	ReadU32() (uint32, error)
	//WriteU32 write uint32
	WriteU32(uint32)

	//ReadI32 read int32
	ReadI32() (int32, error)
	//WriteI32 write int32
	WriteI32(int32)

	//ReadU64 read uint64
	ReadU64() (uint64, error)
	//WriteU64 write uint64
	WriteU64(uint64)

	//ReadI64 read int64
	ReadI64() (int64, error)
	//WriteI64 write int64
	WriteI64(int64)

	//ReadF64 read float64
	ReadF64() (float64, error)
	//WriteF64 write float64
	WriteF64(float64)
}

IBufferX buffer interface

type IReaderX

type IReaderX interface {
	//Read : read specific p, if length is not enough, return error
	Read(p []byte) error
	//ReadN : read n bytes, if length is not enough, return error
	ReadN(n int) ([]byte, error)

	ZReadN(n int) ([]byte, error)

	//ReadByte : read byte
	ReadByte() (byte, error)
	//ReadBool : read bool
	ReadBool() (bool, error)

	//ReadLimitString : read string
	ReadLimitString(limit uint32) (string, error)

	//ReadString : read string
	ReadString() (string, error)

	//ReadU16 : read uint16
	ReadU16() (uint16, error)

	//ReadI16 : read int16
	ReadI16() (int16, error)

	//ReadU32 : read uint32
	ReadU32() (uint32, error)

	//ReadI32 : read int32
	ReadI32() (int32, error)

	//ReadU64 : read uint64
	ReadU64() (uint64, error)

	//ReadI64 : read int64
	ReadI64() (int64, error)

	//ReadF64 : read float64
	ReadF64() (float64, error)
}

IReaderX reader interface

type ReaderX

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

ReaderX buffer implement

func NewReaderX

func NewReaderX(reader io.Reader) *ReaderX

NewReaderX new with io.reader

func (*ReaderX) Read

func (b *ReaderX) Read(p []byte) error

Read : read to a buffer

func (*ReaderX) ReadBool

func (b *ReaderX) ReadBool() (bool, error)

ReadBool read a bool

func (*ReaderX) ReadByte

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

ReadByte read a byte

func (*ReaderX) ReadF64

func (b *ReaderX) ReadF64() (float64, error)

ReadF64 read float64

func (*ReaderX) ReadI16

func (b *ReaderX) ReadI16() (int16, error)

ReadI16 read int16

func (*ReaderX) ReadI32

func (b *ReaderX) ReadI32() (int32, error)

ReadI32 read int32

func (*ReaderX) ReadI64

func (b *ReaderX) ReadI64() (int64, error)

ReadI64 read int64

func (*ReaderX) ReadLimitString

func (b *ReaderX) ReadLimitString(limit uint32) (string, error)

ReadLimitString read limit size string

func (*ReaderX) ReadN

func (b *ReaderX) ReadN(n int) ([]byte, error)

ReadN read n length buffer

func (*ReaderX) ReadString

func (b *ReaderX) ReadString() (string, error)

ReadString read string

func (*ReaderX) ReadU16

func (b *ReaderX) ReadU16() (uint16, error)

ReadU16 read uint16

func (*ReaderX) ReadU32

func (b *ReaderX) ReadU32() (uint32, error)

ReadU32 read uint32

func (*ReaderX) ReadU64

func (b *ReaderX) ReadU64() (uint64, error)

ReadU64 read uint64

func (*ReaderX) ZReadN

func (b *ReaderX) ZReadN(n int) ([]byte, error)

ZReadN read n length buffer - no copy

Jump to

Keyboard shortcuts

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