forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
52 lines (43 loc) · 1.19 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package io // import "github.com/v2ray/v2ray-core/common/io"
import (
"io"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/alloc"
)
// Reader extends io.Reader with alloc.Buffer.
type Reader interface {
common.Releasable
// Read reads content from underlying reader, and put it into an alloc.Buffer.
Read() (*alloc.Buffer, error)
}
// AdaptiveReader is a Reader that adjusts its reading speed automatically.
type AdaptiveReader struct {
reader io.Reader
allocate func() *alloc.Buffer
}
// NewAdaptiveReader creates a new AdaptiveReader.
// The AdaptiveReader instance doesn't take the ownership of reader.
func NewAdaptiveReader(reader io.Reader) *AdaptiveReader {
return &AdaptiveReader{
reader: reader,
allocate: alloc.NewBuffer,
}
}
// Read implements Reader.Read().
func (this *AdaptiveReader) Read() (*alloc.Buffer, error) {
buffer := this.allocate().Clear()
_, err := buffer.FillFrom(this.reader)
if err != nil {
buffer.Release()
return nil, err
}
if buffer.Len() >= alloc.BufferSize {
this.allocate = alloc.NewLargeBuffer
} else {
this.allocate = alloc.NewBuffer
}
return buffer, nil
}
func (this *AdaptiveReader) Release() {
this.reader = nil
}