1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 09:17:55 -04:00
v2fly/common/buf/reader.go

107 lines
1.9 KiB
Go
Raw Normal View History

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
2017-04-19 17:30:18 -04:00
buffer []byte
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) {
2017-04-19 17:30:18 -04:00
nBytes, err := r.reader.Read(r.buffer)
if 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-19 17:30:18 -04:00
p := r.buffer[:nBytes]
for len(p) > 0 {
2017-04-15 15:07:23 -04:00
b := New()
2017-04-19 17:30:18 -04:00
nBytes, _ := b.Write(p)
2017-04-15 15:07:23 -04:00
mb.Append(b)
2017-04-19 17:30:18 -04:00
p = p[nBytes:]
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 {
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-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
2017-04-19 15:27:21 -04:00
nBytes, _ := mb.Read(b)
2017-04-19 05:52:27 -04:00
if !mb.IsEmpty() {
r.leftOver = mb
2016-12-09 07:17:34 -05:00
}
2017-04-19 15:27:21 -04:00
return nBytes, nil
2016-12-09 07:17:34 -05:00
}
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 {
2017-04-19 10:29:36 -04:00
if err := mbWriter.Write(r.leftOver); err != nil {
return 0, err
}
2017-04-19 05:52:27 -04:00
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
}