Documentation
¶
Index ¶
- Constants
- Variables
- func Alloc(size int) []byte
- func Format(b int64) string
- func Free(data []byte) bool
- func Parse(value string) (i int64, err error)
- func ReadBool(r io.Reader) (bool, error)
- func ReadByte(r io.Reader) (byte, error)
- func ReadFloat32BE(r io.Reader, out *float32) error
- func ReadFloat32LE(r io.Reader, out *float32) error
- func ReadFloat64BE(r io.Reader, out *float64) error
- func ReadFloat64LE(r io.Reader, out *float64) error
- func ReadInt16BE(r io.Reader, out *int16) error
- func ReadInt16LE(r io.Reader, out *int16) error
- func ReadInt32BE(r io.Reader, out *int32) error
- func ReadInt32LE(r io.Reader, out *int32) error
- func ReadInt64BE(r io.Reader, out *int64) error
- func ReadInt64LE(r io.Reader, out *int64) error
- func ReadString(r io.Reader, out *string) error
- func ReadStringByLen(r io.Reader, len int, out *string) error
- func ReadUint16BE(r io.Reader, out *uint16) error
- func ReadUint16LE(r io.Reader, out *uint16) error
- func ReadUint32BE(r io.Reader, out *uint32) error
- func ReadUint32LE(r io.Reader, out *uint32) error
- func ReadUint64BE(r io.Reader, out *uint64) error
- func ReadUint64LE(r io.Reader, out *uint64) error
- func ReadUvarint16(r io.ByteReader, out *uint16) error
- func ReadUvarint32(r io.ByteReader, out *uint32) error
- func ReadUvarint64(r io.ByteReader, out *uint64) error
- func ReadVarint16(r io.ByteReader, out *int16) error
- func ReadVarint32(r io.ByteReader, out *int32) error
- func ReadVarint64(r io.ByteReader, out *int64) error
- func SetChunkSize(size int)
- func SetPool(p *Pool)
- func Write(w io.Writer, v []byte) error
- func WriteBool(w io.Writer, v bool) error
- func WriteByte(w io.Writer, v byte) error
- func WriteFloat32BE(w io.Writer, v float32) error
- func WriteFloat32LE(w io.Writer, v float32) error
- func WriteFloat64BE(w io.Writer, v float64) error
- func WriteFloat64LE(w io.Writer, v float64) error
- func WriteInt16BE(w io.Writer, v int16) error
- func WriteInt16LE(w io.Writer, v int16) error
- func WriteInt32BE(w io.Writer, v int32) error
- func WriteInt32LE(w io.Writer, v int32) error
- func WriteInt64BE(w io.Writer, v int64) error
- func WriteInt64LE(w io.Writer, v int64) error
- func WriteString(w io.Writer, s string) error
- func WriteUint16BE(w io.Writer, v uint16) error
- func WriteUint16LE(w io.Writer, v uint16) error
- func WriteUint32BE(w io.Writer, v uint32) error
- func WriteUint32LE(w io.Writer, v uint32) error
- func WriteUint64BE(w io.Writer, v uint64) error
- func WriteUint64LE(w io.Writer, v uint64) error
- func WriteUvarint16(w io.Writer, v uint16) error
- func WriteUvarint32(w io.Writer, v uint32) error
- func WriteUvarint64(w io.Writer, v uint64) error
- func WriteVarint16(w io.Writer, v int16) error
- func WriteVarint32(w io.Writer, v int32) error
- func WriteVarint64(w io.Writer, v int64) error
- func Writef(w io.Writer, format string, args ...interface{}) error
- func Writev(w io.Writer, format string, args ...interface{}) error
- type Buffer
- type BytePeeker
- type Peeker
- type Pool
- type ReaderFromOnce
Constants ¶
const ( KB MB GB TB PB EB )
const (
// NPOS 无效索引
NPOS = -1
)
Variables ¶
var ( ErrNotSupport = errors.New("[buffer] not support") ErrInvalidParam = errors.New("[buffer] invalid param") )
Functions ¶
func Format ¶
Format formats bytes integer to human readable string. For example, 31323 bytes will return 30.59KB.
func Parse ¶
Parse parses human readable bytes string to bytes integer. For example, 6GB (6G is also valid) will return 6442450944.
func ReadInt16BE ¶
------------------------------------------------ BigEndian encoding ------------------------------------------------ ReadInt16BE .
func ReadInt16LE ¶
------------------------------------------------ LittleEndian encoding ------------------------------------------------ ReadInt16LE .
func ReadString ¶
ReadString 读取string,要求以长度+content编码
func ReadStringByLen ¶
ReadStringByLen 通过长度读取string
func WriteInt16BE ¶
------------------------------------------------ BigEndian encoding ------------------------------------------------
func WriteInt16LE ¶
------------------------------------------------ LittleEndian encoding ------------------------------------------------
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
type BytePeeker ¶
BytePeeker 类似io.ByteReader,但不会修改游标,无数据会返回io.EOF错误
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