-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataoutx.go
More file actions
325 lines (291 loc) · 6.78 KB
/
Copy pathdataoutx.go
File metadata and controls
325 lines (291 loc) · 6.78 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package protocol
import (
"encoding/binary"
"io"
"math"
"sync"
)
const (
INT3MinValue = int32(-8388608) // 0xff800000 sign-extended
INT3MaxValue = int32(8388607) // 0x007fffff
LONG5MinValue = int64(-549755813888) // 0xffffff8000000000 sign-extended
LONG5MaxValue = int64(549755813887) // 0x0000007fffffffff
)
type DataOutputX struct {
buf []byte
written int
writer io.Writer // optional: when set, writes to stream instead of buffer
scratch [8]byte // reusable scratch buffer — already on heap with the struct, avoids per-call allocation
}
var dataOutputXPool = sync.Pool{
New: func() any {
return &DataOutputX{buf: make([]byte, 0, 256)}
},
}
// AcquireDataOutputX returns a DataOutputX from the pool, ready for reuse.
func AcquireDataOutputX() *DataOutputX {
o := dataOutputXPool.Get().(*DataOutputX)
o.Reset()
return o
}
// ReleaseDataOutputX returns a DataOutputX to the pool.
// Do not use the DataOutputX after releasing; copy ToByteArray() before calling this.
func ReleaseDataOutputX(o *DataOutputX) {
if o.writer != nil {
return
}
dataOutputXPool.Put(o)
}
func NewDataOutputX() *DataOutputX {
return &DataOutputX{buf: make([]byte, 0, 256)}
}
func NewDataOutputXWithSize(size int) *DataOutputX {
return &DataOutputX{buf: make([]byte, 0, size)}
}
// NewDataOutputXStream creates a stream-based DataOutputX that writes to an io.Writer.
func NewDataOutputXStream(w io.Writer) *DataOutputX {
return &DataOutputX{writer: w}
}
func (o *DataOutputX) ToByteArray() []byte {
return o.buf
}
// Reset clears the buffer for reuse (e.g. from sync.Pool) without reallocating.
func (o *DataOutputX) Reset() {
o.buf = o.buf[:0]
o.written = 0
}
func (o *DataOutputX) Size() int {
return o.written
}
// Flush writes any buffered data to the underlying writer (stream mode only).
func (o *DataOutputX) Flush() error {
if o.writer == nil {
return nil
}
if f, ok := o.writer.(interface{ Flush() error }); ok {
return f.Flush()
}
return nil
}
func (o *DataOutputX) Write(b []byte) *DataOutputX {
if o.writer != nil {
o.writer.Write(b)
o.written += len(b)
return o
}
o.buf = append(o.buf, b...)
o.written += len(b)
return o
}
func (o *DataOutputX) WriteByte(v byte) *DataOutputX {
if o.writer != nil {
o.scratch[0] = v
o.writer.Write(o.scratch[:1])
o.written++
return o
}
o.buf = append(o.buf, v)
o.written++
return o
}
func (o *DataOutputX) WriteBoolean(v bool) *DataOutputX {
if v {
o.WriteByte(1)
} else {
o.WriteByte(0)
}
return o
}
func (o *DataOutputX) WriteInt16(v int16) *DataOutputX {
u := uint16(v)
if o.writer != nil {
binary.BigEndian.PutUint16(o.scratch[:2], u)
o.writer.Write(o.scratch[:2])
o.written += 2
return o
}
o.buf = append(o.buf, byte(u>>8), byte(u))
o.written += 2
return o
}
func (o *DataOutputX) WriteUint16(v uint16) *DataOutputX {
if o.writer != nil {
binary.BigEndian.PutUint16(o.scratch[:2], v)
o.writer.Write(o.scratch[:2])
o.written += 2
return o
}
o.buf = append(o.buf, byte(v>>8), byte(v))
o.written += 2
return o
}
func (o *DataOutputX) WriteInt32(v int32) *DataOutputX {
u := uint32(v)
if o.writer != nil {
binary.BigEndian.PutUint32(o.scratch[:4], u)
o.writer.Write(o.scratch[:4])
o.written += 4
return o
}
o.buf = append(o.buf, byte(u>>24), byte(u>>16), byte(u>>8), byte(u))
o.written += 4
return o
}
func (o *DataOutputX) WriteInt64(v int64) *DataOutputX {
u := uint64(v)
if o.writer != nil {
binary.BigEndian.PutUint64(o.scratch[:8], u)
o.writer.Write(o.scratch[:8])
o.written += 8
return o
}
o.buf = append(o.buf, byte(u>>56), byte(u>>48), byte(u>>40), byte(u>>32),
byte(u>>24), byte(u>>16), byte(u>>8), byte(u))
o.written += 8
return o
}
func (o *DataOutputX) WriteFloat32(v float32) *DataOutputX {
return o.WriteInt32(int32(math.Float32bits(v)))
}
func (o *DataOutputX) WriteFloat64(v float64) *DataOutputX {
return o.WriteInt64(int64(math.Float64bits(v)))
}
func (o *DataOutputX) WriteInt3(v int32) *DataOutputX {
if o.writer != nil {
BigEndian.PutInt3(o.scratch[:3], v)
o.writer.Write(o.scratch[:3])
o.written += 3
return o
}
o.buf = append(o.buf, byte(v>>16), byte(v>>8), byte(v))
o.written += 3
return o
}
func (o *DataOutputX) WriteLong5(v int64) *DataOutputX {
if o.writer != nil {
BigEndian.PutInt5(o.scratch[:5], v)
o.writer.Write(o.scratch[:5])
o.written += 5
return o
}
o.buf = append(o.buf, byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
o.written += 5
return o
}
func (o *DataOutputX) WriteDecimal(v int64) *DataOutputX {
if v == 0 {
o.WriteByte(0)
} else if v >= -128 && v <= 127 {
o.WriteByte(1)
o.WriteByte(byte(int8(v)))
} else if v >= -32768 && v <= 32767 {
o.WriteByte(2)
o.WriteInt16(int16(v))
} else if int64(INT3MinValue) <= v && v <= int64(INT3MaxValue) {
o.WriteByte(3)
o.WriteInt3(int32(v))
} else if v >= -2147483648 && v <= 2147483647 {
o.WriteByte(4)
o.WriteInt32(int32(v))
} else if LONG5MinValue <= v && v <= LONG5MaxValue {
o.WriteByte(5)
o.WriteLong5(v)
} else {
o.WriteByte(8)
o.WriteInt64(v)
}
return o
}
func (o *DataOutputX) WriteBlob(value []byte) *DataOutputX {
if len(value) == 0 {
o.WriteByte(0)
} else {
length := len(value)
if length <= 253 {
o.WriteByte(byte(length))
o.Write(value)
} else if length <= 65535 {
o.WriteByte(255)
o.WriteUint16(uint16(length))
o.Write(value)
} else {
o.WriteByte(254)
o.WriteInt32(int32(length))
o.Write(value)
}
}
return o
}
func (o *DataOutputX) WriteText(s string) *DataOutputX {
if s == "" {
o.WriteByte(0)
} else {
o.WriteBlob([]byte(s))
}
return o
}
func (o *DataOutputX) WriteIntBytes(b []byte) *DataOutputX {
o.WriteInt32(int32(len(b)))
o.Write(b)
return o
}
func (o *DataOutputX) WriteShortBytes(b []byte) *DataOutputX {
o.WriteInt16(int16(len(b)))
o.Write(b)
return o
}
func (o *DataOutputX) WriteArrayInt(v []int32) *DataOutputX {
if v == nil {
o.WriteInt16(0)
} else {
o.WriteInt16(int16(len(v)))
for _, val := range v {
o.WriteInt32(val)
}
}
return o
}
func (o *DataOutputX) WriteArrayLong(v []int64) *DataOutputX {
if v == nil {
o.WriteInt16(0)
} else {
o.WriteInt16(int16(len(v)))
for _, val := range v {
o.WriteInt64(val)
}
}
return o
}
func (o *DataOutputX) WriteArrayFloat(v []float32) *DataOutputX {
if v == nil {
o.WriteInt16(0)
} else {
o.WriteInt16(int16(len(v)))
for _, val := range v {
o.WriteFloat32(val)
}
}
return o
}
func (o *DataOutputX) WriteDecimalArray(v []int64) *DataOutputX {
if v == nil {
o.WriteDecimal(0)
} else {
o.WriteDecimal(int64(len(v)))
for _, val := range v {
o.WriteDecimal(val)
}
}
return o
}
func (o *DataOutputX) WriteDecimalIntArray(v []int32) *DataOutputX {
if v == nil {
o.WriteDecimal(0)
} else {
o.WriteDecimal(int64(len(v)))
for _, val := range v {
o.WriteDecimal(int64(val))
}
}
return o
}