1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
buffer.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
package sys
// ByteBuffer is an expandable buffer backed by a byte slice.
type ByteBuffer struct {
buf []byte
offset int
}
// NewByteBuffer creates a new ByteBuffer with an initial capacity of
// initialSize.
func NewByteBuffer(initialSize int) *ByteBuffer {
return &ByteBuffer{buf: make([]byte, initialSize)}
}
// Write appends the contents of p to the buffer, growing the buffer as needed.
// The return value is the length of p; err is always nil.
func (b *ByteBuffer) Write(p []byte) (int, error) {
if len(b.buf) < b.offset+len(p) {
// Create a buffer larger than needed so we don't spend lots of time
// allocating and copying.
spaceNeeded := len(b.buf) - b.offset + len(p)
largerBuf := make([]byte, 2*len(b.buf)+spaceNeeded)
copy(largerBuf, b.buf[:b.offset])
b.buf = largerBuf
}
n := copy(b.buf[b.offset:], p)
b.offset += n
return n, nil
}
// Reset resets the buffer to be empty. It retains the same underlying storage.
func (b *ByteBuffer) Reset() {
b.offset = 0
b.buf = b.buf[:cap(b.buf)]
}
// Bytes returns a slice of length b.Len() holding the bytes that have been
// written to the buffer.
func (b *ByteBuffer) Bytes() []byte {
return b.buf[:b.offset]
}
// Len returns the number of bytes that have been written to the buffer.
func (b *ByteBuffer) Len() int {
return b.offset
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v5.5.2

搜索帮助

0d507c66 1850385 C8b1a773 1850385