-
Notifications
You must be signed in to change notification settings - Fork 5
/
bstream.go
179 lines (142 loc) · 3.09 KB
/
bstream.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
package bstream
import "io"
type bit bool
const (
zero bit = false
one bit = true
)
//BStream :
type BStream struct {
stream []byte
rCount uint8 // Number of bits still unread from the first byte of the stream
wCount uint8 // Number of bits still empty in the last byte of the stream
}
//NewBStreamReader :
func NewBStreamReader(data []byte) *BStream {
return &BStream{stream: data, rCount: 8}
}
//NewBStreamWriter :
func NewBStreamWriter(nByte uint8) *BStream {
return &BStream{stream: make([]byte, 0, nByte), rCount: 8}
}
//WriteBit :
func (b *BStream) WriteBit(input bit) {
if b.wCount == 0 {
b.stream = append(b.stream, 0)
b.wCount = 8
}
latestIndex := len(b.stream) - 1
if input {
b.stream[latestIndex] |= 1 << (b.wCount - 1)
}
b.wCount--
}
//WriteOneByte :
func (b *BStream) WriteOneByte(data byte) {
if b.wCount == 0 {
b.stream = append(b.stream, data)
return
}
latestIndex := len(b.stream) - 1
b.stream[latestIndex] |= data >> (8 - b.wCount)
b.stream = append(b.stream, 0)
latestIndex++
b.stream[latestIndex] = data << b.wCount
}
//WriteBits :
func (b *BStream) WriteBits(data uint64, count int) {
data <<= uint(64 - count)
//handle write byte if count over 8
for count >= 8 {
byt := byte(data >> (64 - 8))
b.WriteOneByte(byt)
data <<= 8
count -= 8
}
//handle write bit
for count > 0 {
bi := data >> (64 - 1)
b.WriteBit(bi == 1)
data <<= 1
count--
}
}
func (b *BStream) ReadBit() (bit, error) {
//empty return io.EOF
if len(b.stream) == 0 {
return zero, io.EOF
}
//if first byte already empty, move to next byte to retrieval
if b.rCount == 0 {
b.stream = b.stream[1:]
if len(b.stream) == 0 {
return zero, io.EOF
}
b.rCount = 8
}
// handle bit retrieval
retBit := b.stream[0] & (1 << (b.rCount - 1))
b.rCount--
return retBit != 0, nil
}
func (b *BStream) ReadByte() (byte, error) {
//empty return io.EOF
if len(b.stream) == 0 {
return 0, io.EOF
}
//if first byte already empty, move to next byte to retrieval
if b.rCount == 0 {
b.stream = b.stream[1:]
if len(b.stream) == 0 {
return 0, io.EOF
}
b.rCount = 8
}
//just remain 8 bit, just return this byte directly
if b.rCount == 8 {
byt := b.stream[0]
b.stream = b.stream[1:]
return byt, nil
}
//handle byte retrieval
retByte := b.stream[0] << (8 - b.rCount)
b.stream = b.stream[1:]
//check if we could finish retrieval on next byte
if len(b.stream) == 0 {
return 0, io.EOF
}
//handle remain bit on next stream
retByte |= b.stream[0] >> b.rCount
return retByte, nil
}
//ReadBits :
func (b *BStream) ReadBits(count int) (uint64, error) {
var retValue uint64
//handle byte reading
for count >= 8 {
retValue <<= 8
byt, err := b.ReadByte()
if err != nil {
return 0, err
}
retValue |= uint64(byt)
count = count - 8
}
for count > 0 {
retValue <<= 1
bi, err := b.ReadBit()
if err != nil {
return 0, err
}
if bi {
retValue |= 1
}
count--
}
return retValue, nil
}
// Bytes returns the bytes in the stream - taken from
// https://github.com/dgryski/go-tsz/bstream.go
func (b *BStream) Bytes() []byte {
return b.stream
}