forked from folbricht/desync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
39 lines (34 loc) · 822 Bytes
/
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
package casync
import (
"encoding/binary"
"io"
)
type reader struct {
io.Reader
}
// ReadUint64 reads the next 8 bytes from the reader and returns it as little
// endian Uint64
func (r reader) ReadUint64() (uint64, error) {
b := make([]byte, 8)
if _, err := io.ReadFull(r, b); err != nil {
return 0, err
}
return binary.LittleEndian.Uint64(b), nil
}
// ReadN returns the next n bytes from the reader or an error if there are not
// enough left
func (r reader) ReadN(n uint64) ([]byte, error) {
b := make([]byte, n)
if _, err := io.ReadFull(r, b); err != nil {
return nil, err
}
return b, nil
}
// ReadID reads and returns a ChunkID
func (r reader) ReadID() (ChunkID, error) {
b := make([]byte, 32)
if _, err := io.ReadFull(r, b); err != nil {
return ChunkID{}, err
}
return ChunkIDFromSlice(b)
}