-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigendianwriter.go
490 lines (392 loc) · 10.8 KB
/
bigendianwriter.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package byteio
// File automatically generated with ./gen.sh.
import (
"io"
"math"
)
// BigEndianWriter wraps a io.Writer to provide methods
// to make it easier to Write fundamental types.
type BigEndianWriter struct {
io.Writer
buffer [9]byte
}
// WriteBool Writes a boolean.
func (e *BigEndianWriter) WriteBool(b bool) (int, error) {
if b {
return e.WriteUint8(1)
}
return e.WriteUint8(0)
}
// WriteInt8 Writes a 8 bit int as a int8 using the underlying io.Writer.
func (e *BigEndianWriter) WriteInt8(d int8) (int, error) {
e.buffer[0] = byte(d)
return e.Writer.Write(e.buffer[:1])
}
// WriteInt16 Writes a 16 bit int as a int16 using the underlying io.Writer.
func (e *BigEndianWriter) WriteInt16(d int16) (int, error) {
c := uint16(d)
e.buffer = [9]byte{
byte(c >> 8),
byte(c),
}
return e.Writer.Write(e.buffer[:2])
}
// WriteInt24 Writes a 24 bit int as a int32 using the underlying io.Writer.
func (e *BigEndianWriter) WriteInt24(d int32) (int, error) {
c := uint32(d)
e.buffer = [9]byte{
byte(c >> 16),
byte(c >> 8),
byte(c),
}
return e.Writer.Write(e.buffer[:3])
}
// WriteInt32 Writes a 32 bit int as a int32 using the underlying io.Writer.
func (e *BigEndianWriter) WriteInt32(d int32) (int, error) {
c := uint32(d)
e.buffer = [9]byte{
byte(c >> 24),
byte(c >> 16),
byte(c >> 8),
byte(c),
}
return e.Writer.Write(e.buffer[:4])
}
// WriteInt40 Writes a 40 bit int as a int64 using the underlying io.Writer.
func (e *BigEndianWriter) WriteInt40(d int64) (int, error) {
c := uint64(d)
e.buffer = [9]byte{
byte(c >> 32),
byte(c >> 24),
byte(c >> 16),
byte(c >> 8),
byte(c),
}
return e.Writer.Write(e.buffer[:5])
}
// WriteInt48 Writes a 48 bit int as a int64 using the underlying io.Writer.
func (e *BigEndianWriter) WriteInt48(d int64) (int, error) {
c := uint64(d)
e.buffer = [9]byte{
byte(c >> 40),
byte(c >> 32),
byte(c >> 24),
byte(c >> 16),
byte(c >> 8),
byte(c),
}
return e.Writer.Write(e.buffer[:6])
}
// WriteInt56 Writes a 56 bit int as a int64 using the underlying io.Writer.
func (e *BigEndianWriter) WriteInt56(d int64) (int, error) {
c := uint64(d)
e.buffer = [9]byte{
byte(c >> 48),
byte(c >> 40),
byte(c >> 32),
byte(c >> 24),
byte(c >> 16),
byte(c >> 8),
byte(c),
}
return e.Writer.Write(e.buffer[:7])
}
// WriteInt64 Writes a 64 bit int as a int64 using the underlying io.Writer.
func (e *BigEndianWriter) WriteInt64(d int64) (int, error) {
c := uint64(d)
e.buffer = [9]byte{
byte(c >> 56),
byte(c >> 48),
byte(c >> 40),
byte(c >> 32),
byte(c >> 24),
byte(c >> 16),
byte(c >> 8),
byte(c),
}
return e.Writer.Write(e.buffer[:8])
}
// WriteUint8 Writes a 8 bit uint as a uint8 using the underlying io.Writer.
func (e *BigEndianWriter) WriteUint8(d uint8) (int, error) {
e.buffer[0] = d
return e.Writer.Write(e.buffer[:1])
}
// WriteUint16 Writes a 16 bit uint as a uint16 using the underlying io.Writer.
func (e *BigEndianWriter) WriteUint16(d uint16) (int, error) {
e.buffer = [9]byte{
byte(d >> 8),
byte(d),
}
return e.Writer.Write(e.buffer[:2])
}
// WriteUint24 Writes a 24 bit uint as a uint32 using the underlying io.Writer.
func (e *BigEndianWriter) WriteUint24(d uint32) (int, error) {
e.buffer = [9]byte{
byte(d >> 16),
byte(d >> 8),
byte(d),
}
return e.Writer.Write(e.buffer[:3])
}
// WriteUint32 Writes a 32 bit uint as a uint32 using the underlying io.Writer.
func (e *BigEndianWriter) WriteUint32(d uint32) (int, error) {
e.buffer = [9]byte{
byte(d >> 24),
byte(d >> 16),
byte(d >> 8),
byte(d),
}
return e.Writer.Write(e.buffer[:4])
}
// WriteUint40 Writes a 40 bit uint as a uint64 using the underlying io.Writer.
func (e *BigEndianWriter) WriteUint40(d uint64) (int, error) {
e.buffer = [9]byte{
byte(d >> 32),
byte(d >> 24),
byte(d >> 16),
byte(d >> 8),
byte(d),
}
return e.Writer.Write(e.buffer[:5])
}
// WriteUint48 Writes a 48 bit uint as a uint64 using the underlying io.Writer.
func (e *BigEndianWriter) WriteUint48(d uint64) (int, error) {
e.buffer = [9]byte{
byte(d >> 40),
byte(d >> 32),
byte(d >> 24),
byte(d >> 16),
byte(d >> 8),
byte(d),
}
return e.Writer.Write(e.buffer[:6])
}
// WriteUint56 Writes a 56 bit uint as a uint64 using the underlying io.Writer.
func (e *BigEndianWriter) WriteUint56(d uint64) (int, error) {
e.buffer = [9]byte{
byte(d >> 48),
byte(d >> 40),
byte(d >> 32),
byte(d >> 24),
byte(d >> 16),
byte(d >> 8),
byte(d),
}
return e.Writer.Write(e.buffer[:7])
}
// WriteUint64 Writes a 64 bit uint as a uint64 using the underlying io.Writer.
func (e *BigEndianWriter) WriteUint64(d uint64) (int, error) {
e.buffer = [9]byte{
byte(d >> 56),
byte(d >> 48),
byte(d >> 40),
byte(d >> 32),
byte(d >> 24),
byte(d >> 16),
byte(d >> 8),
byte(d),
}
return e.Writer.Write(e.buffer[:8])
}
// WriteFloat32 Writes a 32 bit float as a float32 using the underlying io.Writer.
func (e *BigEndianWriter) WriteFloat32(d float32) (int, error) {
c := math.Float32bits(d)
e.buffer = [9]byte{
byte(c >> 24),
byte(c >> 16),
byte(c >> 8),
byte(c),
}
return e.Writer.Write(e.buffer[:4])
}
// WriteFloat64 Writes a 64 bit float as a float64 using the underlying io.Writer.
func (e *BigEndianWriter) WriteFloat64(d float64) (int, error) {
c := math.Float64bits(d)
e.buffer = [9]byte{
byte(c >> 56),
byte(c >> 48),
byte(c >> 40),
byte(c >> 32),
byte(c >> 24),
byte(c >> 16),
byte(c >> 8),
byte(c),
}
return e.Writer.Write(e.buffer[:8])
}
// WriteBytesX Writes the length of the Bytes, using ReadUintX and then Writes the bytes.
func (e *BigEndianWriter) WriteBytesX(p []byte) (int, error) {
n, err := e.WriteUintX(uint64(len(p)))
if err != nil {
return n, err
}
m, err := e.Write(p)
return n + m, err
}
// WriteBytes8 Writes the length of the Bytes, using ReadUint8 and then Writes the bytes.
func (e *BigEndianWriter) WriteBytes8(p []byte) (int, error) {
n, err := e.WriteUint8(uint8(len(p)))
if err != nil {
return n, err
}
m, err := e.Write(p)
return n + m, err
}
// WriteBytes16 Writes the length of the Bytes, using ReadUint16 and then Writes the bytes.
func (e *BigEndianWriter) WriteBytes16(p []byte) (int, error) {
n, err := e.WriteUint16(uint16(len(p)))
if err != nil {
return n, err
}
m, err := e.Write(p)
return n + m, err
}
// WriteBytes24 Writes the length of the Bytes, using ReadUint24 and then Writes the bytes.
func (e *BigEndianWriter) WriteBytes24(p []byte) (int, error) {
n, err := e.WriteUint24(uint32(len(p)))
if err != nil {
return n, err
}
m, err := e.Write(p)
return n + m, err
}
// WriteBytes32 Writes the length of the Bytes, using ReadUint32 and then Writes the bytes.
func (e *BigEndianWriter) WriteBytes32(p []byte) (int, error) {
n, err := e.WriteUint32(uint32(len(p)))
if err != nil {
return n, err
}
m, err := e.Write(p)
return n + m, err
}
// WriteBytes40 Writes the length of the Bytes, using ReadUint40 and then Writes the bytes.
func (e *BigEndianWriter) WriteBytes40(p []byte) (int, error) {
n, err := e.WriteUint40(uint64(len(p)))
if err != nil {
return n, err
}
m, err := e.Write(p)
return n + m, err
}
// WriteBytes48 Writes the length of the Bytes, using ReadUint48 and then Writes the bytes.
func (e *BigEndianWriter) WriteBytes48(p []byte) (int, error) {
n, err := e.WriteUint48(uint64(len(p)))
if err != nil {
return n, err
}
m, err := e.Write(p)
return n + m, err
}
// WriteBytes56 Writes the length of the Bytes, using ReadUint56 and then Writes the bytes.
func (e *BigEndianWriter) WriteBytes56(p []byte) (int, error) {
n, err := e.WriteUint56(uint64(len(p)))
if err != nil {
return n, err
}
m, err := e.Write(p)
return n + m, err
}
// WriteBytes64 Writes the length of the Bytes, using ReadUint64 and then Writes the bytes.
func (e *BigEndianWriter) WriteBytes64(p []byte) (int, error) {
n, err := e.WriteUint64(uint64(len(p)))
if err != nil {
return n, err
}
m, err := e.Write(p)
return n + m, err
}
// WriteString Writes a string.
func (e *BigEndianWriter) WriteString(d string) (int, error) {
return io.WriteString(e.Writer, d)
}
// WriteStringX Writes the length of the String, using ReadUintX and then Writes the bytes.
func (e *BigEndianWriter) WriteStringX(p string) (int, error) {
n, err := e.WriteUintX(uint64(len(p)))
if err != nil {
return n, err
}
m, err := e.WriteString(p)
return n + m, err
}
// WriteString8 Writes the length of the String, using ReadUint8 and then Writes the bytes.
func (e *BigEndianWriter) WriteString8(p string) (int, error) {
n, err := e.WriteUint8(uint8(len(p)))
if err != nil {
return n, err
}
m, err := e.WriteString(p)
return n + m, err
}
// WriteString16 Writes the length of the String, using ReadUint16 and then Writes the bytes.
func (e *BigEndianWriter) WriteString16(p string) (int, error) {
n, err := e.WriteUint16(uint16(len(p)))
if err != nil {
return n, err
}
m, err := e.WriteString(p)
return n + m, err
}
// WriteString24 Writes the length of the String, using ReadUint24 and then Writes the bytes.
func (e *BigEndianWriter) WriteString24(p string) (int, error) {
n, err := e.WriteUint24(uint32(len(p)))
if err != nil {
return n, err
}
m, err := e.WriteString(p)
return n + m, err
}
// WriteString32 Writes the length of the String, using ReadUint32 and then Writes the bytes.
func (e *BigEndianWriter) WriteString32(p string) (int, error) {
n, err := e.WriteUint32(uint32(len(p)))
if err != nil {
return n, err
}
m, err := e.WriteString(p)
return n + m, err
}
// WriteString40 Writes the length of the String, using ReadUint40 and then Writes the bytes.
func (e *BigEndianWriter) WriteString40(p string) (int, error) {
n, err := e.WriteUint40(uint64(len(p)))
if err != nil {
return n, err
}
m, err := e.WriteString(p)
return n + m, err
}
// WriteString48 Writes the length of the String, using ReadUint48 and then Writes the bytes.
func (e *BigEndianWriter) WriteString48(p string) (int, error) {
n, err := e.WriteUint48(uint64(len(p)))
if err != nil {
return n, err
}
m, err := e.WriteString(p)
return n + m, err
}
// WriteString56 Writes the length of the String, using ReadUint56 and then Writes the bytes.
func (e *BigEndianWriter) WriteString56(p string) (int, error) {
n, err := e.WriteUint56(uint64(len(p)))
if err != nil {
return n, err
}
m, err := e.WriteString(p)
return n + m, err
}
// WriteString64 Writes the length of the String, using ReadUint64 and then Writes the bytes.
func (e *BigEndianWriter) WriteString64(p string) (int, error) {
n, err := e.WriteUint64(uint64(len(p)))
if err != nil {
return n, err
}
m, err := e.WriteString(p)
return n + m, err
}
// WriteString0 Writes the bytes of the string ending with a 0 byte.
func (e *BigEndianWriter) WriteString0(p string) (int, error) {
n, err := e.WriteString(p)
if err == nil {
var m int
m, err = e.WriteUint8(0)
n += m
}
return n, err
}