2 Star 2 Fork 6

王布衣 / gox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
binary.go 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
package struc
import (
"encoding/binary"
"io"
"reflect"
)
type byteWriter struct {
buf []byte
pos int
}
func (b byteWriter) Write(p []byte) (int, error) {
capacity := len(b.buf) - b.pos
if capacity < len(p) {
p = p[:capacity]
}
if len(p) > 0 {
copy(b.buf[b.pos:], p)
b.pos += len(p)
}
return len(p), nil
}
type binaryFallback reflect.Value
func (b binaryFallback) String() string {
return b.String()
}
func (b binaryFallback) Sizeof(val reflect.Value, options *Options) int {
return binary.Size(val.Interface())
}
func (b binaryFallback) Pack(buf []byte, val reflect.Value, options *Options) (int, error) {
tmp := byteWriter{buf: buf}
var order binary.ByteOrder = binary.BigEndian
if options.Order != nil {
order = options.Order
}
err := binary.Write(tmp, order, val.Interface())
return tmp.pos, err
}
func (b binaryFallback) Unpack(r io.Reader, val reflect.Value, options *Options) error {
var order binary.ByteOrder = binary.BigEndian
if options.Order != nil {
order = options.Order
}
return binary.Read(r, order, val.Interface())
}
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.21.1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891