byteio - stickylittleendianreader.go
1 package byteio
2
3 // File automatically generated with ./gen.sh
4
5 import (
6 "io"
7 "math"
8 )
9
10 // StickyLittleEndianReader wraps a io.Reader to provide methods
11 // to make it easier to Read fundamental types
12 type StickyLittleEndianReader struct {
13 io.Reader
14 buffer [9]byte
15 Err error
16 Count int64
17 }
18
19 // Read implements the io.Reader interface
20 func (e *StickyLittleEndianReader) Read(p []byte) (int, error) {
21 if e.Err != nil {
22 return 0, e.Err
23 }
24 var n int
25 n, e.Err = io.ReadFull(e.Reader, p)
26 e.Count += int64(n)
27 return n, e.Err
28 }
29
30 // ReadBool Reads a boolean
31 func (e *StickyLittleEndianReader) ReadBool() bool {
32 return e.ReadUint8() != 0
33 }
34
35 // ReadInt8 Reads a 8 bit int as a int8 using the underlying io.Reader
36 // Any errors and the running byte read count are stored instead of being returned
37 func (e *StickyLittleEndianReader) ReadInt8() int8 {
38 if e.Err != nil {
39 return 0
40 }
41 n, err := io.ReadFull(e.Reader, e.buffer[:1])
42 e.Count += int64(n)
43 if err != nil {
44 e.Err = err
45 return 0
46 }
47 return int8(e.buffer[0])
48 }
49
50 // ReadInt16 Reads a 16 bit int as a int16 using the underlying io.Reader
51 // Any errors and the running byte read count are stored instead of being returned
52 func (e *StickyLittleEndianReader) ReadInt16() int16 {
53 if e.Err != nil {
54 return 0
55 }
56 n, err := io.ReadFull(e.Reader, e.buffer[:2])
57 e.Count += int64(n)
58 if err != nil {
59 e.Err = err
60 return 0
61 }
62 return int16(uint16(e.buffer[0]) | uint16(e.buffer[1])<<8)
63 }
64
65 // ReadInt24 Reads a 24 bit int as a int32 using the underlying io.Reader
66 // Any errors and the running byte read count are stored instead of being returned
67 func (e *StickyLittleEndianReader) ReadInt24() int32 {
68 if e.Err != nil {
69 return 0
70 }
71 n, err := io.ReadFull(e.Reader, e.buffer[:3])
72 e.Count += int64(n)
73 if err != nil {
74 e.Err = err
75 return 0
76 }
77 return int32(uint32(e.buffer[0]) | uint32(e.buffer[1])<<8 | uint32(e.buffer[2])<<16)
78 }
79
80 // ReadInt32 Reads a 32 bit int as a int32 using the underlying io.Reader
81 // Any errors and the running byte read count are stored instead of being returned
82 func (e *StickyLittleEndianReader) ReadInt32() int32 {
83 if e.Err != nil {
84 return 0
85 }
86 n, err := io.ReadFull(e.Reader, e.buffer[:4])
87 e.Count += int64(n)
88 if err != nil {
89 e.Err = err
90 return 0
91 }
92 return int32(uint32(e.buffer[0]) | uint32(e.buffer[1])<<8 | uint32(e.buffer[2])<<16 | uint32(e.buffer[3])<<24)
93 }
94
95 // ReadInt40 Reads a 40 bit int as a int64 using the underlying io.Reader
96 // Any errors and the running byte read count are stored instead of being returned
97 func (e *StickyLittleEndianReader) ReadInt40() int64 {
98 if e.Err != nil {
99 return 0
100 }
101 n, err := io.ReadFull(e.Reader, e.buffer[:5])
102 e.Count += int64(n)
103 if err != nil {
104 e.Err = err
105 return 0
106 }
107 return int64(uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32)
108 }
109
110 // ReadInt48 Reads a 48 bit int as a int64 using the underlying io.Reader
111 // Any errors and the running byte read count are stored instead of being returned
112 func (e *StickyLittleEndianReader) ReadInt48() int64 {
113 if e.Err != nil {
114 return 0
115 }
116 n, err := io.ReadFull(e.Reader, e.buffer[:6])
117 e.Count += int64(n)
118 if err != nil {
119 e.Err = err
120 return 0
121 }
122 return int64(uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40)
123 }
124
125 // ReadInt56 Reads a 56 bit int as a int64 using the underlying io.Reader
126 // Any errors and the running byte read count are stored instead of being returned
127 func (e *StickyLittleEndianReader) ReadInt56() int64 {
128 if e.Err != nil {
129 return 0
130 }
131 n, err := io.ReadFull(e.Reader, e.buffer[:7])
132 e.Count += int64(n)
133 if err != nil {
134 e.Err = err
135 return 0
136 }
137 return int64(uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40 | uint64(e.buffer[6])<<48)
138 }
139
140 // ReadInt64 Reads a 64 bit int as a int64 using the underlying io.Reader
141 // Any errors and the running byte read count are stored instead of being returned
142 func (e *StickyLittleEndianReader) ReadInt64() int64 {
143 if e.Err != nil {
144 return 0
145 }
146 n, err := io.ReadFull(e.Reader, e.buffer[:8])
147 e.Count += int64(n)
148 if err != nil {
149 e.Err = err
150 return 0
151 }
152 return int64(uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40 | uint64(e.buffer[6])<<48 | uint64(e.buffer[7])<<56)
153 }
154
155 // ReadUint8 Reads a 8 bit uint as a uint8 using the underlying io.Reader
156 // Any errors and the running byte read count are stored instead of being returned
157 func (e *StickyLittleEndianReader) ReadUint8() uint8 {
158 if e.Err != nil {
159 return 0
160 }
161 n, err := io.ReadFull(e.Reader, e.buffer[:1])
162 e.Count += int64(n)
163 if err != nil {
164 e.Err = err
165 return 0
166 }
167 return e.buffer[0]
168 }
169
170 // ReadUint16 Reads a 16 bit uint as a uint16 using the underlying io.Reader
171 // Any errors and the running byte read count are stored instead of being returned
172 func (e *StickyLittleEndianReader) ReadUint16() uint16 {
173 if e.Err != nil {
174 return 0
175 }
176 n, err := io.ReadFull(e.Reader, e.buffer[:2])
177 e.Count += int64(n)
178 if err != nil {
179 e.Err = err
180 return 0
181 }
182 return uint16(e.buffer[0]) | uint16(e.buffer[1])<<8
183 }
184
185 // ReadUint24 Reads a 24 bit uint as a uint32 using the underlying io.Reader
186 // Any errors and the running byte read count are stored instead of being returned
187 func (e *StickyLittleEndianReader) ReadUint24() uint32 {
188 if e.Err != nil {
189 return 0
190 }
191 n, err := io.ReadFull(e.Reader, e.buffer[:3])
192 e.Count += int64(n)
193 if err != nil {
194 e.Err = err
195 return 0
196 }
197 return uint32(e.buffer[0]) | uint32(e.buffer[1])<<8 | uint32(e.buffer[2])<<16
198 }
199
200 // ReadUint32 Reads a 32 bit uint as a uint32 using the underlying io.Reader
201 // Any errors and the running byte read count are stored instead of being returned
202 func (e *StickyLittleEndianReader) ReadUint32() uint32 {
203 if e.Err != nil {
204 return 0
205 }
206 n, err := io.ReadFull(e.Reader, e.buffer[:4])
207 e.Count += int64(n)
208 if err != nil {
209 e.Err = err
210 return 0
211 }
212 return uint32(e.buffer[0]) | uint32(e.buffer[1])<<8 | uint32(e.buffer[2])<<16 | uint32(e.buffer[3])<<24
213 }
214
215 // ReadUint40 Reads a 40 bit uint as a uint64 using the underlying io.Reader
216 // Any errors and the running byte read count are stored instead of being returned
217 func (e *StickyLittleEndianReader) ReadUint40() uint64 {
218 if e.Err != nil {
219 return 0
220 }
221 n, err := io.ReadFull(e.Reader, e.buffer[:5])
222 e.Count += int64(n)
223 if err != nil {
224 e.Err = err
225 return 0
226 }
227 return uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32
228 }
229
230 // ReadUint48 Reads a 48 bit uint as a uint64 using the underlying io.Reader
231 // Any errors and the running byte read count are stored instead of being returned
232 func (e *StickyLittleEndianReader) ReadUint48() uint64 {
233 if e.Err != nil {
234 return 0
235 }
236 n, err := io.ReadFull(e.Reader, e.buffer[:6])
237 e.Count += int64(n)
238 if err != nil {
239 e.Err = err
240 return 0
241 }
242 return uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40
243 }
244
245 // ReadUint56 Reads a 56 bit uint as a uint64 using the underlying io.Reader
246 // Any errors and the running byte read count are stored instead of being returned
247 func (e *StickyLittleEndianReader) ReadUint56() uint64 {
248 if e.Err != nil {
249 return 0
250 }
251 n, err := io.ReadFull(e.Reader, e.buffer[:7])
252 e.Count += int64(n)
253 if err != nil {
254 e.Err = err
255 return 0
256 }
257 return uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40 | uint64(e.buffer[6])<<48
258 }
259
260 // ReadUint64 Reads a 64 bit uint as a uint64 using the underlying io.Reader
261 // Any errors and the running byte read count are stored instead of being returned
262 func (e *StickyLittleEndianReader) ReadUint64() uint64 {
263 if e.Err != nil {
264 return 0
265 }
266 n, err := io.ReadFull(e.Reader, e.buffer[:8])
267 e.Count += int64(n)
268 if err != nil {
269 e.Err = err
270 return 0
271 }
272 return uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40 | uint64(e.buffer[6])<<48 | uint64(e.buffer[7])<<56
273 }
274
275 // ReadFloat32 Reads a 32 bit float as a float32 using the underlying io.Reader
276 // Any errors and the running byte read count are stored instead of being returned
277 func (e *StickyLittleEndianReader) ReadFloat32() float32 {
278 if e.Err != nil {
279 return 0
280 }
281 n, err := io.ReadFull(e.Reader, e.buffer[:4])
282 e.Count += int64(n)
283 if err != nil {
284 e.Err = err
285 return 0
286 }
287 return math.Float32frombits(uint32(e.buffer[0]) | uint32(e.buffer[1])<<8 | uint32(e.buffer[2])<<16 | uint32(e.buffer[3])<<24)
288 }
289
290 // ReadFloat64 Reads a 64 bit float as a float64 using the underlying io.Reader
291 // Any errors and the running byte read count are stored instead of being returned
292 func (e *StickyLittleEndianReader) ReadFloat64() float64 {
293 if e.Err != nil {
294 return 0
295 }
296 n, err := io.ReadFull(e.Reader, e.buffer[:8])
297 e.Count += int64(n)
298 if err != nil {
299 e.Err = err
300 return 0
301 }
302 return math.Float64frombits(uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40 | uint64(e.buffer[6])<<48 | uint64(e.buffer[7])<<56)
303 }
304
305 // ReadBytes Reads a []byte
306 func (e *StickyLittleEndianReader) ReadBytes(size int) []byte {
307 if e.Err != nil {
308 return nil
309 }
310 buf := make([]byte, size)
311 var n int
312 n, e.Err = io.ReadFull(e.Reader, buf)
313 e.Count += int64(n)
314 return buf[:n]
315 }
316
317 // ReadBytesX Reads the length of the Bytes, using ReadUintX and then Reads the bytes
318 func (e *StickyLittleEndianReader) ReadBytesX() []byte {
319 return e.ReadBytes(int(e.ReadUintX()))
320 }
321
322 // ReadBytes8 Reads the length of the Bytes, using ReadUint8 and then Reads the bytes
323 func (e *StickyLittleEndianReader) ReadBytes8() []byte {
324 return e.ReadBytes(int(e.ReadUint8()))
325 }
326
327 // ReadBytes16 Reads the length of the Bytes, using ReadUint16 and then Reads the bytes
328 func (e *StickyLittleEndianReader) ReadBytes16() []byte {
329 return e.ReadBytes(int(e.ReadUint16()))
330 }
331
332 // ReadBytes24 Reads the length of the Bytes, using ReadUint24 and then Reads the bytes
333 func (e *StickyLittleEndianReader) ReadBytes24() []byte {
334 return e.ReadBytes(int(e.ReadUint24()))
335 }
336
337 // ReadBytes32 Reads the length of the Bytes, using ReadUint32 and then Reads the bytes
338 func (e *StickyLittleEndianReader) ReadBytes32() []byte {
339 return e.ReadBytes(int(e.ReadUint32()))
340 }
341
342 // ReadBytes40 Reads the length of the Bytes, using ReadUint40 and then Reads the bytes
343 func (e *StickyLittleEndianReader) ReadBytes40() []byte {
344 return e.ReadBytes(int(e.ReadUint40()))
345 }
346
347 // ReadBytes48 Reads the length of the Bytes, using ReadUint48 and then Reads the bytes
348 func (e *StickyLittleEndianReader) ReadBytes48() []byte {
349 return e.ReadBytes(int(e.ReadUint48()))
350 }
351
352 // ReadBytes56 Reads the length of the Bytes, using ReadUint56 and then Reads the bytes
353 func (e *StickyLittleEndianReader) ReadBytes56() []byte {
354 return e.ReadBytes(int(e.ReadUint56()))
355 }
356
357 // ReadBytes64 Reads the length of the Bytes, using ReadUint64 and then Reads the bytes
358 func (e *StickyLittleEndianReader) ReadBytes64() []byte {
359 return e.ReadBytes(int(e.ReadUint64()))
360 }
361
362 // ReadString Reads a string
363 func (e *StickyLittleEndianReader) ReadString(size int) string {
364 if e.Err != nil {
365 return ""
366 }
367 buf := make([]byte, size)
368 var n int
369 n, e.Err = io.ReadFull(e.Reader, buf)
370 e.Count += int64(n)
371 return string(buf[:n])
372 }
373
374 // ReadStringX Reads the length of the String, using ReadUintX and then Reads the bytes
375 func (e *StickyLittleEndianReader) ReadStringX() string {
376 return e.ReadString(int(e.ReadUintX()))
377 }
378
379 // ReadString8 Reads the length of the String, using ReadUint8 and then Reads the bytes
380 func (e *StickyLittleEndianReader) ReadString8() string {
381 return e.ReadString(int(e.ReadUint8()))
382 }
383
384 // ReadString16 Reads the length of the String, using ReadUint16 and then Reads the bytes
385 func (e *StickyLittleEndianReader) ReadString16() string {
386 return e.ReadString(int(e.ReadUint16()))
387 }
388
389 // ReadString24 Reads the length of the String, using ReadUint24 and then Reads the bytes
390 func (e *StickyLittleEndianReader) ReadString24() string {
391 return e.ReadString(int(e.ReadUint24()))
392 }
393
394 // ReadString32 Reads the length of the String, using ReadUint32 and then Reads the bytes
395 func (e *StickyLittleEndianReader) ReadString32() string {
396 return e.ReadString(int(e.ReadUint32()))
397 }
398
399 // ReadString40 Reads the length of the String, using ReadUint40 and then Reads the bytes
400 func (e *StickyLittleEndianReader) ReadString40() string {
401 return e.ReadString(int(e.ReadUint40()))
402 }
403
404 // ReadString48 Reads the length of the String, using ReadUint48 and then Reads the bytes
405 func (e *StickyLittleEndianReader) ReadString48() string {
406 return e.ReadString(int(e.ReadUint48()))
407 }
408
409 // ReadString56 Reads the length of the String, using ReadUint56 and then Reads the bytes
410 func (e *StickyLittleEndianReader) ReadString56() string {
411 return e.ReadString(int(e.ReadUint56()))
412 }
413
414 // ReadString64 Reads the length of the String, using ReadUint64 and then Reads the bytes
415 func (e *StickyLittleEndianReader) ReadString64() string {
416 return e.ReadString(int(e.ReadUint64()))
417 }
418
419 // ReadString0 Reads the bytes of the string until a 0 byte is read
420 func (e *StickyLittleEndianReader) ReadString0() string {
421 var d []byte
422 for {
423 p := e.ReadUint8()
424 if p == 0 {
425 break
426 }
427 d = append(d, p)
428 }
429 return string(d)
430 }
431