byteio - stickybigendianwriter.go
1 package byteio
2
3 // File automatically generated with ./gen.sh.
4
5 import (
6 "io"
7 "math"
8 )
9
10 // StickyBigEndianWriter wraps a io.Writer to provide methods
11 // to make it easier to Write fundamental types.
12 type StickyBigEndianWriter struct {
13 io.Writer
14 buffer [9]byte
15 Err error
16 Count int64
17 }
18
19 // GetCount returns any error received.
20 func (e *StickyBigEndianWriter) GetError() error {
21 return e.Err
22 }
23
24 // GetCount returns the number of bytes written.
25 func (e *StickyBigEndianWriter) GetCount() int64 {
26 return e.Count
27 }
28
29 // Write implements the io.Writer interface.
30 func (e *StickyBigEndianWriter) Write(p []byte) (int, error) {
31 if e.Err != nil {
32 return 0, e.Err
33 }
34
35 n, err := e.Writer.Write(p)
36 e.Err = err
37 e.Count += int64(n)
38
39 return n, e.Err
40 }
41
42 // WriteBool Writes a boolean.
43 func (e *StickyBigEndianWriter) WriteBool(b bool) {
44 if b {
45 e.WriteUint8(1)
46 } else {
47 e.WriteUint8(0)
48 }
49 }
50
51 // WriteInt8 Writes a 8 bit int as a int8 using the underlying io.Writer.
52 // Any errors and the running byte read count are stored instead of being returned.
53 func (e *StickyBigEndianWriter) WriteInt8(d int8) {
54 if e.Err != nil {
55 return
56 }
57
58 e.buffer[0] = byte(d)
59
60 var n int
61
62 n, e.Err = e.Writer.Write(e.buffer[:1])
63 e.Count += int64(n)
64 }
65
66 // WriteInt16 Writes a 16 bit int as a int16 using the underlying io.Writer.
67 // Any errors and the running byte read count are stored instead of being returned.
68 func (e *StickyBigEndianWriter) WriteInt16(d int16) {
69 if e.Err != nil {
70 return
71 }
72
73 c := uint16(d)
74 e.buffer = [9]byte{
75 byte(c >> 8),
76 byte(c),
77 }
78
79 var n int
80
81 n, e.Err = e.Writer.Write(e.buffer[:2])
82 e.Count += int64(n)
83 }
84
85 // WriteInt24 Writes a 24 bit int as a int32 using the underlying io.Writer.
86 // Any errors and the running byte read count are stored instead of being returned.
87 func (e *StickyBigEndianWriter) WriteInt24(d int32) {
88 if e.Err != nil {
89 return
90 }
91
92 c := uint32(d)
93 e.buffer = [9]byte{
94 byte(c >> 16),
95 byte(c >> 8),
96 byte(c),
97 }
98
99 var n int
100
101 n, e.Err = e.Writer.Write(e.buffer[:3])
102 e.Count += int64(n)
103 }
104
105 // WriteInt32 Writes a 32 bit int as a int32 using the underlying io.Writer.
106 // Any errors and the running byte read count are stored instead of being returned.
107 func (e *StickyBigEndianWriter) WriteInt32(d int32) {
108 if e.Err != nil {
109 return
110 }
111
112 c := uint32(d)
113 e.buffer = [9]byte{
114 byte(c >> 24),
115 byte(c >> 16),
116 byte(c >> 8),
117 byte(c),
118 }
119
120 var n int
121
122 n, e.Err = e.Writer.Write(e.buffer[:4])
123 e.Count += int64(n)
124 }
125
126 // WriteInt40 Writes a 40 bit int as a int64 using the underlying io.Writer.
127 // Any errors and the running byte read count are stored instead of being returned.
128 func (e *StickyBigEndianWriter) WriteInt40(d int64) {
129 if e.Err != nil {
130 return
131 }
132
133 c := uint64(d)
134 e.buffer = [9]byte{
135 byte(c >> 32),
136 byte(c >> 24),
137 byte(c >> 16),
138 byte(c >> 8),
139 byte(c),
140 }
141
142 var n int
143
144 n, e.Err = e.Writer.Write(e.buffer[:5])
145 e.Count += int64(n)
146 }
147
148 // WriteInt48 Writes a 48 bit int as a int64 using the underlying io.Writer.
149 // Any errors and the running byte read count are stored instead of being returned.
150 func (e *StickyBigEndianWriter) WriteInt48(d int64) {
151 if e.Err != nil {
152 return
153 }
154
155 c := uint64(d)
156 e.buffer = [9]byte{
157 byte(c >> 40),
158 byte(c >> 32),
159 byte(c >> 24),
160 byte(c >> 16),
161 byte(c >> 8),
162 byte(c),
163 }
164
165 var n int
166
167 n, e.Err = e.Writer.Write(e.buffer[:6])
168 e.Count += int64(n)
169 }
170
171 // WriteInt56 Writes a 56 bit int as a int64 using the underlying io.Writer.
172 // Any errors and the running byte read count are stored instead of being returned.
173 func (e *StickyBigEndianWriter) WriteInt56(d int64) {
174 if e.Err != nil {
175 return
176 }
177
178 c := uint64(d)
179 e.buffer = [9]byte{
180 byte(c >> 48),
181 byte(c >> 40),
182 byte(c >> 32),
183 byte(c >> 24),
184 byte(c >> 16),
185 byte(c >> 8),
186 byte(c),
187 }
188
189 var n int
190
191 n, e.Err = e.Writer.Write(e.buffer[:7])
192 e.Count += int64(n)
193 }
194
195 // WriteInt64 Writes a 64 bit int as a int64 using the underlying io.Writer.
196 // Any errors and the running byte read count are stored instead of being returned.
197 func (e *StickyBigEndianWriter) WriteInt64(d int64) {
198 if e.Err != nil {
199 return
200 }
201
202 c := uint64(d)
203 e.buffer = [9]byte{
204 byte(c >> 56),
205 byte(c >> 48),
206 byte(c >> 40),
207 byte(c >> 32),
208 byte(c >> 24),
209 byte(c >> 16),
210 byte(c >> 8),
211 byte(c),
212 }
213
214 var n int
215
216 n, e.Err = e.Writer.Write(e.buffer[:8])
217 e.Count += int64(n)
218 }
219
220 // WriteUint8 Writes a 8 bit uint as a uint8 using the underlying io.Writer.
221 // Any errors and the running byte read count are stored instead of being returned.
222 func (e *StickyBigEndianWriter) WriteUint8(d uint8) {
223 if e.Err != nil {
224 return
225 }
226
227 e.buffer[0] = d
228
229 var n int
230
231 n, e.Err = e.Writer.Write(e.buffer[:1])
232 e.Count += int64(n)
233 }
234
235 // WriteUint16 Writes a 16 bit uint as a uint16 using the underlying io.Writer.
236 // Any errors and the running byte read count are stored instead of being returned.
237 func (e *StickyBigEndianWriter) WriteUint16(d uint16) {
238 if e.Err != nil {
239 return
240 }
241
242 e.buffer = [9]byte{
243 byte(d >> 8),
244 byte(d),
245 }
246
247 var n int
248
249 n, e.Err = e.Writer.Write(e.buffer[:2])
250 e.Count += int64(n)
251 }
252
253 // WriteUint24 Writes a 24 bit uint as a uint32 using the underlying io.Writer.
254 // Any errors and the running byte read count are stored instead of being returned.
255 func (e *StickyBigEndianWriter) WriteUint24(d uint32) {
256 if e.Err != nil {
257 return
258 }
259
260 e.buffer = [9]byte{
261 byte(d >> 16),
262 byte(d >> 8),
263 byte(d),
264 }
265
266 var n int
267
268 n, e.Err = e.Writer.Write(e.buffer[:3])
269 e.Count += int64(n)
270 }
271
272 // WriteUint32 Writes a 32 bit uint as a uint32 using the underlying io.Writer.
273 // Any errors and the running byte read count are stored instead of being returned.
274 func (e *StickyBigEndianWriter) WriteUint32(d uint32) {
275 if e.Err != nil {
276 return
277 }
278
279 e.buffer = [9]byte{
280 byte(d >> 24),
281 byte(d >> 16),
282 byte(d >> 8),
283 byte(d),
284 }
285
286 var n int
287
288 n, e.Err = e.Writer.Write(e.buffer[:4])
289 e.Count += int64(n)
290 }
291
292 // WriteUint40 Writes a 40 bit uint as a uint64 using the underlying io.Writer.
293 // Any errors and the running byte read count are stored instead of being returned.
294 func (e *StickyBigEndianWriter) WriteUint40(d uint64) {
295 if e.Err != nil {
296 return
297 }
298
299 e.buffer = [9]byte{
300 byte(d >> 32),
301 byte(d >> 24),
302 byte(d >> 16),
303 byte(d >> 8),
304 byte(d),
305 }
306
307 var n int
308
309 n, e.Err = e.Writer.Write(e.buffer[:5])
310 e.Count += int64(n)
311 }
312
313 // WriteUint48 Writes a 48 bit uint as a uint64 using the underlying io.Writer.
314 // Any errors and the running byte read count are stored instead of being returned.
315 func (e *StickyBigEndianWriter) WriteUint48(d uint64) {
316 if e.Err != nil {
317 return
318 }
319
320 e.buffer = [9]byte{
321 byte(d >> 40),
322 byte(d >> 32),
323 byte(d >> 24),
324 byte(d >> 16),
325 byte(d >> 8),
326 byte(d),
327 }
328
329 var n int
330
331 n, e.Err = e.Writer.Write(e.buffer[:6])
332 e.Count += int64(n)
333 }
334
335 // WriteUint56 Writes a 56 bit uint as a uint64 using the underlying io.Writer.
336 // Any errors and the running byte read count are stored instead of being returned.
337 func (e *StickyBigEndianWriter) WriteUint56(d uint64) {
338 if e.Err != nil {
339 return
340 }
341
342 e.buffer = [9]byte{
343 byte(d >> 48),
344 byte(d >> 40),
345 byte(d >> 32),
346 byte(d >> 24),
347 byte(d >> 16),
348 byte(d >> 8),
349 byte(d),
350 }
351
352 var n int
353
354 n, e.Err = e.Writer.Write(e.buffer[:7])
355 e.Count += int64(n)
356 }
357
358 // WriteUint64 Writes a 64 bit uint as a uint64 using the underlying io.Writer.
359 // Any errors and the running byte read count are stored instead of being returned.
360 func (e *StickyBigEndianWriter) WriteUint64(d uint64) {
361 if e.Err != nil {
362 return
363 }
364
365 e.buffer = [9]byte{
366 byte(d >> 56),
367 byte(d >> 48),
368 byte(d >> 40),
369 byte(d >> 32),
370 byte(d >> 24),
371 byte(d >> 16),
372 byte(d >> 8),
373 byte(d),
374 }
375
376 var n int
377
378 n, e.Err = e.Writer.Write(e.buffer[:8])
379 e.Count += int64(n)
380 }
381
382 // WriteFloat32 Writes a 32 bit float as a float32 using the underlying io.Writer.
383 // Any errors and the running byte read count are stored instead of being returned.
384 func (e *StickyBigEndianWriter) WriteFloat32(d float32) {
385 if e.Err != nil {
386 return
387 }
388
389 c := math.Float32bits(d)
390 e.buffer = [9]byte{
391 byte(c >> 24),
392 byte(c >> 16),
393 byte(c >> 8),
394 byte(c),
395 }
396
397 var n int
398
399 n, e.Err = e.Writer.Write(e.buffer[:4])
400 e.Count += int64(n)
401 }
402
403 // WriteFloat64 Writes a 64 bit float as a float64 using the underlying io.Writer.
404 // Any errors and the running byte read count are stored instead of being returned.
405 func (e *StickyBigEndianWriter) WriteFloat64(d float64) {
406 if e.Err != nil {
407 return
408 }
409
410 c := math.Float64bits(d)
411 e.buffer = [9]byte{
412 byte(c >> 56),
413 byte(c >> 48),
414 byte(c >> 40),
415 byte(c >> 32),
416 byte(c >> 24),
417 byte(c >> 16),
418 byte(c >> 8),
419 byte(c),
420 }
421
422 var n int
423
424 n, e.Err = e.Writer.Write(e.buffer[:8])
425 e.Count += int64(n)
426 }
427
428 // WriteBytes Writes a []byte.
429 func (e *StickyBigEndianWriter) WriteBytes(d []byte) {
430 if e.Err != nil {
431 return
432 }
433
434 n, err := e.Write(d)
435 e.Count += int64(n)
436 e.Err = err
437 }
438
439 // WriteBytesX Writes the length of the Bytes, using WriteUintX and then Writes the bytes.
440 func (e *StickyBigEndianWriter) WriteBytesX(p []byte) {
441 e.WriteUintX(uint64(len(p)))
442 e.Write(p)
443 }
444
445 // WriteBytes8 Writes the length of the Bytes, using WriteUint8 and then Writes the bytes.
446 func (e *StickyBigEndianWriter) WriteBytes8(p []byte) {
447 e.WriteUint8(uint8(len(p)))
448 e.Write(p)
449 }
450
451 // WriteBytes16 Writes the length of the Bytes, using WriteUint16 and then Writes the bytes.
452 func (e *StickyBigEndianWriter) WriteBytes16(p []byte) {
453 e.WriteUint16(uint16(len(p)))
454 e.Write(p)
455 }
456
457 // WriteBytes24 Writes the length of the Bytes, using WriteUint24 and then Writes the bytes.
458 func (e *StickyBigEndianWriter) WriteBytes24(p []byte) {
459 e.WriteUint24(uint32(len(p)))
460 e.Write(p)
461 }
462
463 // WriteBytes32 Writes the length of the Bytes, using WriteUint32 and then Writes the bytes.
464 func (e *StickyBigEndianWriter) WriteBytes32(p []byte) {
465 e.WriteUint32(uint32(len(p)))
466 e.Write(p)
467 }
468
469 // WriteBytes40 Writes the length of the Bytes, using WriteUint40 and then Writes the bytes.
470 func (e *StickyBigEndianWriter) WriteBytes40(p []byte) {
471 e.WriteUint40(uint64(len(p)))
472 e.Write(p)
473 }
474
475 // WriteBytes48 Writes the length of the Bytes, using WriteUint48 and then Writes the bytes.
476 func (e *StickyBigEndianWriter) WriteBytes48(p []byte) {
477 e.WriteUint48(uint64(len(p)))
478 e.Write(p)
479 }
480
481 // WriteBytes56 Writes the length of the Bytes, using WriteUint56 and then Writes the bytes.
482 func (e *StickyBigEndianWriter) WriteBytes56(p []byte) {
483 e.WriteUint56(uint64(len(p)))
484 e.Write(p)
485 }
486
487 // WriteBytes64 Writes the length of the Bytes, using WriteUint64 and then Writes the bytes.
488 func (e *StickyBigEndianWriter) WriteBytes64(p []byte) {
489 e.WriteUint64(uint64(len(p)))
490 e.Write(p)
491 }
492
493 // WriteString Writes a string.
494 func (e *StickyBigEndianWriter) WriteString(d string) (int, error) {
495 if e.Err != nil {
496 return 0, e.Err
497 }
498
499 n, err := io.WriteString(e.Writer, d)
500 e.Count += int64(n)
501 e.Err = err
502
503 return n, err
504 }
505
506 // WriteStringX Writes the length of the String, using WriteUintX and then Writes the bytes.
507 func (e *StickyBigEndianWriter) WriteStringX(p string) {
508 e.WriteUintX(uint64(len(p)))
509 e.WriteString(p)
510 }
511
512 // WriteString8 Writes the length of the String, using WriteUint8 and then Writes the bytes.
513 func (e *StickyBigEndianWriter) WriteString8(p string) {
514 e.WriteUint8(uint8(len(p)))
515 e.WriteString(p)
516 }
517
518 // WriteString16 Writes the length of the String, using WriteUint16 and then Writes the bytes.
519 func (e *StickyBigEndianWriter) WriteString16(p string) {
520 e.WriteUint16(uint16(len(p)))
521 e.WriteString(p)
522 }
523
524 // WriteString24 Writes the length of the String, using WriteUint24 and then Writes the bytes.
525 func (e *StickyBigEndianWriter) WriteString24(p string) {
526 e.WriteUint24(uint32(len(p)))
527 e.WriteString(p)
528 }
529
530 // WriteString32 Writes the length of the String, using WriteUint32 and then Writes the bytes.
531 func (e *StickyBigEndianWriter) WriteString32(p string) {
532 e.WriteUint32(uint32(len(p)))
533 e.WriteString(p)
534 }
535
536 // WriteString40 Writes the length of the String, using WriteUint40 and then Writes the bytes.
537 func (e *StickyBigEndianWriter) WriteString40(p string) {
538 e.WriteUint40(uint64(len(p)))
539 e.WriteString(p)
540 }
541
542 // WriteString48 Writes the length of the String, using WriteUint48 and then Writes the bytes.
543 func (e *StickyBigEndianWriter) WriteString48(p string) {
544 e.WriteUint48(uint64(len(p)))
545 e.WriteString(p)
546 }
547
548 // WriteString56 Writes the length of the String, using WriteUint56 and then Writes the bytes.
549 func (e *StickyBigEndianWriter) WriteString56(p string) {
550 e.WriteUint56(uint64(len(p)))
551 e.WriteString(p)
552 }
553
554 // WriteString64 Writes the length of the String, using WriteUint64 and then Writes the bytes.
555 func (e *StickyBigEndianWriter) WriteString64(p string) {
556 e.WriteUint64(uint64(len(p)))
557 e.WriteString(p)
558 }
559
560 // WriteString0 Writes the bytes of the string ending with a 0 byte.
561 func (e *StickyBigEndianWriter) WriteString0(p string) {
562 e.WriteString(p)
563 e.WriteUint8(0)
564 }
565