2016-12-09 07:17:34 -05:00
|
|
|
package buf
|
|
|
|
|
2017-04-17 16:35:20 -04:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/errors"
|
|
|
|
)
|
2016-12-09 07:17:34 -05:00
|
|
|
|
|
|
|
// BytesToBufferReader is a Reader that adjusts its reading speed automatically.
|
|
|
|
type BytesToBufferReader struct {
|
2017-11-09 16:33:15 -05:00
|
|
|
io.Reader
|
2017-04-19 17:30:18 -04:00
|
|
|
buffer []byte
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-13 05:15:04 -05:00
|
|
|
// NewBytesToBufferReader returns a new BytesToBufferReader.
|
2017-11-08 16:23:21 -05:00
|
|
|
func NewBytesToBufferReader(reader io.Reader) Reader {
|
|
|
|
return &BytesToBufferReader{
|
2017-11-09 16:33:15 -05:00
|
|
|
Reader: reader,
|
2017-11-08 16:23:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
|
|
|
|
b := New()
|
2018-03-29 12:15:50 -04:00
|
|
|
for i := 0; i < 64; i++ {
|
2018-03-29 04:40:18 -04:00
|
|
|
err := b.Reset(ReadFrom(r.Reader))
|
|
|
|
if b.IsFull() {
|
|
|
|
r.buffer = newBytes(Size + 1)
|
|
|
|
}
|
|
|
|
if !b.IsEmpty() {
|
|
|
|
return NewMultiBufferValue(b), nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
b.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-26 09:08:37 -05:00
|
|
|
}
|
2018-03-29 12:15:50 -04:00
|
|
|
|
|
|
|
return nil, newError("Reader returns too many empty payloads.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *BytesToBufferReader) freeBuffer() {
|
|
|
|
freeBytes(r.buffer)
|
|
|
|
r.buffer = nil
|
2017-11-08 16:23:21 -05:00
|
|
|
}
|
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
// ReadMultiBuffer implements Reader.
|
|
|
|
func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
|
2017-11-08 16:23:21 -05:00
|
|
|
if r.buffer == nil {
|
|
|
|
return r.readSmall()
|
|
|
|
}
|
|
|
|
|
2018-03-29 12:15:50 -04:00
|
|
|
nBytes, err := r.Reader.Read(r.buffer)
|
|
|
|
if nBytes > 0 {
|
|
|
|
mb := NewMultiBufferCap(nBytes/Size + 1)
|
|
|
|
mb.Write(r.buffer[:nBytes])
|
2018-04-01 06:20:32 -04:00
|
|
|
if nBytes == len(r.buffer) && nBytes < int(largeSize) {
|
2018-03-16 11:32:03 -04:00
|
|
|
freeBytes(r.buffer)
|
2018-03-29 12:15:50 -04:00
|
|
|
r.buffer = newBytes(uint32(nBytes) + 1)
|
|
|
|
} else if nBytes < Size {
|
|
|
|
r.freeBuffer()
|
2017-12-08 07:23:15 -05:00
|
|
|
}
|
2018-03-29 12:15:50 -04:00
|
|
|
return mb, nil
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
2018-03-29 12:15:50 -04:00
|
|
|
|
|
|
|
r.freeBuffer()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read() returns empty payload and nil err. We don't expect this to happen, but just in case.
|
|
|
|
return r.readSmall()
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-13 05:15:04 -05:00
|
|
|
// BufferedReader is a Reader that keeps its internal buffer.
|
2017-11-09 16:33:15 -05:00
|
|
|
type BufferedReader struct {
|
2017-11-25 15:58:47 -05:00
|
|
|
stream Reader
|
|
|
|
leftOver MultiBuffer
|
|
|
|
buffered bool
|
2017-04-15 16:22:29 -04:00
|
|
|
}
|
|
|
|
|
2018-02-13 05:15:04 -05:00
|
|
|
// NewBufferedReader returns a new BufferedReader.
|
2017-11-09 16:33:15 -05:00
|
|
|
func NewBufferedReader(reader Reader) *BufferedReader {
|
2017-11-25 15:58:47 -05:00
|
|
|
return &BufferedReader{
|
2017-11-09 16:33:15 -05:00
|
|
|
stream: reader,
|
|
|
|
buffered: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 05:15:04 -05:00
|
|
|
// SetBuffered sets whether to keep the interal buffer.
|
2017-11-09 16:33:15 -05:00
|
|
|
func (r *BufferedReader) SetBuffered(f bool) {
|
|
|
|
r.buffered = f
|
2017-04-15 16:22:29 -04:00
|
|
|
}
|
|
|
|
|
2018-02-13 05:15:04 -05:00
|
|
|
// IsBuffered returns true if internal buffer is used.
|
2017-11-09 16:33:15 -05:00
|
|
|
func (r *BufferedReader) IsBuffered() bool {
|
|
|
|
return r.buffered
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-13 05:15:04 -05:00
|
|
|
// ReadByte implements io.ByteReader.
|
2017-11-24 17:16:01 -05:00
|
|
|
func (r *BufferedReader) ReadByte() (byte, error) {
|
|
|
|
var b [1]byte
|
|
|
|
_, err := r.Read(b[:])
|
|
|
|
return b[0], err
|
|
|
|
}
|
|
|
|
|
2018-02-13 05:15:04 -05:00
|
|
|
// Read implements io.Reader. It reads from internal buffer first (if available) and then reads from the underlying reader.
|
2017-11-09 16:33:15 -05:00
|
|
|
func (r *BufferedReader) Read(b []byte) (int, error) {
|
2017-04-19 05:52:27 -04:00
|
|
|
if r.leftOver != nil {
|
2017-04-19 15:27:21 -04:00
|
|
|
nBytes, _ := r.leftOver.Read(b)
|
2017-04-19 05:52:27 -04:00
|
|
|
if r.leftOver.IsEmpty() {
|
|
|
|
r.leftOver.Release()
|
|
|
|
r.leftOver = nil
|
|
|
|
}
|
2017-04-19 15:27:21 -04:00
|
|
|
return nBytes, nil
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
|
2017-11-25 15:58:47 -05:00
|
|
|
if !r.buffered {
|
|
|
|
if reader, ok := r.stream.(io.Reader); ok {
|
|
|
|
return reader.Read(b)
|
|
|
|
}
|
2017-11-09 16:33:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mb, err := r.stream.ReadMultiBuffer()
|
2017-11-24 17:16:01 -05:00
|
|
|
if mb != nil {
|
|
|
|
nBytes, _ := mb.Read(b)
|
|
|
|
if !mb.IsEmpty() {
|
|
|
|
r.leftOver = mb
|
|
|
|
}
|
|
|
|
return nBytes, err
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
2017-11-24 17:16:01 -05:00
|
|
|
return 0, err
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
2017-04-15 16:22:29 -04:00
|
|
|
|
2018-02-13 05:15:04 -05:00
|
|
|
// ReadMultiBuffer implements Reader.
|
2017-11-09 16:33:15 -05:00
|
|
|
func (r *BufferedReader) ReadMultiBuffer() (MultiBuffer, error) {
|
2017-04-19 05:52:27 -04:00
|
|
|
if r.leftOver != nil {
|
|
|
|
mb := r.leftOver
|
|
|
|
r.leftOver = nil
|
|
|
|
return mb, nil
|
2017-04-15 16:22:29 -04:00
|
|
|
}
|
2017-04-19 05:52:27 -04:00
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
return r.stream.ReadMultiBuffer()
|
2017-04-15 16:22:29 -04:00
|
|
|
}
|
2017-04-17 16:35:20 -04:00
|
|
|
|
2017-11-24 19:05:30 -05:00
|
|
|
// ReadAtMost returns a MultiBuffer with at most size.
|
2017-11-26 09:08:37 -05:00
|
|
|
func (r *BufferedReader) ReadAtMost(size int) (MultiBuffer, error) {
|
2017-11-24 19:05:30 -05:00
|
|
|
if r.leftOver == nil {
|
2017-11-26 09:08:37 -05:00
|
|
|
mb, err := r.stream.ReadMultiBuffer()
|
|
|
|
if mb.IsEmpty() && err != nil {
|
|
|
|
return nil, err
|
2017-11-24 19:05:30 -05:00
|
|
|
}
|
2017-11-26 09:08:37 -05:00
|
|
|
r.leftOver = mb
|
|
|
|
}
|
|
|
|
|
|
|
|
mb := r.leftOver.SliceBySize(size)
|
|
|
|
if r.leftOver.IsEmpty() {
|
|
|
|
r.leftOver = nil
|
2017-11-24 19:05:30 -05:00
|
|
|
}
|
2017-11-26 09:08:37 -05:00
|
|
|
return mb, nil
|
2017-11-24 19:05:30 -05:00
|
|
|
}
|
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
func (r *BufferedReader) writeToInternal(writer io.Writer) (int64, error) {
|
2017-04-17 16:35:20 -04:00
|
|
|
mbWriter := NewWriter(writer)
|
|
|
|
totalBytes := int64(0)
|
2017-04-19 05:52:27 -04:00
|
|
|
if r.leftOver != nil {
|
2017-11-03 08:19:48 -04:00
|
|
|
totalBytes += int64(r.leftOver.Len())
|
2017-11-09 16:33:15 -05:00
|
|
|
if err := mbWriter.WriteMultiBuffer(r.leftOver); err != nil {
|
2017-04-19 10:29:36 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
2018-03-28 16:23:38 -04:00
|
|
|
r.leftOver = nil
|
2017-04-19 05:52:27 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 16:35:20 -04:00
|
|
|
for {
|
2017-11-09 16:33:15 -05:00
|
|
|
mb, err := r.stream.ReadMultiBuffer()
|
2017-11-24 17:16:01 -05:00
|
|
|
if mb != nil {
|
|
|
|
totalBytes += int64(mb.Len())
|
|
|
|
if werr := mbWriter.WriteMultiBuffer(mb); werr != nil {
|
|
|
|
return totalBytes, err
|
|
|
|
}
|
2017-04-17 16:35:20 -04:00
|
|
|
}
|
2017-11-24 17:16:01 -05:00
|
|
|
if err != nil {
|
2017-04-17 16:35:20 -04:00
|
|
|
return totalBytes, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 05:15:04 -05:00
|
|
|
// WriteTo implements io.WriterTo.
|
2017-11-09 16:33:15 -05:00
|
|
|
func (r *BufferedReader) WriteTo(writer io.Writer) (int64, error) {
|
2017-04-17 16:35:20 -04:00
|
|
|
nBytes, err := r.writeToInternal(writer)
|
|
|
|
if errors.Cause(err) == io.EOF {
|
|
|
|
return nBytes, nil
|
|
|
|
}
|
|
|
|
return nBytes, err
|
|
|
|
}
|