-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.go
More file actions
47 lines (41 loc) · 675 Bytes
/
reader.go
File metadata and controls
47 lines (41 loc) · 675 Bytes
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
package bencode
import (
"bytes"
"errors"
"io"
)
type reader struct {
*bytes.Reader
}
func newReader(data []byte) *reader {
return &reader{bytes.NewReader(data)}
}
func (r *reader) readUntil(c byte) ([]byte, error) {
res := []byte("")
for {
b, err := r.ReadByte()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return []byte{}, err
}
if b == c {
break
}
res = append(res, b)
}
return res, nil
}
func (r *reader) readNBytes(n uint64) ([]byte, error) {
res := []byte("")
var i uint64
for i = 0; i < n; i++ {
b, err := r.ReadByte()
if err != nil {
return []byte(""), err
}
res = append(res, b)
}
return res, nil
}