bytex

package
v0.0.0-...-680e691 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KB
	MB
	GB
	TB
	PB
	EB
)
View Source
const (
	// NPOS 无效索引
	NPOS = -1
)

Variables

View Source
var (
	ErrNotSupport   = errors.New("[buffer] not support")
	ErrInvalidParam = errors.New("[buffer] invalid param")
)

Functions

func Alloc

func Alloc(size int) []byte

Alloc 申请内存

func Format

func Format(b int64) string

Format formats bytes integer to human readable string. For example, 31323 bytes will return 30.59KB.

https://github.com/labstack/gommon

func Free

func Free(data []byte) bool

Free 释放内存

func Parse

func Parse(value string) (i int64, err error)

Parse parses human readable bytes string to bytes integer. For example, 6GB (6G is also valid) will return 6442450944.

func ReadBool

func ReadBool(r io.Reader) (bool, error)

ReadBool 读取bool

func ReadByte

func ReadByte(r io.Reader) (byte, error)

ReadByte .

func ReadFloat32BE

func ReadFloat32BE(r io.Reader, out *float32) error

ReadFloat32BE .

func ReadFloat32LE

func ReadFloat32LE(r io.Reader, out *float32) error

ReadFloat32LE

func ReadFloat64BE

func ReadFloat64BE(r io.Reader, out *float64) error

ReadFloat64BE 读取float64

func ReadFloat64LE

func ReadFloat64LE(r io.Reader, out *float64) error

ReadFloat64LE .

func ReadInt16BE

func ReadInt16BE(r io.Reader, out *int16) error

------------------------------------------------ BigEndian encoding ------------------------------------------------ ReadInt16BE .

func ReadInt16LE

func ReadInt16LE(r io.Reader, out *int16) error

------------------------------------------------ LittleEndian encoding ------------------------------------------------ ReadInt16LE .

func ReadInt32BE

func ReadInt32BE(r io.Reader, out *int32) error

ReadInt32BE .

func ReadInt32LE

func ReadInt32LE(r io.Reader, out *int32) error

ReadInt32LE .

func ReadInt64BE

func ReadInt64BE(r io.Reader, out *int64) error

ReadInt64BE .

func ReadInt64LE

func ReadInt64LE(r io.Reader, out *int64) error

ReadInt64LE .

func ReadString

func ReadString(r io.Reader, out *string) error

ReadString 读取string,要求以长度+content编码

func ReadStringByLen

func ReadStringByLen(r io.Reader, len int, out *string) error

ReadStringByLen 通过长度读取string

func ReadUint16BE

func ReadUint16BE(r io.Reader, out *uint16) error

ReadUint16BE .

func ReadUint16LE

func ReadUint16LE(r io.Reader, out *uint16) error

ReadUint16LE .

func ReadUint32BE

func ReadUint32BE(r io.Reader, out *uint32) error

ReadUint32BE .

func ReadUint32LE

func ReadUint32LE(r io.Reader, out *uint32) error

ReadUint32LE .

func ReadUint64BE

func ReadUint64BE(r io.Reader, out *uint64) error

ReadUint64BE .

func ReadUint64LE

func ReadUint64LE(r io.Reader, out *uint64) error

ReadUint64LE .

func ReadUvarint16

func ReadUvarint16(r io.ByteReader, out *uint16) error

ReadUvarint16 .

func ReadUvarint32

func ReadUvarint32(r io.ByteReader, out *uint32) error

ReadUvarint32 .

func ReadUvarint64

func ReadUvarint64(r io.ByteReader, out *uint64) error

ReadUvarint .

func ReadVarint16

func ReadVarint16(r io.ByteReader, out *int16) error

ReadVarint16 .

func ReadVarint32

func ReadVarint32(r io.ByteReader, out *int32) error

ReadVarint32 .

func ReadVarint64

func ReadVarint64(r io.ByteReader, out *int64) error

ReadVarint64 读取varint

func SetChunkSize

func SetChunkSize(size int)

SetChunkSize 修改默认分配大小,大小应是2的幂,大小标准应尽量与消息包大小保持一致

func SetPool

func SetPool(p *Pool)

SetPool 设置默认的Pool,非线程安全,仅可以在程序启动前设置

func Write

func Write(w io.Writer, v []byte) error

Write .

func WriteBool

func WriteBool(w io.Writer, v bool) error

WriteBool 写入布尔值

func WriteByte

func WriteByte(w io.Writer, v byte) error

WriteByte 写入字节

func WriteFloat32BE

func WriteFloat32BE(w io.Writer, v float32) error

func WriteFloat32LE

func WriteFloat32LE(w io.Writer, v float32) error

func WriteFloat64BE

func WriteFloat64BE(w io.Writer, v float64) error

func WriteFloat64LE

func WriteFloat64LE(w io.Writer, v float64) error

func WriteInt16BE

func WriteInt16BE(w io.Writer, v int16) error

------------------------------------------------ BigEndian encoding ------------------------------------------------

func WriteInt16LE

func WriteInt16LE(w io.Writer, v int16) error

------------------------------------------------ LittleEndian encoding ------------------------------------------------

func WriteInt32BE

func WriteInt32BE(w io.Writer, v int32) error

func WriteInt32LE

func WriteInt32LE(w io.Writer, v int32) error

func WriteInt64BE

