1 Star 0 Fork 0

BUPT-ZKJC/fabric

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
protobuf_util.go 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
MJL 提交于 2021-08-06 18:37 +08:00 . first commit
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package blkstorage
import (
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)
// buffer provides a wrapper on top of proto.Buffer.
// The purpose of this wrapper is to get to know the current position in the []byte
type buffer struct {
buf *proto.Buffer
position int
}
// newBuffer constructs a new instance of Buffer
func newBuffer(b []byte) *buffer {
return &buffer{proto.NewBuffer(b), 0}
}
// DecodeVarint wraps the actual method and updates the position
func (b *buffer) DecodeVarint() (uint64, error) {
val, err := b.buf.DecodeVarint()
if err == nil {
b.position += proto.SizeVarint(val)
} else {
err = errors.Wrap(err, "error decoding varint with proto.Buffer")
}
return val, err
}
// DecodeRawBytes wraps the actual method and updates the position
func (b *buffer) DecodeRawBytes(alloc bool) ([]byte, error) {
val, err := b.buf.DecodeRawBytes(alloc)
if err == nil {
b.position += proto.SizeVarint(uint64(len(val))) + len(val)
} else {
err = errors.Wrap(err, "error decoding raw bytes with proto.Buffer")
}
return val, err
}
// GetBytesConsumed returns the offset of the current position in the underlying []byte
func (b *buffer) GetBytesConsumed() int {
return b.position
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bupt-zkjc/fabric.git
git@gitee.com:bupt-zkjc/fabric.git
bupt-zkjc
fabric
fabric
98d302355562

搜索帮助