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