-
Notifications
You must be signed in to change notification settings - Fork 17
/
limitedreader.go
53 lines (46 loc) · 1 KB
/
limitedreader.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
package iorw
import (
"fmt"
"io"
)
type LimitedReaderAt struct {
ReaderAt
min, max int
}
// min<=max; allows arguments min<0 && max>length
func NewLimitedReaderAt(r ReaderAt, min, max int) *LimitedReaderAt {
if min > max {
panic(fmt.Sprintf("bad min/max: %v>%v", min, max))
}
return &LimitedReaderAt{r, min, max}
}
func NewLimitedReaderAtPad(r ReaderAt, min, max, pad int) *LimitedReaderAt {
return NewLimitedReaderAt(r, min-pad, max+pad)
}
func (r *LimitedReaderAt) ReadFastAt(i, n int) ([]byte, error) {
if i < r.min {
return nil, fmt.Errorf("limited index: %v<%v: %w", i, r.min, io.EOF)
}
if i+n > r.max {
if i > r.max {
return nil, fmt.Errorf("limited index: %v>%v: %w", i, r.max, io.EOF)
}
// n>0, there is data to read
n = r.max - i
}
return r.ReaderAt.ReadFastAt(i, n)
}
func (r *LimitedReaderAt) Min() int {
u := r.ReaderAt.Min()
if u > r.min {
return u
}
return r.min
}
func (r *LimitedReaderAt) Max() int {
u := r.ReaderAt.Max()
if u < r.max {
return u
}
return r.max
}