v2ray-core/common/buf/multi_buffer.go

266 lines
5.4 KiB
Go
Raw Normal View History

2017-04-15 19:07:23 +00:00
package buf
2017-11-04 00:33:28 +00:00
import (
"io"
"net"
2017-11-07 10:40:12 +00:00
"v2ray.com/core/common"
2017-11-04 00:33:28 +00:00
"v2ray.com/core/common/errors"
2018-04-08 21:22:55 +00:00
"v2ray.com/core/common/serial"
2017-11-04 00:33:28 +00:00
)
2017-04-15 19:07:23 +00:00
2017-11-07 15:53:02 +00:00
// ReadAllToMultiBuffer reads all content from the reader into a MultiBuffer, until EOF.
2017-11-04 00:33:28 +00:00
func ReadAllToMultiBuffer(reader io.Reader) (MultiBuffer, error) {
2017-11-08 23:55:28 +00:00
mb := NewMultiBufferCap(128)
2017-11-04 00:33:28 +00:00
2018-04-13 11:54:36 +00:00
if _, err := mb.ReadFrom(reader); err != nil {
mb.Release()
return nil, err
2017-11-04 00:33:28 +00:00
}
2018-04-13 11:54:36 +00:00
return mb, nil
2017-11-04 00:33:28 +00:00
}
2018-04-01 22:25:42 +00:00
// ReadSizeToMultiBuffer reads specific number of bytes from reader into a MultiBuffer.
func ReadSizeToMultiBuffer(reader io.Reader, size int32) (MultiBuffer, error) {
mb := NewMultiBufferCap(32)
for size > 0 {
bSize := size
if bSize > Size {
bSize = Size
}
2018-04-02 20:01:55 +00:00
b := NewSize(bSize)
2018-04-01 22:44:47 +00:00
if err := b.Reset(ReadFullFrom(reader, bSize)); err != nil {
2018-04-01 22:25:42 +00:00
mb.Release()
return nil, err
}
size -= bSize
mb.Append(b)
}
return mb, nil
}
2017-11-07 10:40:12 +00:00
// ReadAllToBytes reads all content from the reader into a byte array, until EOF.
2017-11-04 00:33:28 +00:00
func ReadAllToBytes(reader io.Reader) ([]byte, error) {
mb, err := ReadAllToMultiBuffer(reader)
if err != nil {
return nil, err
}
if mb.Len() == 0 {
return nil, nil
}
2017-11-04 00:33:28 +00:00
b := make([]byte, mb.Len())
2017-11-07 10:40:12 +00:00
common.Must2(mb.Read(b))
2017-11-04 00:33:28 +00:00
mb.Release()
return b, nil
}
2017-04-23 17:16:56 +00:00
// MultiBuffer is a list of Buffers. The order of Buffer matters.
2017-04-15 19:07:23 +00:00
type MultiBuffer []*Buffer
2017-11-08 23:55:28 +00:00
// NewMultiBufferCap creates a new MultiBuffer instance.
2018-04-02 18:00:50 +00:00
func NewMultiBufferCap(capacity int32) MultiBuffer {
2017-11-08 23:55:28 +00:00
return MultiBuffer(make([]*Buffer, 0, capacity))
2017-04-15 19:07:23 +00:00
}
2017-04-23 17:16:56 +00:00
// NewMultiBufferValue wraps a list of Buffers into MultiBuffer.
2017-04-15 19:07:23 +00:00
func NewMultiBufferValue(b ...*Buffer) MultiBuffer {
return MultiBuffer(b)
}
2017-11-07 14:02:21 +00:00
// Append appends buffer to the end of this MultiBuffer
2017-04-23 17:16:56 +00:00
func (mb *MultiBuffer) Append(buf *Buffer) {
2018-03-29 19:40:23 +00:00
if buf != nil {
*mb = append(*mb, buf)
}
2017-04-15 19:07:23 +00:00
}
2017-11-07 14:02:21 +00:00
// AppendMulti appends a MultiBuffer to the end of this one.
2017-04-23 17:16:56 +00:00
func (mb *MultiBuffer) AppendMulti(buf MultiBuffer) {
*mb = append(*mb, buf...)
2017-04-15 19:07:23 +00:00
}
2017-12-13 14:55:39 +00:00
// Copy copied the beginning part of the MultiBuffer into the given byte array.
2017-04-24 23:56:08 +00:00
func (mb MultiBuffer) Copy(b []byte) int {
total := 0
for _, bb := range mb {
nBytes := copy(b[total:], bb.Bytes())
total += nBytes
2018-04-02 18:00:50 +00:00
if int32(nBytes) < bb.Len() {
2017-04-24 23:56:08 +00:00
break
}
}
return total
}
2018-04-13 11:54:36 +00:00
// ReadFrom implements io.ReaderFrom.
func (mb *MultiBuffer) ReadFrom(reader io.Reader) (int64, error) {
totalBytes := int64(0)
for {
b := New()
err := b.Reset(ReadFrom(reader))
if b.IsEmpty() {
b.Release()
} else {
mb.Append(b)
}
totalBytes += int64(b.Len())
if err != nil {
if errors.Cause(err) == io.EOF {
return totalBytes, nil
}
return totalBytes, err
}
}
}
2017-11-07 14:02:21 +00:00
// Read implements io.Reader.
2017-04-15 19:07:23 +00:00
func (mb *MultiBuffer) Read(b []byte) (int, error) {
2018-04-08 21:22:55 +00:00
if mb.Len() == 0 {
return 0, io.EOF
}
2017-04-15 19:07:23 +00:00
endIndex := len(*mb)
totalBytes := 0
for i, bb := range *mb {
2017-04-19 19:27:21 +00:00
nBytes, _ := bb.Read(b)
2017-04-15 19:07:23 +00:00
totalBytes += nBytes
b = b[nBytes:]
if bb.IsEmpty() {
bb.Release()
2017-11-07 15:28:39 +00:00
(*mb)[i] = nil
2017-04-15 19:07:23 +00:00
} else {
endIndex = i
break
}
}
*mb = (*mb)[endIndex:]
return totalBytes, nil
2018-04-13 11:54:36 +00:00
}
// WriteTo implements io.WriterTo.
func (mb *MultiBuffer) WriteTo(writer io.Writer) (int64, error) {
defer mb.Release()
totalBytes := int64(0)
for _, b := range *mb {
nBytes, err := writer.Write(b.Bytes())
totalBytes += int64(nBytes)
if err != nil {
return totalBytes, err
}
}
return totalBytes, nil
2017-04-15 19:07:23 +00:00
}
2017-11-07 14:02:21 +00:00
// Write implements io.Writer.
2018-04-08 21:22:55 +00:00
func (mb *MultiBuffer) Write(b []byte) (int, error) {
totalBytes := len(b)
2017-05-01 22:28:16 +00:00
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)
}
2018-04-08 21:22:55 +00:00
return totalBytes, nil
2017-05-01 22:28:16 +00:00
}
2018-04-16 22:31:10 +00:00
// WriteMultiBuffer implements Writer.
func (mb *MultiBuffer) WriteMultiBuffer(b MultiBuffer) error {
*mb = append(*mb, b...)
return nil
}
2017-04-23 17:16:56 +00:00
// Len returns the total number of bytes in the MultiBuffer.
2018-04-18 09:45:40 +00:00
func (mb *MultiBuffer) Len() int32 {
if mb == nil {
return 0
}
2018-04-02 18:00:50 +00:00
size := int32(0)
2018-04-18 09:45:40 +00:00
for _, b := range *mb {
2017-04-15 19:07:23 +00:00
size += b.Len()
}
return size
}
2017-04-23 17:16:56 +00:00
// IsEmpty return true if the MultiBuffer has no content.
2017-04-15 19:07:23 +00:00
func (mb MultiBuffer) IsEmpty() bool {
for _, b := range mb {
if !b.IsEmpty() {
return false
}
}
return true
}
2017-04-23 17:16:56 +00:00
// Release releases all Buffers in the MultiBuffer.
2017-11-07 10:40:12 +00:00
func (mb *MultiBuffer) Release() {
for i, b := range *mb {
2017-04-15 19:07:23 +00:00
b.Release()
2017-11-07 10:40:12 +00:00
(*mb)[i] = nil
2017-04-15 19:07:23 +00:00
}
2017-11-08 23:55:28 +00:00
*mb = nil
2017-04-15 19:07:23 +00:00
}
2017-04-16 20:30:29 +00:00
2018-04-08 21:22:55 +00:00
func (mb MultiBuffer) String() string {
v := make([]interface{}, len(mb))
for i, b := range mb {
v[i] = b
}
return serial.Concat(v...)
}
2017-04-23 17:16:56 +00:00
// ToNetBuffers converts this MultiBuffer to net.Buffers. The return net.Buffers points to the same content of the MultiBuffer.
2017-04-16 20:30:29 +00:00
func (mb MultiBuffer) ToNetBuffers() net.Buffers {
bs := make([][]byte, len(mb))
for i, b := range mb {
bs[i] = b.Bytes()
}
return bs
}
2017-12-13 14:55:39 +00:00
// SliceBySize splits the beginning of this MultiBuffer into another one, for at most size bytes.
2018-04-01 22:44:47 +00:00
func (mb *MultiBuffer) SliceBySize(size int32) MultiBuffer {
2017-11-08 23:55:28 +00:00
slice := NewMultiBufferCap(10)
2018-04-02 18:00:50 +00:00
sliceSize := int32(0)
endIndex := len(*mb)
for i, b := range *mb {
2018-04-02 18:00:50 +00:00
if b.Len()+sliceSize > size {
endIndex = i
break
}
2017-04-19 19:27:21 +00:00
sliceSize += b.Len()
slice.Append(b)
2017-11-07 10:40:12 +00:00
(*mb)[i] = nil
}
*mb = (*mb)[endIndex:]
if endIndex == 0 && len(*mb) > 0 {
2018-04-02 20:01:55 +00:00
b := NewSize(size)
common.Must(b.Reset(ReadFullFrom((*mb)[0], size)))
return NewMultiBufferValue(b)
}
return slice
}
2017-05-01 22:28:16 +00:00
2017-11-08 17:14:44 +00:00
// SplitFirst splits out the first Buffer in this MultiBuffer.
2017-05-01 22:28:16 +00:00
func (mb *MultiBuffer) SplitFirst() *Buffer {
if len(*mb) == 0 {
return nil
}
b := (*mb)[0]
2017-11-07 10:40:12 +00:00
(*mb)[0] = nil
2017-05-01 22:28:16 +00:00
*mb = (*mb)[1:]
return b
}