@@ -13,6 +13,7 @@ import 'package:typed_data/typed_buffers.dart' show Uint8Buffer;
13
13
///
14
14
/// The byte order used is [Endianness.HOST_ENDIAN] throughout.
15
15
class WriteBuffer {
16
+ /// Creates an interface for incrementally building a [ByteData] instance.
16
17
WriteBuffer () {
17
18
_buffer = new Uint8Buffer ();
18
19
_eightBytes = new ByteData (8 );
@@ -23,49 +24,59 @@ class WriteBuffer {
23
24
ByteData _eightBytes;
24
25
Uint8List _eightBytesAsList;
25
26
27
+ /// Write a Uint8 into the buffer.
26
28
void putUint8 (int byte) {
27
29
_buffer.add (byte);
28
30
}
29
31
32
+ /// Write a Uint16 into the buffer.
30
33
void putUint16 (int value) {
31
34
_eightBytes.setUint16 (0 , value, Endianness .HOST_ENDIAN );
32
35
_buffer.addAll (_eightBytesAsList, 0 , 2 );
33
36
}
34
37
38
+ /// Write a Uint32 into the buffer.
35
39
void putUint32 (int value) {
36
40
_eightBytes.setUint32 (0 , value, Endianness .HOST_ENDIAN );
37
41
_buffer.addAll (_eightBytesAsList, 0 , 4 );
38
42
}
39
43
44
+ /// Write an Int32 into the buffer.
40
45
void putInt32 (int value) {
41
46
_eightBytes.setInt32 (0 , value, Endianness .HOST_ENDIAN );
42
47
_buffer.addAll (_eightBytesAsList, 0 , 4 );
43
48
}
44
49
50
+ /// Write an Int64 into the buffer.
45
51
void putInt64 (int value) {
46
52
_eightBytes.setInt64 (0 , value, Endianness .HOST_ENDIAN );
47
53
_buffer.addAll (_eightBytesAsList, 0 , 8 );
48
54
}
49
55
56
+ /// Write an Float64 into the buffer.
50
57
void putFloat64 (double value) {
51
58
_eightBytes.setFloat64 (0 , value, Endianness .HOST_ENDIAN );
52
59
_buffer.addAll (_eightBytesAsList);
53
60
}
54
61
62
+ /// Write all the values from a [Uint8List] into the buffer.
55
63
void putUint8List (Uint8List list) {
56
64
_buffer.addAll (list);
57
65
}
58
66
67
+ /// Write all the values from a [Int32List] into the buffer.
59
68
void putInt32List (Int32List list) {
60
69
_alignTo (4 );
61
70
_buffer.addAll (list.buffer.asUint8List (list.offsetInBytes, 4 * list.length));
62
71
}
63
72
73
+ /// Write all the values from an [Int64List] into the buffer.
64
74
void putInt64List (Int64List list) {
65
75
_alignTo (8 );
66
76
_buffer.addAll (list.buffer.asUint8List (list.offsetInBytes, 8 * list.length));
67
77
}
68
78
79
+ /// Write all the values from a [Float64List] into the buffer.
69
80
void putFloat64List (Float64List list) {
70
81
_alignTo (8 );
71
82
_buffer.addAll (list.buffer.asUint8List (list.offsetInBytes, 8 * list.length));
@@ -79,6 +90,7 @@ class WriteBuffer {
79
90
}
80
91
}
81
92
93
+ /// Finalize and return the written [ByteData] .
82
94
ByteData done () {
83
95
final ByteData result = _buffer.buffer.asByteData (0 , _buffer.lengthInBytes);
84
96
_buffer = null ;
@@ -90,80 +102,94 @@ class WriteBuffer {
90
102
///
91
103
/// The byte order used is [Endianness.HOST_ENDIAN] throughout.
92
104
class ReadBuffer {
93
- final ByteData data;
94
- int position = 0 ;
95
-
96
105
/// Creates a [ReadBuffer] for reading from the specified [data] .
97
106
ReadBuffer (this .data) {
98
107
assert (data != null );
99
108
}
100
109
110
+ /// The underlying data being read.
111
+ final ByteData data;
112
+
113
+ /// The position to read next.
114
+ int _position = 0 ;
115
+
116
+ /// Whether the buffer has data remaining to read.
117
+ bool get hasRemaining => _position < data.lengthInBytes;
118
+
119
+ /// Reads a Uint8 from the buffer.
101
120
int getUint8 () {
102
- return data.getUint8 (position ++ );
121
+ return data.getUint8 (_position ++ );
103
122
}
104
123
124
+ /// Reads a Uint16 from the buffer.
105
125
int getUint16 () {
106
- final int value = data.getUint16 (position , Endianness .HOST_ENDIAN );
107
- position += 2 ;
126
+ final int value = data.getUint16 (_position , Endianness .HOST_ENDIAN );
127
+ _position += 2 ;
108
128
return value;
109
129
}
110
130
131
+ /// Reads a Uint32 from the buffer.
111
132
int getUint32 () {
112
- final int value = data.getUint32 (position , Endianness .HOST_ENDIAN );
113
- position += 4 ;
133
+ final int value = data.getUint32 (_position , Endianness .HOST_ENDIAN );
134
+ _position += 4 ;
114
135
return value;
115
136
}
116
137
138
+ /// Reads an Int32 from the buffer.
117
139
int getInt32 () {
118
- final int value = data.getInt32 (position , Endianness .HOST_ENDIAN );
119
- position += 4 ;
140
+ final int value = data.getInt32 (_position , Endianness .HOST_ENDIAN );
141
+ _position += 4 ;
120
142
return value;
121
143
}
122
144
145
+ /// Reads an Int64 from the buffer.
123
146
int getInt64 () {
124
- final int value = data.getInt64 (position , Endianness .HOST_ENDIAN );
125
- position += 8 ;
147
+ final int value = data.getInt64 (_position , Endianness .HOST_ENDIAN );
148
+ _position += 8 ;
126
149
return value;
127
150
}
128
151
152
+ /// Reads a Float64 from the buffer.
129
153
double getFloat64 () {
130
- final double value = data.getFloat64 (position , Endianness .HOST_ENDIAN );
131
- position += 8 ;
154
+ final double value = data.getFloat64 (_position , Endianness .HOST_ENDIAN );
155
+ _position += 8 ;
132
156
return value;
133
157
}
134
158
159
+ /// Reads the given number of Uint8s from the buffer.
135
160
Uint8List getUint8List (int length) {
136
- final Uint8List list = data.buffer.asUint8List (data.offsetInBytes + position , length);
137
- position += length;
161
+ final Uint8List list = data.buffer.asUint8List (data.offsetInBytes + _position , length);
162
+ _position += length;
138
163
return list;
139
164
}
140
165
166
+ /// Reads the given number of Int32s from the buffer.
141
167
Int32List getInt32List (int length) {
142
168
_alignTo (4 );
143
- final Int32List list = data.buffer.asInt32List (data.offsetInBytes + position , length);
144
- position += 4 * length;
169
+ final Int32List list = data.buffer.asInt32List (data.offsetInBytes + _position , length);
170
+ _position += 4 * length;
145
171
return list;
146
172
}
147
173
174
+ /// Reads the given number of Int64s from the buffer.
148
175
Int64List getInt64List (int length) {
149
176
_alignTo (8 );
150
- final Int64List list = data.buffer.asInt64List (data.offsetInBytes + position , length);
151
- position += 8 * length;
177
+ final Int64List list = data.buffer.asInt64List (data.offsetInBytes + _position , length);
178
+ _position += 8 * length;
152
179
return list;
153
180
}
154
181
182
+ /// Reads the given number of Float64s from the buffer.
155
183
Float64List getFloat64List (int length) {
156
184
_alignTo (8 );
157
- final Float64List list = data.buffer.asFloat64List (data.offsetInBytes + position , length);
158
- position += 8 * length;
185
+ final Float64List list = data.buffer.asFloat64List (data.offsetInBytes + _position , length);
186
+ _position += 8 * length;
159
187
return list;
160
188
}
161
189
162
190
void _alignTo (int alignment) {
163
- final int mod = position % alignment;
191
+ final int mod = _position % alignment;
164
192
if (mod != 0 )
165
- position += alignment - mod;
193
+ _position += alignment - mod;
166
194
}
167
-
168
- bool get hasRemaining => position < data.lengthInBytes;
169
195
}
0 commit comments