Line data Source code
1 : /*
2 : * Package : Cbor
3 : * Author : S. Hamblett <steve.hamblett@linux.com>
4 : * Date : 12/12/2016
5 : * Copyright : S.Hamblett
6 : */
7 :
8 : part of cbor;
9 :
10 : /// The Input class provides data access primitives to the underlying
11 : /// UTF-8 data buffer supplied.
12 : class Input {
13 : typed.Uint8Buffer _data;
14 : int _offset;
15 :
16 3 : Input(typed.Uint8Buffer data, int size) {
17 3 : this._data = data;
18 3 : this._offset = 0;
19 : }
20 :
21 : /// Does the input have the numbe of bytes.
22 : bool hasBytes(int count) {
23 15 : return _data.lengthInBytes - _offset >= count;
24 : }
25 :
26 : /// Get a single byte.
27 : int getByte() {
28 12 : return _data[_offset++];
29 : }
30 :
31 : /// Get a short, 16 bits.
32 : int getShort() {
33 27 : final int value = (_data[_offset] << 8 | _data[_offset + 1]);
34 6 : _offset += 2;
35 : return value;
36 : }
37 :
38 : /// Get an int, 32 bits.
39 : int getInt() {
40 15 : final int value = (_data[_offset] << 24) |
41 18 : (_data[_offset + 1] << 16) |
42 18 : (_data[_offset + 2] << 8) |
43 12 : (_data[_offset + 3]);
44 6 : _offset += 4;
45 : return value;
46 : }
47 :
48 : /// Get a long, 64 bits.
49 : int getLong() {
50 5 : final int value = (_data[_offset] << 56) |
51 6 : (_data[_offset + 1] << 48) |
52 6 : (_data[_offset + 2] << 40) |
53 6 : (_data[_offset + 3] << 32) |
54 6 : (_data[_offset + 4] << 24) |
55 6 : (_data[_offset + 5] << 16) |
56 6 : (_data[_offset + 6] << 8) |
57 4 : (_data[_offset + 7]);
58 2 : _offset += 8;
59 : return value;
60 : }
61 :
62 : /// Get the number of bytes specified.
63 : typed.Uint8Buffer getBytes(int count) {
64 15 : final List<int> tmp = _data.sublist(_offset, _offset + count);
65 3 : final typed.Uint8Buffer buff = new typed.Uint8Buffer();
66 3 : buff.addAll(tmp);
67 6 : _offset += count;
68 : return buff;
69 : }
70 :
71 : /// Get a half-precision float from an integer value.
72 : double getHalfFloat(int val) {
73 : // Check for known patterns/anomalies and return
74 : // the correct values otherwise use the algorithm below.
75 :
76 : // Prefilter
77 3 : if (val == 1) return 5.960464477539063e-8;
78 3 : final double ret = getHalfPrecisionDouble(val);
79 : // Post filter
80 3 : if (ret == 65536.0) return double.INFINITY;
81 3 : if (ret == -65536.0) return -(double.INFINITY);
82 3 : if (ret == 98304.0) return double.NAN;
83 : return ret;
84 : }
85 :
86 : /// Get a single-precision float from a buffer value.
87 : double getSingleFloat(typed.Uint8Buffer buff) {
88 6 : final ByteData bdata = new ByteData.view(buff.buffer);
89 3 : return bdata.getFloat32(0);
90 : }
91 :
92 : /// Get a double-precision float from a buffer value.
93 : double getDoubleFloat(typed.Uint8Buffer buff) {
94 6 : final ByteData bdata = new ByteData.view(buff.buffer);
95 3 : return bdata.getFloat64(0);
96 : }
97 :
98 : /// Reset the offset.
99 : void reset() {
100 2 : _offset = 0;
101 : }
102 : }
|