serialization.dart 7.83 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:math' as math;
6 7 8 9 10
import 'dart:typed_data';

/// Write-only buffer for incrementally building a [ByteData] instance.
///
/// A WriteBuffer instance can be used only once. Attempts to reuse will result
11
/// in [StateError]s being thrown.
12
///
13
/// The byte order used is [Endian.host] throughout.
14
class WriteBuffer {
15
  /// Creates an interface for incrementally building a [ByteData] instance.
16 17 18 19 20
  /// [startCapacity] determines the start size of the [WriteBuffer] in bytes.
  /// The closer that value is to the real size used, the better the
  /// performance.
  factory WriteBuffer({int startCapacity = 8}) {
    assert(startCapacity > 0);
21 22
    final ByteData eightBytes = ByteData(8);
    final Uint8List eightBytesAsList = eightBytes.buffer.asUint8List();
23
    return WriteBuffer._(Uint8List(startCapacity), eightBytes, eightBytesAsList);
24 25
  }

26 27
  WriteBuffer._(this._buffer, this._eightBytes, this._eightBytesAsList);

28 29
  Uint8List _buffer;
  int _currentSize = 0;
30
  bool _isDone = false;
31
  final ByteData _eightBytes;
32 33 34
  final Uint8List _eightBytesAsList;
  static final Uint8List _zeroBuffer = Uint8List(8);

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  void _add(int byte) {
    if (_currentSize == _buffer.length) {
      _resize();
    }
    _buffer[_currentSize] = byte;
    _currentSize += 1;
  }

  void _append(Uint8List other) {
    final int newSize = _currentSize + other.length;
    if (newSize >= _buffer.length) {
      _resize(newSize);
    }
    _buffer.setRange(_currentSize, newSize, other);
    _currentSize += other.length;
  }

52
  void _addAll(Uint8List data, [int start = 0, int? end]) {
53 54 55 56
    final int newEnd = end ?? _eightBytesAsList.length;
    final int newSize = _currentSize + (newEnd - start);
    if (newSize >= _buffer.length) {
      _resize(newSize);
57
    }
58 59 60 61 62 63 64 65 66 67
    _buffer.setRange(_currentSize, newSize, data);
    _currentSize = newSize;
  }

  void _resize([int? requiredLength]) {
    final int doubleLength = _buffer.length * 2;
    final int newLength = math.max(requiredLength ?? 0, doubleLength);
    final Uint8List newBuffer = Uint8List(newLength);
    newBuffer.setRange(0, _buffer.length, _buffer);
    _buffer = newBuffer;
68
  }
69

70
  /// Write a Uint8 into the buffer.
71
  void putUint8(int byte) {
72
    assert(!_isDone);
73
    _add(byte);
74 75
  }

76
  /// Write a Uint16 into the buffer.
77
  void putUint16(int value, {Endian? endian}) {
78
    assert(!_isDone);
79
    _eightBytes.setUint16(0, value, endian ?? Endian.host);
80
    _addAll(_eightBytesAsList, 0, 2);
81 82
  }

83
  /// Write a Uint32 into the buffer.
84
  void putUint32(int value, {Endian? endian}) {
85
    assert(!_isDone);
86
    _eightBytes.setUint32(0, value, endian ?? Endian.host);
87
    _addAll(_eightBytesAsList, 0, 4);
88 89
  }

90
  /// Write an Int32 into the buffer.
91
  void putInt32(int value, {Endian? endian}) {
92
    assert(!_isDone);
93
    _eightBytes.setInt32(0, value, endian ?? Endian.host);
94
    _addAll(_eightBytesAsList, 0, 4);
95 96
  }

97
  /// Write an Int64 into the buffer.
98
  void putInt64(int value, {Endian? endian}) {
99
    assert(!_isDone);
100
    _eightBytes.setInt64(0, value, endian ?? Endian.host);
101
    _addAll(_eightBytesAsList, 0, 8);
102 103
  }

104
  /// Write an Float64 into the buffer.
105
  void putFloat64(double value, {Endian? endian}) {
106
    assert(!_isDone);
107
    _alignTo(8);
108
    _eightBytes.setFloat64(0, value, endian ?? Endian.host);
109
    _addAll(_eightBytesAsList);
110 111
  }

112
  /// Write all the values from a [Uint8List] into the buffer.
113
  void putUint8List(Uint8List list) {
114
    assert(!_isDone);
115
    _append(list);
116 117
  }

118
  /// Write all the values from an [Int32List] into the buffer.
119
  void putInt32List(Int32List list) {
120
    assert(!_isDone);
121
    _alignTo(4);
122
    _append(list.buffer.asUint8List(list.offsetInBytes, 4 * list.length));
123 124
  }

125
  /// Write all the values from an [Int64List] into the buffer.
126
  void putInt64List(Int64List list) {
127
    assert(!_isDone);
128
    _alignTo(8);
129
    _append(list.buffer.asUint8List(list.offsetInBytes, 8 * list.length));
130 131
  }

132 133 134 135
  /// Write all the values from a [Float32List] into the buffer.
  void putFloat32List(Float32List list) {
    assert(!_isDone);
    _alignTo(4);
136
    _append(list.buffer.asUint8List(list.offsetInBytes, 4 * list.length));
137 138
  }

139
  /// Write all the values from a [Float64List] into the buffer.
140
  void putFloat64List(Float64List list) {
141
    assert(!_isDone);
142
    _alignTo(8);
143
    _append(list.buffer.asUint8List(list.offsetInBytes, 8 * list.length));
144 145 146
  }

