mirror of
https://github.com/soheilhy/cmux.git
synced 2024-11-09 19:21:52 +08:00
fbd0877935
This commit implements a new buffer that eliminates a few copies.
43 lines
551 B
Go
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
|
|
}
|