forked from colinmarc/sequencefile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vint.go
62 lines (51 loc) · 1.05 KB
/
vint.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
package sequencefile
import "io"
// ReadVInt reads an int64 encoded in hadoop's "VInt" format, described and
// implemented here: https://goo.gl/1h4mrG. It does at most two reads to the
// underlying io.Reader.
func ReadVInt(r io.Reader) (int64, error) {
lenByte, err := mustReadByte(r)
if err != nil {
return 0, err
}
l := int8(lenByte)
var remaining int
var negative bool
if l >= -112 {
return int64(l), nil
} else if l >= -120 {
remaining = int(-112 - l)
negative = false
} else {
remaining = int(-120 - l)
negative = true
}
var res uint64
buf := make([]byte, remaining)
_, err = io.ReadFull(r, buf)
if err != nil {
return 0, err
}
for _, b := range buf {
res = (res << 8) | uint64(b)
}
if negative {
res = ^res
}
return int64(res), nil
}
func mustReadByte(r io.Reader) (byte, error) {
var b byte
var err error
if br, ok := r.(io.ByteReader); ok {
b, err = br.ReadByte()
} else {
buf := make([]byte, 1)
_, err = io.ReadFull(r, buf)
b = buf[0]
}
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return b, err
}