1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-01 16:57:46 -04:00
v2fly/common/buf/multi_buffer.go

298 lines
6.3 KiB
Go
Raw Normal View History

2017-04-15 15:07:23 -04:00
package buf
2017-11-03 20:33:28 -04:00
import (
"io"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/errors"
"github.com/v2fly/v2ray-core/v4/common/serial"
2017-11-03 20:33:28 -04:00
)
2017-04-15 15:07:23 -04:00
2017-11-07 05:40:12 -05:00
// ReadAllToBytes reads all content from the reader into a byte array, until EOF.
2017-11-03 20:33:28 -04:00
func ReadAllToBytes(reader io.Reader) ([]byte, error) {
2018-11-18 13:36:36 -05:00
mb, err := ReadFrom(reader)
2017-11-03 20:33:28 -04:00
if err != nil {
return nil, err
}
if mb.Len() == 0 {
return nil, nil
}
2017-11-03 20:33:28 -04:00
b := make([]byte, mb.Len())
mb, _ = SplitBytes(mb, b)
2018-11-17 16:45:07 -05:00
ReleaseMulti(mb)
2017-11-03 20:33:28 -04:00
return b, nil
}
2017-04-23 13:16:56 -04:00
// MultiBuffer is a list of Buffers. The order of Buffer matters.
2017-04-15 15:07:23 -04:00
type MultiBuffer []*Buffer
2018-11-17 03:12:20 -05:00
// MergeMulti merges content from src to dest, and returns the new address of dest and src
func MergeMulti(dest MultiBuffer, src MultiBuffer) (MultiBuffer, MultiBuffer) {
dest = append(dest, src...)
for idx := range src {
src[idx] = nil
2018-03-29 15:40:23 -04:00
}
2018-11-17 03:12:20 -05:00
return dest, src[:0]
2017-04-15 15:07:23 -04:00
}
2018-11-18 13:57:29 -05:00
// MergeBytes merges the given bytes into MultiBuffer and return the new address of the merged MultiBuffer.
2018-11-18 13:36:36 -05:00
func MergeBytes(dest MultiBuffer, src []byte) MultiBuffer {
n := len(dest)
if n > 0 && !(dest)[n-1].IsFull() {
nBytes, _ := (dest)[n-1].Write(src)
src = src[nBytes:]
}
for len(src) > 0 {
b := New()
nBytes, _ := b.Write(src)
src = src[nBytes:]
dest = append(dest, b)
}
return dest
}
2018-11-17 16:45:07 -05:00
// ReleaseMulti release all content of the MultiBuffer, and returns an empty MultiBuffer.
func ReleaseMulti(mb MultiBuffer) MultiBuffer {
for i := range mb {
mb[i].Release()
mb[i] = nil
}
return mb[:0]
}
2017-12-13 09:55:39 -05:00
// Copy copied the beginning part of the MultiBuffer into the given byte array.
2017-04-24 19:56:08 -04: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 14:00:50 -04:00
if int32(nBytes) < bb.Len() {
2017-04-24 19:56:08 -04:00
break
}
}
return total
}
2018-11-18 13:36:36 -05:00
// ReadFrom reads all content from reader until EOF.
func ReadFrom(reader io.Reader) (MultiBuffer, error) {
mb := make(MultiBuffer, 0, 16)
2018-04-13 07:54:36 -04:00
for {
b := New()
2018-11-02 10:01:33 -04:00
_, err := b.ReadFullFrom(reader, Size)
2018-04-13 07:54:36 -04:00
if b.IsEmpty() {
b.Release()
} else {
2018-11-18 13:36:36 -05:00
mb = append(mb, b)
2018-04-13 07:54:36 -04:00
}
if err != nil {
2018-07-30 16:45:06 -04:00
if errors.Cause(err) == io.EOF || errors.Cause(err) == io.ErrUnexpectedEOF {
2018-11-18 13:36:36 -05:00
return mb, nil
2018-04-13 07:54:36 -04:00
}
2018-11-18 13:36:36 -05:00
return mb, err
2018-04-13 07:54:36 -04:00
}
}
}
2018-11-18 13:57:29 -05:00
// SplitBytes splits the given amount of bytes from the beginning of the MultiBuffer.
// It returns the new address of MultiBuffer leftover, and number of bytes written into the input byte slice.
func SplitBytes(mb MultiBuffer, b []byte) (MultiBuffer, int) {
2017-04-15 15:07:23 -04:00
totalBytes := 0
2018-11-18 18:33:00 -05:00
endIndex := -1
for i := range mb {
pBuffer := mb[i]
nBytes, _ := pBuffer.Read(b)
2017-04-15 15:07:23 -04:00
totalBytes += nBytes
b = b[nBytes:]
2018-11-18 18:33:00 -05:00
if !pBuffer.IsEmpty() {
endIndex = i
2017-04-15 15:07:23 -04:00
break
}
2018-11-18 18:33:00 -05:00
pBuffer.Release()
mb[i] = nil
}
if endIndex == -1 {
mb = mb[:0]
} else {
mb = mb[endIndex:]
2017-05-01 18:28:16 -04:00
}
2018-04-08 17:22:55 -04:00
return mb, totalBytes
2018-04-16 18:31:10 -04:00
}
2019-01-05 18:34:38 -05:00
// SplitFirstBytes splits the first buffer from MultiBuffer, and then copy its content into the given slice.
func SplitFirstBytes(mb MultiBuffer, p []byte) (MultiBuffer, int) {
mb, b := SplitFirst(mb)
if b == nil {
return mb, 0
}
n := copy(p, b.Bytes())
b.Release()
return mb, n
}
2018-12-27 10:36:48 -05:00
// Compact returns another MultiBuffer by merging all content of the given one together.
func Compact(mb MultiBuffer) MultiBuffer {
if len(mb) == 0 {
return mb
}
mb2 := make(MultiBuffer, 0, len(mb))
last := mb[0]
for i := 1; i < len(mb); i++ {
curr := mb[i]
if last.Len()+curr.Len() > Size {
mb2 = append(mb2, last)
last = curr
} else {
common.Must2(last.ReadFrom(curr))
curr.Release()
}
}
mb2 = append(mb2, last)
return mb2
}
2018-11-18 13:57:29 -05:00
// SplitFirst splits the first Buffer from the beginning of the MultiBuffer.
func SplitFirst(mb MultiBuffer) (MultiBuffer, *Buffer) {
if len(mb) == 0 {
return mb, nil
}
b := mb[0]
mb[0] = nil
mb = mb[1:]
return mb, b
}
2018-11-18 14:16:14 -05:00
// SplitSize splits the beginning of the MultiBuffer into another one, for at most size bytes.
func SplitSize(mb MultiBuffer, size int32) (MultiBuffer, MultiBuffer) {
if len(mb) == 0 {
return mb, nil
}
if mb[0].Len() > size {
b := New()
copy(b.Extend(size), mb[0].BytesTo(size))
mb[0].Advance(size)
return mb, MultiBuffer{b}
}
totalBytes := int32(0)
var r MultiBuffer
2018-11-18 16:12:31 -05:00
endIndex := -1
2018-11-18 14:16:14 -05:00
for i := range mb {
if totalBytes+mb[i].Len() > size {
endIndex = i
break
}
2018-11-18 16:12:31 -05:00
totalBytes += mb[i].Len()
2018-11-18 14:16:14 -05:00
r = append(r, mb[i])
mb[i] = nil
}
2018-11-18 16:12:31 -05:00
if endIndex == -1 {
2018-11-18 14:16:14 -05:00
// To reuse mb array
mb = mb[:0]
} else {
mb = mb[endIndex:]
}
return mb, r
}
2018-12-27 11:00:34 -05:00
// WriteMultiBuffer writes all buffers from the MultiBuffer to the Writer one by one, and return error if any, with leftover MultiBuffer.
func WriteMultiBuffer(writer io.Writer, mb MultiBuffer) (MultiBuffer, error) {
for {
mb2, b := SplitFirst(mb)
mb = mb2
if b == nil {
break
}
_, err := writer.Write(b.Bytes())
b.Release()
if err != nil {
2018-12-27 13:59:49 -05:00
return mb, err
2018-12-27 11:00:34 -05:00
}
}
return nil, nil
}
2017-04-23 13:16:56 -04:00
// Len returns the total number of bytes in the MultiBuffer.
2018-11-16 05:29:16 -05:00
func (mb MultiBuffer) Len() int32 {
2018-04-18 05:45:40 -04:00
if mb == nil {
return 0
}
2018-04-02 14:00:50 -04:00
size := int32(0)
2018-11-16 05:29:16 -05:00
for _, b := range mb {
2017-04-15 15:07:23 -04:00
size += b.Len()
}
return size
}
2017-04-23 13:16:56 -04:00
// IsEmpty return true if the MultiBuffer has no content.
2017-04-15 15:07:23 -04:00
func (mb MultiBuffer) IsEmpty() bool {
for _, b := range mb {
if !b.IsEmpty() {
return false
}
}
return true
}
2018-11-17 16:45:07 -05:00
// String returns the content of the MultiBuffer in string.
2018-04-08 17:22:55 -04:00
func (mb MultiBuffer) String() string {
v := make([]interface{}, len(mb))
for i, b := range mb {
v[i] = b
}
return serial.Concat(v...)
}
2018-11-18 13:57:29 -05:00
// MultiBufferContainer is a ReadWriteCloser wrapper over MultiBuffer.
2018-11-18 13:36:36 -05:00
type MultiBufferContainer struct {
MultiBuffer
}
2018-11-18 13:57:29 -05:00
// Read implements io.Reader.
2018-11-18 13:36:36 -05:00
func (c *MultiBufferContainer) Read(b []byte) (int, error) {
if c.MultiBuffer.IsEmpty() {
return 0, io.EOF
}
mb, nBytes := SplitBytes(c.MultiBuffer, b)
2018-11-18 13:36:36 -05:00
c.MultiBuffer = mb
return nBytes, nil
2018-11-18 13:36:36 -05:00
}
2018-11-18 13:57:29 -05:00
// ReadMultiBuffer implements Reader.
2018-11-18 13:36:36 -05:00
func (c *MultiBufferContainer) ReadMultiBuffer() (MultiBuffer, error) {
mb := c.MultiBuffer
c.MultiBuffer = nil
return mb, nil
}
2018-11-18 13:57:29 -05:00
// Write implements io.Writer.
2018-11-18 13:36:36 -05:00
func (c *MultiBufferContainer) Write(b []byte) (int, error) {
c.MultiBuffer = MergeBytes(c.MultiBuffer, b)
return len(b), nil
}
2018-11-18 13:57:29 -05:00
// WriteMultiBuffer implement Writer.
2018-11-18 13:36:36 -05:00
func (c *MultiBufferContainer) WriteMultiBuffer(b MultiBuffer) error {
mb, _ := MergeMulti(c.MultiBuffer, b)
c.MultiBuffer = mb
return nil
}
2018-11-18 13:57:29 -05:00
// Close implement io.Closer.
2018-11-18 13:36:36 -05:00
func (c *MultiBufferContainer) Close() error {
c.MultiBuffer = ReleaseMulti(c.MultiBuffer)
return nil
}