-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
57 lines (45 loc) · 933 Bytes
/
read.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
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
package mitosis
import (
"encoding/binary"
"io"
)
// check panics with the given error if it is not nil.
func check(err error) {
if err != nil {
panic(err)
}
}
var endian = binary.LittleEndian
func readU32(r io.Reader) uint32 {
var v uint32
check(binary.Read(r, endian, &v))
return v
}
func readU64(r io.Reader) uint64 {
var v uint64
check(binary.Read(r, endian, &v))
return v
}
func readRaw(r io.Reader, size uint32) []byte {
if size == 0 {
return nil
}
data := make([]byte, size)
_, err := r.Read(data)
check(err)
return data
}
func readByteSlice(r io.Reader) []byte {
size := readU32(r)
return readRaw(r, size)
}
func readStringSlice(r io.Reader) []string {
size := readU32(r)
list := make([]string, size)
for i := range list {
list[i] = string(readByteSlice(r))
}
return list
}