-
Notifications
You must be signed in to change notification settings - Fork 221
/
content.go
66 lines (53 loc) · 999 Bytes
/
content.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
package main
import (
"errors"
"io"
)
const contentCharset = "-ABCDEFGHIJKLMNOPQRSTUVWXYZ"
type contentReader struct {
size int64
cur int64
}
// Read implements the io.Read interface.
func (r *contentReader) Read(p []byte) (int, error) {
length := r.size - 1
if r.cur >= length {
return 0, io.EOF
}
if len(p) == 0 {
return 0, nil
}
var n int
if r.cur == 0 {
p[n] = '|'
r.cur++
n++
}
for n < len(p) && r.cur <= length {
p[n] = contentCharset[int(r.cur)%len(contentCharset)]
r.cur++
n++
}
if r.cur >= length {
p[n-1] = '|'
}
return n, nil
}
// Seek implements the io.Seek interface.
func (r *contentReader) Seek(offset int64, whence int) (int64, error) {
switch whence {
default:
return 0, errors.New("seek: invalid whence")
case io.SeekStart:
offset += 0
case io.SeekCurrent:
offset += r.cur
case io.SeekEnd:
offset += r.size - 1
}
if offset < 0 {
return 0, errors.New("seek: invalid offset")
}
r.cur = offset
return offset, nil
}