-
Notifications
You must be signed in to change notification settings - Fork 15
/
reader.go
120 lines (109 loc) · 2.57 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package avro
import (
"encoding/binary"
"fmt"
"io"
"math"
)
const bufSize = 512
// read reads n bytes from the buffer and returns
// them. The returned slice is only valid until
// the next call to any of the reader methods.
// n must be less than or equal to cap(d.buf).
func (d *decoder) read(n int) []byte {
if d.fill(n) < n {
d.error(io.ErrUnexpectedEOF)
}
buf := d.buf[d.scan : d.scan+n]
d.scan += n
return buf
}
func (d *decoder) fill(n int) int {
if len(d.buf)-d.scan >= n {
return n
}
if d.readErr != nil {
// If there's an error, there's no point in doing
// anything more. This is also crucial to avoid
// corrupting the buffer when it has been provided by a
// caller.
return len(d.buf) - d.scan
}
// Slide any remaining bytes to the
// start of the buffer.
total := copy(d.buf, d.buf[d.scan:])
d.scan = 0
d.buf = d.buf[:cap(d.buf)]
for total < n {
if d.readErr != nil {
if d.readErr == io.EOF {
d.buf = d.buf[:total]
return total
}
d.error(d.readErr)
}
nr, err := d.r.Read(d.buf[total:])
if err != nil {
d.readErr = err
}
total += nr
}
d.buf = d.buf[:total]
return n
}
func (d *decoder) readBool() bool {
return d.read(1)[0] != 0
}
func (d *decoder) readDouble() float64 {
bits := binary.LittleEndian.Uint64(d.read(8))
return math.Float64frombits(bits)
}
func (d *decoder) readFloat() float64 {
bits := binary.LittleEndian.Uint32(d.read(4))
return float64(math.Float32frombits(bits))
}
func (d *decoder) readBytes() []byte {
// TODO bounds-check readLong result.
// https://github.com/heetch/avro/issues/33
size := d.readLong()
// Make a temporary buffer for the bytes, limiting the size to
// an arbitrary sane default (~2.2GB).
if size < 0 || size > math.MaxInt32 {
d.error(fmt.Errorf("length out of range: %d", size))
}
return d.readFixed(int(size))
}
func (d *decoder) readFixed(size int) []byte {
if size < cap(d.buf) {
// The bytes will fit in the buffer we already
// have, so use that.
return d.read(size)
}
buf := make([]byte, size)
n := copy(buf, d.buf[d.scan:])
_, err := io.ReadFull(d.r, buf[n:])
if err != nil {
d.error(err)
}
d.scan = len(d.buf)
return buf
}
func (d *decoder) readLong() int64 {
// Note: d.fill doesn't mind if we get less
// than the required number of bytes.
d.fill(binary.MaxVarintLen64)
x, nr := binary.Varint(d.buf[d.scan:])
switch {
case nr > 0:
d.scan += nr
return x
case nr == 0:
d.error(io.ErrUnexpectedEOF)
default:
d.error(fmt.Errorf("integer too large"))
}
panic("unreachable")
}
func (d *decoder) readString() string {
return string(d.readBytes())
}