forked from elodina/go-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
337 lines (280 loc) · 10 KB
/
decoder.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package avro
import (
"encoding/binary"
"math"
)
// Decoder is an interface that provides low-level support for deserializing Avro values.
type Decoder interface {
// Reads a null value. Returns a decoded value and an error if it occurs.
ReadNull() (interface{}, error)
// Reads a boolean value. Returns a decoded value and an error if it occurs.
ReadBoolean() (bool, error)
// Reads an in value. Returns a decoded value and an error if it occurs.
ReadInt() (int32, error)
// Reads a long value. Returns a decoded value and an error if it occurs.
ReadLong() (int64, error)
// Reads a float value. Returns a decoded value and an error if it occurs.
ReadFloat() (float32, error)
// Reads a double value. Returns a decoded value and an error if it occurs.
ReadDouble() (float64, error)
// Reads a bytes value. Returns a decoded value and an error if it occurs.
ReadBytes() ([]byte, error)
// Reads a string value. Returns a decoded value and an error if it occurs.
ReadString() (string, error)
// Reads an enum value (which is an Avro int value). Returns a decoded value and an error if it occurs.
ReadEnum() (int32, error)
// Reads and returns the size of the first block of an array. If call to this return non-zero, then the caller
// should read the indicated number of items and then call ArrayNext() to find out the number of items in the
// next block. Returns a decoded value and an error if it occurs.
ReadArrayStart() (int64, error)
// Processes the next block of an array and returns the number of items in the block.
// Returns a decoded value and an error if it occurs.
ArrayNext() (int64, error)
// Reads and returns the size of the first block of map entries. If call to this return non-zero, then the caller
// should read the indicated number of items and then call MapNext() to find out the number of items in the
// next block. Usage is similar to ReadArrayStart(). Returns a decoded value and an error if it occurs.
ReadMapStart() (int64, error)
// Processes the next block of map entries and returns the number of items in the block.
// Returns a decoded value and an error if it occurs.
MapNext() (int64, error)
// Reads fixed sized binary object into the provided buffer.
// Returns an error if it occurs.
ReadFixed([]byte) error
// Reads fixed sized binary object into the provided buffer.
// The second parameter is the position where the data needs to be written, the third is the size of binary object.
// Returns an error if it occurs.
ReadFixedWithBounds([]byte, int, int) error
// SetBlock is used for Avro Object Container Files where the data is split in blocks and sets a data block
// for this decoder and sets the position to the start of this block.
SetBlock(*DataBlock)
// Seek sets the reading position of this Decoder to a given value allowing to skip items etc.
Seek(int64)
// Tell returns the current reading position of this Decoder.
Tell() int64
}
// DataBlock is a structure that holds a certain amount of entries and the actual buffer to read from.
type DataBlock struct {
// Actual data
Data []byte
// Number of entries encoded in Data.
NumEntries int64
// Size of data buffer in bytes.
BlockSize int
// Number of unread entries in this DataBlock.
BlockRemaining int64
}
var maxIntBufSize = 5
var maxLongBufSize = 10
// BinaryDecoder implements Decoder and provides low-level support for deserializing Avro values.
type BinaryDecoder struct {
buf []byte
pos int64
}
// NewBinaryDecoder creates a new BinaryDecoder to read from a given buffer.
func NewBinaryDecoder(buf []byte) *BinaryDecoder {
return &BinaryDecoder{buf, 0}
}
// ReadNull reads a null value. Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ReadNull() (interface{}, error) {
return nil, nil
}
// ReadInt reads an int value. Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ReadInt() (int32, error) {
if err := checkEOF(bd.buf, bd.pos, 1); err != nil {
return 0, EOF
}
var value uint32
var b uint8
var offset int
bufLen := int64(len(bd.buf))
for {
if offset == maxIntBufSize {
return 0, IntOverflow
}
if bd.pos >= bufLen {
return 0, InvalidInt
}
b = bd.buf[bd.pos]
value |= uint32(b&0x7F) << uint(7*offset)
bd.pos++
offset++
if b&0x80 == 0 {
break
}
}
return int32((value >> 1) ^ -(value & 1)), nil
}
// ReadLong reads a long value. Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ReadLong() (int64, error) {
var value uint64
var b uint8
var offset int
bufLen := int64(len(bd.buf))
for {
if offset == maxLongBufSize {
return 0, LongOverflow
}
if bd.pos >= bufLen {
return 0, InvalidLong
}
b = bd.buf[bd.pos]
value |= uint64(b&0x7F) << uint(7*offset)
bd.pos++
offset++
if b&0x80 == 0 {
break
}
}
return int64((value >> 1) ^ -(value & 1)), nil
}
// ReadString reads a string value. Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ReadString() (string, error) {
if err := checkEOF(bd.buf, bd.pos, 1); err != nil {
return "", err
}
length, err := bd.ReadLong()
if err != nil || length < 0 {
return "", InvalidStringLength
}
if err := checkEOF(bd.buf, bd.pos, int(length)); err != nil {
return "", err
}
value := string(bd.buf[bd.pos : bd.pos+length])
bd.pos += length
return value, nil
}
// ReadBoolean reads a boolean value. Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ReadBoolean() (bool, error) {
b := bd.buf[bd.pos] & 0xFF
bd.pos++
var err error
if b != 0 && b != 1 {
err = InvalidBool
}
return b == 1, err
}
// ReadBytes reads a bytes value. Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ReadBytes() ([]byte, error) {
//TODO make something with these if's!!
if err := checkEOF(bd.buf, bd.pos, 1); err != nil {
return nil, EOF
}
length, err := bd.ReadLong()
if err != nil {
return nil, err
}
if length < 0 {
return nil, NegativeBytesLength
}
if err = checkEOF(bd.buf, bd.pos, int(length)); err != nil {
return nil, EOF
}
bytes := make([]byte, length)
copy(bytes[:], bd.buf[bd.pos:bd.pos+length])
bd.pos += length
return bytes, err
}
// ReadFloat reads a float value. Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ReadFloat() (float32, error) {
var float float32
if err := checkEOF(bd.buf, bd.pos, 4); err != nil {
return float, err
}
bits := binary.LittleEndian.Uint32(bd.buf[bd.pos : bd.pos+4])
float = math.Float32frombits(bits)
bd.pos += 4
return float, nil
}
// ReadDouble reads a double value. Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ReadDouble() (float64, error) {
var double float64
if err := checkEOF(bd.buf, bd.pos, 8); err != nil {
return double, err
}
bits := binary.LittleEndian.Uint64(bd.buf[bd.pos : bd.pos+8])
double = math.Float64frombits(bits)
bd.pos += 8
return double, nil
}
// ReadEnum reads an enum value (which is an Avro int value). Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ReadEnum() (int32, error) {
return bd.ReadInt()
}
// ReadArrayStart reads and returns the size of the first block of an array. If call to this return non-zero, then the caller
// should read the indicated number of items and then call ArrayNext() to find out the number of items in the
// next block. Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ReadArrayStart() (int64, error) {
return bd.readItemCount()
}
// ArrayNext processes the next block of an array and returns the number of items in the block.
// Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ArrayNext() (int64, error) {
return bd.readItemCount()
}
// ReadMapStart reads and returns the size of the first block of map entries. If call to this return non-zero, then the caller
// should read the indicated number of items and then call MapNext() to find out the number of items in the
// next block. Usage is similar to ReadArrayStart(). Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) ReadMapStart() (int64, error) {
return bd.readItemCount()
}
// MapNext processes the next block of map entries and returns the number of items in the block.
// Returns a decoded value and an error if it occurs.
func (bd *BinaryDecoder) MapNext() (int64, error) {
return bd.readItemCount()
}
// ReadFixed reads fixed sized binary object into the provided buffer.
// Returns an error if it occurs.
func (bd *BinaryDecoder) ReadFixed(bytes []byte) error {
return bd.readBytes(bytes, 0, len(bytes))
}
// ReadFixedWithBounds reads fixed sized binary object into the provided buffer.
// The second parameter is the position where the data needs to be written, the third is the size of binary object.
// Returns an error if it occurs.
func (bd *BinaryDecoder) ReadFixedWithBounds(bytes []byte, start int, length int) error {
return bd.readBytes(bytes, start, length)
}
// SetBlock is used for Avro Object Container Files where the data is split in blocks and sets a data block
// for this decoder and sets the position to the start of this block.
func (bd *BinaryDecoder) SetBlock(block *DataBlock) {
bd.buf = block.Data
bd.Seek(0)
}
// Seek sets the reading position of this Decoder to a given value allowing to skip items etc.
func (bd *BinaryDecoder) Seek(pos int64) {
bd.pos = pos
}
// Tell returns the current reading position of this Decoder.
func (bd *BinaryDecoder) Tell() int64 {
return bd.pos
}
func checkEOF(buf []byte, pos int64, length int) error {
if int64(len(buf)) < pos+int64(length) {
return EOF
}
return nil
}
func (bd *BinaryDecoder) readItemCount() (int64, error) {
count, err := bd.ReadLong()
if err != nil {
return 0, err
}
if count < 0 {
_, err = bd.ReadLong()
if err != nil {
return 0, err
}
count = -count
}
return count, err
}
func (bd *BinaryDecoder) readBytes(bytes []byte, start int, length int) error {
if length < 0 {
return NegativeBytesLength
}
if err := checkEOF(bd.buf, bd.pos, int(start+length)); err != nil {
return EOF
}
copy(bytes[:], bd.buf[bd.pos+int64(start):bd.pos+int64(start)+int64(length)])
bd.pos += int64(length)
return nil
}