forked from Lazyshot/go-hbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_buffer.go
114 lines (89 loc) · 1.91 KB
/
output_buffer.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
package hbase
import (
"encoding/binary"
pb "github.com/golang/protobuf/proto"
)
type outputBuffer struct {
b []byte
}
func newOutputBuffer() *outputBuffer {
b := []byte{}
return &outputBuffer{
b: b,
}
}
func (b *outputBuffer) Bytes() []byte {
return b.b
}
func (b *outputBuffer) Write(d []byte) (int, error) {
b.b = append(b.b, d...)
return len(d), nil
}
func (b *outputBuffer) WriteByte(d byte) error {
return binary.Write(b, byte_order, d)
}
func (b *outputBuffer) WriteString(d string) error {
return binary.Write(b, byte_order, d)
}
func (b *outputBuffer) WriteInt32(d int32) error {
return binary.Write(b, byte_order, d)
}
func (b *outputBuffer) WriteInt64(d int64) error {
return binary.Write(b, byte_order, d)
}
func (b *outputBuffer) WriteFloat32(d float32) error {
return binary.Write(b, byte_order, d)
}
func (b *outputBuffer) WriteFloat64(d float64) error {
return binary.Write(b, byte_order, d)
}
func (b *outputBuffer) WriteVarint32(n int32) error {
for true {
if (n & 0x7F) == 0 {
b.WriteByte(byte(n))
return nil
} else {
b.WriteByte(byte((n & 0x7F) | 0x80))
n >>= 7
}
}
return nil
}
func (b *outputBuffer) WritePBMessage(d pb.Message) error {
buf, err := pb.Marshal(d)
if err != nil {
return err
}
_, err = b.Write(buf)
return err
}
func (b *outputBuffer) writeDelimitedBuffers(bufs ...*outputBuffer) error {
totalLength := 0
lens := make([][]byte, len(bufs))
for i, v := range bufs {
n := len(v.Bytes())
lenb := pb.EncodeVarint(uint64(n))
totalLength += len(lenb) + n
lens[i] = lenb
}
b.WriteInt32(int32(totalLength))
for i, v := range bufs {
b.Write(lens[i])
b.Write(v.Bytes())
}
return nil
}
func (b *outputBuffer) PrependSize() error {
size := int32(len(b.b))
newBuf := newOutputBuffer()
err := newBuf.WriteInt32(size)
if err != nil {
return err
}
_, err = newBuf.Write(b.b)
if err != nil {
return err
}
*b = *newBuf
return nil
}