You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/buf/multi_buffer.go

139 lines
2.6 KiB

8 years ago
package buf
8 years ago
import "net"
8 years ago
type MultiBufferWriter interface {
8 years ago
WriteMultiBuffer(MultiBuffer) error
8 years ago
}
8 years ago
type MultiBufferReader interface {
ReadMultiBuffer() (MultiBuffer, error)
}
8 years ago
// MultiBuffer is a list of Buffers. The order of Buffer matters.
8 years ago
type MultiBuffer []*Buffer
8 years ago
// NewMultiBuffer creates a new MultiBuffer instance.
8 years ago
func NewMultiBuffer() MultiBuffer {
return MultiBuffer(make([]*Buffer, 0, 128))
8 years ago
}
8 years ago
// NewMultiBufferValue wraps a list of Buffers into MultiBuffer.
8 years ago
func NewMultiBufferValue(b ...*Buffer) MultiBuffer {
return MultiBuffer(b)
}
8 years ago
func (mb *MultiBuffer) Append(buf *Buffer) {
*mb = append(*mb, buf)
8 years ago
}
8 years ago
func (mb *MultiBuffer) AppendMulti(buf MultiBuffer) {
*mb = append(*mb, buf...)
8 years ago
}
8 years ago
func (mb MultiBuffer) Copy(b []byte) int {
total := 0
for _, bb := range mb {
nBytes := copy(b[total:], bb.Bytes())
total += nBytes
if nBytes < bb.Len() {
break
}
}
return total
}
8 years ago
func (mb *MultiBuffer) Read(b []byte) (int, error) {
endIndex := len(*mb)
totalBytes := 0
for i, bb := range *mb {
8 years ago
nBytes, _ := bb.Read(b)
8 years ago
totalBytes += nBytes
b = b[nBytes:]
if bb.IsEmpty() {
bb.Release()
} else {
endIndex = i
break
}
}
*mb = (*mb)[endIndex:]
return totalBytes, nil
}
func (mb *MultiBuffer) Write(b []byte) {
n := len(*mb)
if n > 0 && !(*mb)[n-1].IsFull() {
nBytes, _ := (*mb)[n-1].Write(b)
b = b[nBytes:]
}
for len(b) > 0 {
bb := New()
nBytes, _ := bb.Write(b)
b = b[nBytes:]
mb.Append(bb)
}
}
8 years ago
// Len returns the total number of bytes in the MultiBuffer.
8 years ago
func (mb MultiBuffer) Len() int {
size := 0
for _, b := range mb {
size += b.Len()
}
return size
}
8 years ago
// IsEmpty return true if the MultiBuffer has no content.
8 years ago
func (mb MultiBuffer) IsEmpty() bool {
for _, b := range mb {
if !b.IsEmpty() {
return false
}
}
return true
}
8 years ago
// Release releases all Buffers in the MultiBuffer.
8 years ago
func (mb MultiBuffer) Release() {
for i, b := range mb {
8 years ago
b.Release()
mb[i] = nil
8 years ago
}
}
8 years ago
// ToNetBuffers converts this MultiBuffer to net.Buffers. The return net.Buffers points to the same content of the MultiBuffer.
func (mb MultiBuffer) ToNetBuffers() net.Buffers {
bs := make([][]byte, len(mb))
for i, b := range mb {
bs[i] = b.Bytes()
}
return bs
}
func (mb *MultiBuffer) SliceBySize(size int) MultiBuffer {
slice := NewMultiBuffer()
sliceSize := 0
endIndex := len(*mb)
for i, b := range *mb {
if b.Len()+sliceSize > size {
endIndex = i
break
}
8 years ago
sliceSize += b.Len()
slice.Append(b)
}
*mb = (*mb)[endIndex:]
return slice
}
func (mb *MultiBuffer) SplitFirst() *Buffer {
if len(*mb) == 0 {
return nil
}
b := (*mb)[0]
*mb = (*mb)[1:]
return b
}