Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion common/buf/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ func (b *Buffer) IsFull() bool {
func (b *Buffer) Write(data []byte) (int, error) {
nBytes := copy(b.v[b.end:], data)
b.end += int32(nBytes)
if nBytes != len(data) {
return nBytes, errors.New("incomplete write")
}
return nBytes, nil
}

Expand Down Expand Up @@ -306,10 +309,11 @@ func (b *Buffer) Read(data []byte) (int, error) {
nBytes := copy(data, b.v[b.start:b.end])
if int32(nBytes) == b.Len() {
b.Clear()
return nBytes, nil
} else {
b.start += int32(nBytes)
return nBytes, errors.New("incomplete read")
}
return nBytes, nil
}

// ReadFrom implements io.ReaderFrom.
Expand Down
10 changes: 10 additions & 0 deletions common/buf/multi_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,22 @@ func Compact(mb MultiBuffer) MultiBuffer {

mb2 := make(MultiBuffer, 0, len(mb))
last := mb[0]
if last.start != 0 {
b := New()
common.Must2(b.ReadFrom(last))
last = b
}

for i := 1; i < len(mb); i++ {
curr := mb[i]
if last.Len()+curr.Len() > Size {
mb2 = append(mb2, last)
last = curr
if last.start != 0 {
b := New()
common.Must2(b.ReadFrom(last))
last = b
}
} else {
common.Must2(last.ReadFrom(curr))
curr.Release()
Expand Down
Loading