  void _alignTo(int alignment) {
147
    assert(!_isDone);
148
    final int mod = _currentSize % alignment;
149
    if (mod != 0) {
150
      _addAll(_zeroBuffer, 0, alignment - mod);
151 152 153
    }
  }

154
  /// Finalize and return the written [ByteData].
155
  ByteData done() {
156 157 158
    if (_isDone) {
      throw StateError('done() must not be called more than once on the same $runtimeType.');
    }
159 160
    final ByteData result = _buffer.buffer.asByteData(0, _currentSize);
    _buffer = Uint8List(0);
161
    _isDone = true;
162 163 164 165 166 167
    return result;
  }
}

/// Read-only buffer for reading sequentially from a [ByteData] instance.
///
168
/// The byte order used is [Endian.host] throughout.
169 170
class ReadBuffer {
  /// Creates a [ReadBuffer] for reading from the specified [data].
171 172
  ReadBuffer(this.data)
    : assert(data != null);
173

174 175 176 177 178 179 180 181 182 183
  /// The underlying data being read.
  final ByteData data;

  /// The position to read next.
  int _position = 0;

  /// Whether the buffer has data remaining to read.
  bool get hasRemaining => _position < data.lengthInBytes;

  /// Reads a Uint8 from the buffer.
184
  int getUint8() {
185
    return data.getUint8(_position++);
186 187
  }

188
  /// Reads a Uint16 from the buffer.
189
  int getUint16({Endian? endian}) {
190
    final int value = data.getUint16(_position, endian ?? Endian.host);
191
    _position += 2;
192 193 194
    return value;
  }

195
  /// Reads a Uint32 from the buffer.
196
  int getUint32({Endian? endian}) {
197
    final int value = data.getUint32(_position, endian ?? Endian.host);
198
    _position += 4;
199 200 201
    return value;
  }

202
  /// Reads an Int32 from the buffer.
203
  int getInt32({Endian? endian}) {
204
    final int value = data.getInt32(_position, endian ?? Endian.host);
205
    _position += 4;
206 207 208
    return value;
  }

209
  /// Reads an Int64 from the buffer.
210
  int getInt64({Endian? endian}) {
211
    final int value = data.getInt64(_position, endian ?? Endian.host);
212
    _position += 8;
213 214 215
    return value;
  }

216
  /// Reads a Float64 from the buffer.
217
  double getFloat64({Endian? endian}) {
218
    _alignTo(8);
219
    final double value = data.getFloat64(_position, endian ?? Endian.host);
220
    _position += 8;
221 222 223
    return value;
  }

224
  /// Reads the given number of Uint8s from the buffer.
225
  Uint8List getUint8List(int length) {
226 227
    final Uint8List list = data.buffer.asUint8List(data.offsetInBytes + _position, length);
    _position += length;
228 229 230
    return list;
  }

231
  /// Reads the given number of Int32s from the buffer.
232 233
  Int32List getInt32List(int length) {
    _alignTo(4);
234 235
    final Int32List list = data.buffer.asInt32List(data.offsetInBytes + _position, length);
    _position += 4 * length;
236 237 238
    return list;
  }

239
  /// Reads the given number of Int64s from the buffer.
240 241
  Int64List getInt64List(int length) {
    _alignTo(8);
242 243
    final Int64List list = data.buffer.asInt64List(data.offsetInBytes + _position, length);
    _position += 8 * length;
244 245 246
    return list;
  }

247 248 249 250 251 252 253 254
  /// Reads the given number of Float32s from the buffer
  Float32List getFloat32List(int length) {
    _alignTo(4);
    final Float32List list = data.buffer.asFloat32List(data.offsetInBytes + _position, length);
    _position += 4 * length;
    return list;
  }

255
  /// Reads the given number of Float64s from the buffer.
256 257
  Float64List getFloat64List(int length) {
    _alignTo(8);
258 259
    final Float64List list = data.buffer.asFloat64List(data.offsetInBytes + _position, length);
    _position += 8 * length;
260 261 262 263
    return list;
  }

  void _alignTo(int alignment) {
264
    final int mod = _position % alignment;
265
    if (mod != 0) {
266
      _position += alignment - mod;
267
    }
268 269
  }
}