-
Notifications
You must be signed in to change notification settings - Fork 7
/
encoder.go
executable file
·202 lines (171 loc) · 3.21 KB
/
encoder.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
package wkproto
import (
"io"
"github.com/valyala/bytebufferpool"
)
var pool = bytebufferpool.Pool{}
type EncodeOptions struct {
Cap int
}
func NewEncodeOptions() *EncodeOptions {
return &EncodeOptions{
Cap: 100,
}
}
type EncodeOption func(*EncodeOptions)
func EcodeWithCap(cap int) EncodeOption {
return func(o *EncodeOptions) {
o.Cap = cap
}
}
// Encoder 编码者
type Encoder struct {
w *bytebufferpool.ByteBuffer
}
// NewEncoder NewEncoder
func NewEncoder() *Encoder {
return &Encoder{
w: pool.Get(),
}
}
// Bytes Bytes
func (e *Encoder) Bytes() []byte {
return e.w.Bytes()
}
// Len Len
func (e *Encoder) Len() int {
return e.w.Len()
}
// WriteByte WriteByte
func (e *Encoder) WriteByte(b byte) error {
return e.w.WriteByte(b)
}
// WriteInt WriteInt
func (e *Encoder) WriteInt(i int) error {
return e.w.WriteByte(byte(i))
}
// WriteUint8 WriteUint8
func (e *Encoder) WriteUint8(i uint8) {
_ = e.WriteInt(int(i))
}
// WriteInt16 WriteInt16
func (e *Encoder) WriteInt16(i int) {
e.w.Write([]byte{byte(i >> 8), byte(i & 0xFF)})
}
// WriteUint16 WriteUint16
func (e *Encoder) WriteUint16(i uint16) {
e.WriteInt16(int(i))
}
// WriteInt32 WriteInt32
func (e *Encoder) WriteInt32(i int32) {
e.w.Write([]byte{
byte(i >> 24),
byte(i >> 16),
byte(i >> 8),
byte(i & 0xFF),
})
}
// WriteInt64 WriteInt64
func (e *Encoder) WriteInt64(i int64) {
e.w.Write([]byte{
byte(i >> 56),
byte(i >> 48),
byte(i >> 40),
byte(i >> 32),
byte(i >> 24),
byte(i >> 16),
byte(i >> 8),
byte(i & 0xFF),
})
}
// WriteUint64 WriteUint64
func (e *Encoder) WriteUint64(i uint64) {
e.w.Write([]byte{
byte(i >> 56),
byte(i >> 48),
byte(i >> 40),
byte(i >> 32),
byte(i >> 24),
byte(i >> 16),
byte(i >> 8),
byte(i & 0xFF),
})
}
// WriteUint32 WriteUint32
func (e *Encoder) WriteUint32(i uint32) {
_ = WriteUint32(i, e.w)
}
// WriteString WriteString
func (e *Encoder) WriteString(str string) {
e.WriteBinary([]byte(str))
}
// WriteStringAll WriteStringAll
func (e *Encoder) WriteStringAll(str string) {
e.WriteBytes([]byte(str))
}
// WriteBinary WriteBinary
func (e *Encoder) WriteBinary(b []byte) {
if len(b) == 0 {
e.WriteInt16(0)
} else {
e.WriteInt16(len(b))
e.w.Write(b)
}
}
// WriteBytes WriteBytes
func (e *Encoder) WriteBytes(b []byte) {
e.w.Write(b)
}
// WriteVariable WriteVariable
func (e *Encoder) WriteVariable(v int) {
b := []byte{}
for v > 0 {
digit := v % 0x80
v /= 0x80
if v > 0 {
digit |= 0x80
}
b = append(b, byte(digit))
}
e.w.Write(b)
}
func (e *Encoder) End() {
bytebufferpool.Put(e.w)
}
// WriteUint32 WriteUint32
func WriteUint32(v uint32, w io.Writer) error {
if _, err := w.Write([]byte{
byte(v >> 24),
byte(v >> 16),
byte(v >> 8),
byte(v & 0xFF),
}); err != nil {
return err
}
return nil
}
// WriteBinary WriteBinary
func WriteBinary(b []byte, w io.Writer) error {
var err error
if len(b) == 0 {
err = WriteInt16(0, w)
if err != nil {
return err
}
} else {
err = WriteInt16(len(b), w)
if err != nil {
return err
}
_, err = w.Write(b)
if err != nil {
return err
}
}
return nil
}
// WriteInt16 WriteInt16
func WriteInt16(i int, w io.Writer) error {
_, err := w.Write([]byte{byte(i >> 8), byte(i & 0xFF)})
return err
}