-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeeker.go
98 lines (87 loc) · 1.75 KB
/
peeker.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package mysqlx
import (
"fmt"
"io"
"math"
"time"
)
const (
ErrNegativeCount = errorString("negative count")
ErrAdvanceTooFar = errorString("advance too far")
)
const (
minReadSize = 512
readAheadSize = 256
)
type Reader interface {
io.Reader
SetReadDeadline(time.Time) error
}
type Peeker struct {
r Reader
pbuf []byte // slice of available bytes to peek from, references into rbuf below
rbuf []byte // underlying Read() buffer
}
func NewPeeker(r Reader) *Peeker {
return &Peeker{r: r}
}
func (p *Peeker) Reset(r Reader) {
p.r = r
p.pbuf = p.rbuf[:0:0]
}
// buf returns a suitable sized slice to read atleast n bytes into
func (p *Peeker) buf(n int) []byte {
nn := minReadSize
if n > nn {
nn = n
// overflow checking
if nn < math.MaxInt32-readAheadSize {
nn += readAheadSize
}
}
if n > len(p.rbuf) {
p.rbuf = make([]byte, nn)
} else if len(p.rbuf) > nn {
// Limit how much to read as likely have to copy it
return p.rbuf[:nn]
}
return p.rbuf
}
func (p *Peeker) fill(n int, deadline time.Time) ([]byte, error) {
buf := p.buf(n)
i := copy(buf, p.pbuf)
if err := p.r.SetReadDeadline(deadline); err != nil {
return nil, fmt.Errorf("failed to set read deadline: %w", err)
}
r, err := p.r.Read(buf[i:])
i += r
for i < n && err == nil {
r, err = p.r.Read(buf[i:])
i += r
}
buf = buf[:i:i]
p.pbuf = buf
if n > i {
return buf, err
}
return buf[:n:n], nil
}
func (p *Peeker) Peek(n int, deadline time.Time) ([]byte, error) {
if n < 0 {
return nil, ErrNegativeCount
}
if n > len(p.pbuf) {
return p.fill(n, deadline)
}
return p.pbuf[:n:n], nil
}
func (p *Peeker) Discard(n int) error {
if n < 0 {
return ErrNegativeCount
}
if n > len(p.pbuf) {
return ErrAdvanceTooFar
}
p.pbuf = p.pbuf[n:]
return nil
}