-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathrunlengthbytereader.go
99 lines (89 loc) · 1.83 KB
/
runlengthbytereader.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
package orc
import "io"
// RunLengthByteReader reads a byte run length encoded stream from ByteReader r.
type RunLengthByteReader struct {
r io.ByteReader
literals []byte
nextByte *byte
numLiterals int
used int
repeat bool
minRepeatSize int
err error
}
func NewRunLengthByteReader(r io.ByteReader) *RunLengthByteReader {
return &RunLengthByteReader{
r: r,
literals: make([]byte, MaxLiteralSize),
minRepeatSize: MinRepeatSize,
}
}
func (b *RunLengthByteReader) available() error {
byt, err := b.ReadByte()
if err != nil {
b.err = err
return err
}
b.nextByte = &byt
return nil
}
func (b *RunLengthByteReader) ReadByte() (byte, error) {
if b.nextByte != nil {
byt := *b.nextByte
b.nextByte = nil
return byt, nil
}
return b.r.ReadByte()
}
func (b *RunLengthByteReader) Next() bool {
return b.used != b.numLiterals || b.available() == nil
}
func (b *RunLengthByteReader) Byte() byte {
var result byte
if b.used == b.numLiterals {
err := b.readValues()
if err != nil {
return 0
}
}
if b.repeat {
result = b.literals[0]
} else {
result = b.literals[b.used]
}
b.used++
return result
}
func (b *RunLengthByteReader) readValues() error {
control, err := b.ReadByte()
if err != nil {
return err
}
b.used = 0
if control < 0x80 {
b.repeat = true
b.numLiterals = int(control) + b.minRepeatSize
val, err := b.ReadByte()
if err != nil {
return err
}
b.literals[0] = val
} else {
b.repeat = false
b.numLiterals = 0x100 - int(control)
for i := 0; i < b.numLiterals; i++ {
result, err := b.ReadByte()
if err != nil {
return err
}
b.literals[i] = result
}
}
return nil
}
func (b *RunLengthByteReader) Value() interface{} {
return int8(b.Byte())
}
func (b *RunLengthByteReader) Err() error {
return b.err
}