2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-14 11:31:28 +08:00
cmux/buffer.go

43 lines
551 B
Go
Raw Normal View History

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
}