2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-09-19 18:45:48 +08:00
cmux/buffer.go
Soheil Hassas Yeganeh fbd0877935 Use a custom buffer intead of buffers from bytes
This commit implements a new buffer that eliminates a few copies.
2015-08-01 11:59:50 -04:00

43 lines
551 B
Go

package cmux
import "io"
type buffer struct {
read int
data []byte
}
func (b *buffer) Read(p []byte) (n int, err error) {
n = len(b.data) - b.read
if n == 0 {
return 0, io.EOF
}
if len(p) < n {
n = len(p)
}
copy(p[:n], b.data[b.read:b.read+n])
b.read += n
return
}
func (b *buffer) Len() int {
return len(b.data) - b.read
}
func (b *buffer) resetRead() {
b.read = 0
}
func (b *buffer) Write(p []byte) (n int, err error) {
n = len(p)
if b.data == nil {
b.data = p[:n:n]
return
}
b.data = append(b.data, p...)
return
}