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-04-15 15:07:23 -04:00
|
|
|
reader io.Reader
|
|
|
|
buffer *Buffer
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements Reader.Read().
|
2017-04-17 16:35:20 -04:00
|
|
|
func (r *BytesToBufferReader) Read() (MultiBuffer, error) {
|
|
|
|
if err := r.buffer.Reset(ReadFrom(r.reader)); err != nil {
|
2016-12-09 07:17:34 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-15 15:07:23 -04:00
|
|
|
mb := NewMultiBuffer()
|
2017-04-17 16:35:20 -04:00
|
|
|
for !r.buffer.IsEmpty() {
|
2017-04-15 15:07:23 -04:00
|
|
|
b := New()
|
2017-04-17 16:35:20 -04:00
|
|
|
b.AppendSupplier(ReadFrom(r.buffer))
|
2017-04-15 15:07:23 -04:00
|
|
|
mb.Append(b)
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
2017-04-15 15:07:23 -04:00
|
|
|
return mb, nil
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
|
2017-04-15 16:22:29 -04:00
|
|
|
type readerAdpater struct {
|
|
|
|
MultiBufferReader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *readerAdpater) Read() (MultiBuffer, error) {
|
|
|
|
return r.ReadMultiBuffer()
|
|
|
|
}
|
|
|
|
|
2017-02-20 04:33:35 -05:00
|
|
|
type bufferToBytesReader struct {
|
2017-04-19 05:52:27 -04:00
|
|
|
stream Reader
|
|
|
|
leftOver MultiBuffer
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 16:35:20 -04:00
|
|
|
func (r *bufferToBytesReader) Read(b []byte) (int, error) {
|
2017-04-19 05:52:27 -04:00
|
|
|
if r.leftOver != nil {
|
|
|
|
nBytes, err := r.leftOver.Read(b)
|
|
|
|
if r.leftOver.IsEmpty() {
|
|
|
|
r.leftOver.Release()
|
|
|
|
r.leftOver = nil
|
|
|
|
}
|
|
|
|
return nBytes, err
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
|
2017-04-19 05:52:27 -04:00
|
|
|
mb, err := r.stream.Read()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
2017-04-19 05:52:27 -04:00
|
|
|
|
|
|
|
nBytes, err := mb.Read(b)
|
|
|
|
if !mb.IsEmpty() {
|
|
|
|
r.leftOver = mb
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
return nBytes, err
|
|
|
|
}
|
2017-04-15 16:22:29 -04:00
|
|
|
|
2017-04-17 16:35:20 -04:00
|
|
|
func (r *bufferToBytesReader) 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
|
|
|
|
|
|
|
return r.stream.Read()
|
2017-04-15 16:22:29 -04:00
|
|
|
}
|
2017-04-17 16:35:20 -04:00
|
|
|
|
|
|
|
func (r *bufferToBytesReader) writeToInternal(writer io.Writer) (int64, error) {
|
|
|
|
mbWriter := NewWriter(writer)
|
|
|
|
totalBytes := int64(0)
|
2017-04-19 05:52:27 -04:00
|
|
|
if r.leftOver != nil {
|
|
|
|
mbWriter.Write(r.leftOver)
|
|
|
|
totalBytes += int64(r.leftOver.Len())
|
|
|
|
}
|
|
|
|
|
2017-04-17 16:35:20 -04:00
|
|
|
for {
|
2017-04-19 05:52:27 -04:00
|
|
|
mb, err := r.stream.Read()
|
|
|
|
if err != nil {
|
|
|
|
return totalBytes, err
|
2017-04-17 16:35:20 -04:00
|
|
|
}
|
2017-04-19 05:52:27 -04:00
|
|
|
totalBytes += int64(mb.Len())
|
|
|
|
if err := mbWriter.Write(mb); err != nil {
|
2017-04-17 16:35:20 -04:00
|
|
|
return totalBytes, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *bufferToBytesReader) WriteTo(writer io.Writer) (int64, error) {
|
|
|
|
nBytes, err := r.writeToInternal(writer)
|
|
|
|
if errors.Cause(err) == io.EOF {
|
|
|
|
return nBytes, nil
|
|
|
|
}
|
|
|
|
return nBytes, err
|
|
|
|
}
|