v2ray-core/common/buf/reader.go

107 lines
1.9 KiB
Go
Raw Normal View History

2016-12-09 12:17:34 +00:00
package buf
2017-04-17 20:35:20 +00:00
import (
"io"
"v2ray.com/core/common/errors"
)
2016-12-09 12:17:34 +00:00
// BytesToBufferReader is a Reader that adjusts its reading speed automatically.
type BytesToBufferReader struct {
2017-04-15 19:07:23 +00:00
reader io.Reader
2017-04-19 21:30:18 +00:00
buffer []byte
2016-12-09 12:17:34 +00:00
}
// Read implements Reader.Read().
2017-04-17 20:35:20 +00:00
func (r *BytesToBufferReader) Read() (MultiBuffer, error) {
2017-04-19 21:30:18 +00:00
nBytes, err := r.reader.Read(r.buffer)
if err != nil {
2016-12-09 12:17:34 +00:00
return nil, err
}
2017-04-15 19:07:23 +00:00
mb := NewMultiBuffer()
2017-04-19 21:30:18 +00:00
p := r.buffer[:nBytes]
for len(p) > 0 {
2017-04-15 19:07:23 +00:00
b := New()
2017-04-19 21:30:18 +00:00
nBytes, _ := b.Write(p)
2017-04-15 19:07:23 +00:00
mb.Append(b)
2017-04-19 21:30:18 +00:00
p = p[nBytes:]
2016-12-09 12:17:34 +00:00
}
2017-04-15 19:07:23 +00:00
return mb, nil
2016-12-09 12:17:34 +00:00
}
2017-04-15 20:22:29 +00:00
type readerAdpater struct {
MultiBufferReader
}
func (r *readerAdpater) Read() (MultiBuffer, error) {
return r.ReadMultiBuffer()
}
2017-02-20 09:33:35 +00:00
type bufferToBytesReader struct {
2017-04-19 09:52:27 +00:00
stream Reader
leftOver MultiBuffer
2016-12-09 12:17:34 +00:00
}
2017-04-17 20:35:20 +00:00
func (r *bufferToBytesReader) Read(b []byte) (int, error) {
2017-04-19 09:52:27 +00:00
if r.leftOver != nil {
2017-04-19 19:27:21 +00:00
nBytes, _ := r.leftOver.Read(b)
2017-04-19 09:52:27 +00:00
if r.leftOver.IsEmpty() {
r.leftOver.Release()
r.leftOver = nil
}
2017-04-19 19:27:21 +00:00
return nBytes, nil
2016-12-09 12:17:34 +00:00
}
2017-04-19 09:52:27 +00:00
mb, err := r.stream.Read()
if err != nil {
return 0, err
2016-12-09 12:17:34 +00:00
}
2017-04-19 09:52:27 +00:00
2017-04-19 19:27:21 +00:00
nBytes, _ := mb.Read(b)
2017-04-19 09:52:27 +00:00
if !mb.IsEmpty() {
r.leftOver = mb
2016-12-09 12:17:34 +00:00
}
2017-04-19 19:27:21 +00:00
return nBytes, nil
2016-12-09 12:17:34 +00:00
}
2017-04-15 20:22:29 +00:00
2017-04-17 20:35:20 +00:00
func (r *bufferToBytesReader) ReadMultiBuffer() (MultiBuffer, error) {
2017-04-19 09:52:27 +00:00
if r.leftOver != nil {
mb := r.leftOver
r.leftOver = nil
return mb, nil
2017-04-15 20:22:29 +00:00
}
2017-04-19 09:52:27 +00:00
return r.stream.Read()
2017-04-15 20:22:29 +00:00
}
2017-04-17 20:35:20 +00:00
func (r *bufferToBytesReader) writeToInternal(writer io.Writer) (int64, error) {
mbWriter := NewWriter(writer)
totalBytes := int64(0)
2017-04-19 09:52:27 +00:00
if r.leftOver != nil {
2017-04-19 14:29:36 +00:00
if err := mbWriter.Write(r.leftOver); err != nil {
return 0, err
}
2017-04-19 09:52:27 +00:00
totalBytes += int64(r.leftOver.Len())
}
2017-04-17 20:35:20 +00:00
for {
2017-04-19 09:52:27 +00:00
mb, err := r.stream.Read()
if err != nil {
return totalBytes, err
2017-04-17 20:35:20 +00:00
}
2017-04-19 09:52:27 +00:00
totalBytes += int64(mb.Len())
if err := mbWriter.Write(mb); err != nil {
2017-04-17 20:35:20 +00: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
}