forked from scritchley/orc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
booleanwriter.go
62 lines (55 loc) · 1.06 KB
/
booleanwriter.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 orc
import (
"io"
)
type BooleanWriter struct {
*RunLengthByteWriter
bitsInData int
data byte
}
func NewBooleanWriter(w io.ByteWriter) *BooleanWriter {
return &BooleanWriter{
RunLengthByteWriter: NewRunLengthByteWriter(w),
}
}
func (b *BooleanWriter) WriteBool(t bool) error {
// If bitsInData is equal to 8 then write the byte
// to the underlying ByteStreamWriter.
if b.bitsInData >= 8 {
err := b.flushBools()
if err != nil {
return err
}
}
if t {
// If true, toggle the bit at relevant position.
b.data |= (1 << uint(7-b.bitsInData))
}
b.bitsInData++
return nil
}
func (b *BooleanWriter) flushBools() error {
if b.bitsInData > 0 {
err := b.RunLengthByteWriter.WriteByte(b.data)
if err != nil {
return err
}
b.bitsInData = 0
b.data = 0
}
return nil
}
func (b *BooleanWriter) Flush() error {
err := b.flushBools()
if err != nil {
return err
}
return b.RunLengthByteWriter.Flush()
}
func (b *BooleanWriter) Close() error {
err := b.Flush()
if err != nil {
return err
}
return b.RunLengthByteWriter.Close()
}