-
Notifications
You must be signed in to change notification settings - Fork 19
/
encoding.go
46 lines (43 loc) · 1.04 KB
/
encoding.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
package objectcache
import (
"encoding/binary"
"errors"
"fmt"
"io"
)
func decode(reader io.Reader) (ObjectCache, error) {
var numObjects uint64
if err := binary.Read(reader, binary.BigEndian, &numObjects); err != nil {
return nil, err
}
objectCache := make(ObjectCache, numObjects)
for index := uint64(0); index < numObjects; index++ {
hash := &objectCache[index]
nRead, err := io.ReadFull(reader, (*hash)[:])
if err != nil {
return nil, err
}
if nRead != len(*hash) {
return nil, errors.New(fmt.Sprintf(
"read: %d, expected: %d", nRead, len(hash)))
}
}
return objectCache, nil
}
func (objectCache ObjectCache) encode(writer io.Writer) error {
numObjects := uint64(len(objectCache))
if err := binary.Write(writer, binary.BigEndian, numObjects); err != nil {
return err
}
for _, hash := range objectCache {
nWritten, err := writer.Write(hash[:])
if err != nil {
return err
}
if nWritten != len(hash) {
return errors.New(fmt.Sprintf(
"wrote: %d, expected: %d", nWritten, len(hash)))
}
}
return nil
}