func WriteInt64BE(w io.Writer, v int64) error

func WriteInt64LE

func WriteInt64LE(w io.Writer, v int64) error

func WriteString

func WriteString(w io.Writer, s string) error

WriteString 写入带有长度的string

func WriteUint16BE

func WriteUint16BE(w io.Writer, v uint16) error

func WriteUint16LE

func WriteUint16LE(w io.Writer, v uint16) error

func WriteUint32BE

func WriteUint32BE(w io.Writer, v uint32) error

func WriteUint32LE

func WriteUint32LE(w io.Writer, v uint32) error

func WriteUint64BE

func WriteUint64BE(w io.Writer, v uint64) error

func WriteUint64LE

func WriteUint64LE(w io.Writer, v uint64) error

func WriteUvarint16

func WriteUvarint16(w io.Writer, v uint16) error

func WriteUvarint32

func WriteUvarint32(w io.Writer, v uint32) error

func WriteUvarint64

func WriteUvarint64(w io.Writer, v uint64) error

WriteUvarint64 写入Uvarint编码数据

func WriteVarint16

func WriteVarint16(w io.Writer, v int16) error

func WriteVarint32

func WriteVarint32(w io.Writer, v int32) error

func WriteVarint64

func WriteVarint64(w io.Writer, v int64) error

WriteVarint64 写入Varint编码数据

func Writef

func Writef(w io.Writer, format string, args ...interface{}) error

Writef .

func Writev

func Writev(w io.Writer, format string, args ...interface{}) error

Writev 格式化写入字符串,会先写入长度,Uvarint编码

Types

type Buffer

type Buffer interface {
	Peeker
	BytePeeker
	io.Closer
	io.Seeker
	io.Writer
	io.Reader
	io.ByteReader
	io.ByteWriter
	io.WriterTo
	ReaderFromOnce
	Len() int                       // 数据长度
	Cap() int                       // 真实容量
	Pos() int                       // 当前位置
	Available() int                 // 可用数据大小[len-pos]
	Empty() bool                    // 是否为空[len==0]
	Bytes() []byte                  // 转为[]byte
	String() string                 // 转为字符串
	Clear()                         // 清空内存并回收复用
	Discard()                       // 丢弃当前位置之前的数据
	Append(data interface{}) error  // 在尾部追加数据,并将pos移动到末尾
	Prepend(data interface{}) error // 在头部插入数据,并将pos移动开始
	WriteN(n int)                   // 从当前位置写入n个字节,移动游标,但不会写入数据
	PeekN(n int) Buffer             // 从当前位置获取n个字符,不修改游标,数据不足返回nil
	ReadN(n int) Buffer             // 从当前位置读取n个字符,数据不足返回nil
	ReadLine() Buffer               // 从当前位置读取一行(\n|\r\n), 不存在返回nil
	IndexByte(c byte, max int) int  // 从当前位置查找byte,max<=0则查询全部,返回相对当前位置的偏移,不存在返回-1
}

Buffer 是一个比较底层的缓冲区接口,主要用于网络数据交换

特点:
1:基于链表,非连续,避免多次内存拷贝
2:基于内存池,减少gc(效果待验证),在Buffer使用完成后,需要主动调用Clear,才能回收复用内存
3:基于引用计数,便于安全的共享内存,但不支持COW(copy on write),多个buffer写入相同内存会被覆盖
4:单游标操作,区别于netty的ByteBuf的读写分离方式,这里只有一个position,使用时需要seek到正确位置
比如:一次消息的处理过程:
SeekToEnd-> 接受数据 --> SeekToStart --> 读取数据并解码处理-->循环这个过程
5:特殊函数
Append和Prepend并不基于当前游标读写,而是直接在尾部和头部修改

区别于Netty中的ByteBuf,不区分readIndex和writeIndex,只有当前位置 https://netty.io/4.1/api/index.html

+-------------+--------------+---------------+ | data(read/write) | unused bytes | +-------------+--------------+---------------+ | | | | 0 <= pos <= length <= capacity

func NewBuffer

func NewBuffer() Buffer

NewBuffer 创建Buffer,底层使用连表,非连续

type BytePeeker

type BytePeeker interface {
	PeekByte() (byte, error)
}

BytePeeker 类似io.ByteReader,但不会修改游标,无数据会返回io.EOF错误

type Peeker

type Peeker interface {
	Peek(data []byte) (int, error)
}

Peeker 类似io.Reader但不会修改游标

type Pool

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

Pool 内存池 https://studygolang.com/articles/16282 https://stackoverflow.com/questions/53613556/is-go-sync-pool-much-slower-than-make https://github.com/golang/go/issues/16323

func NewPool

func NewPool(minPow, maxPow int) *Pool

NewPool 创建Pool

func (*Pool) Get

func (p *Pool) Get(size int) []byte

Get 获取byte

func (*Pool) Put

func (p *Pool) Put(data []byte) bool

Put 释放byte

type ReaderFromOnce

type ReaderFromOnce interface {
	ReadFromOnce(r io.Reader) (n int64, err error)
}

ReaderFromOnce 从io.Reader中读取一次数据

区别于io.ReaderFrom,这里不管成功或失败,只会读取一次,
之所以这样是因为io.Reader可能是阻塞的,比如net.Conn

Jump to

Keyboard shortcuts

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