-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
75 lines (61 loc) · 1.31 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Package reversereader provides basic interfaces to read.
// It traverse an io.Reader as a backward stream.
package reversereader
import "io"
// NewReader returns a io.Reader for reading underlying io.ReadSeeker backwards.
func NewReader(rs io.ReadSeeker) io.Reader {
return &reader{
rs: rs,
rem: -1,
}
}
type reader struct {
rs io.ReadSeeker
rem int64
}
// Read satisfies the io.Reader interface.
func (r *reader) Read(p []byte) (int, error) {
if len(p) == 0 {
return 0, nil
}
if r.rem < 0 {
// Set remaining length
rem, err := r.rs.Seek(0, io.SeekEnd)
if err != nil {
return 0, err
}
r.rem = rem
}
// offset r.rem
// | |
// |<-- len(p) -->|
// | |
// | |
// | |
// v v
// ############################## r.rs
// [ ] p
// [ ] q
q := p[0:]
offset := r.rem - int64(len(p))
if offset < 0 {
q = p[-offset:]
offset = 0
}
if _, err := r.rs.Seek(offset, io.SeekStart); err != nil {
return 0, err
}
if _, err := io.ReadFull(r.rs, q); err != nil {
return 0, err
}
// Reverse the contents of p
for i := 0; i < len(p)/2; i++ {
j := len(p) - 1 - i
p[i], p[j] = p[j], p[i]
}
r.rem = offset
if r.rem == 0 {
return len(q), io.EOF
}
return len(q), nil
}