|
| 1 | +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +part of dart._internal; |
| 6 | + |
| 7 | +/// Builds a list of bytes, allowing bytes and lists of bytes to be added at the |
| 8 | +/// end. |
| 9 | +/// |
| 10 | +/// Used to efficiently collect bytes and lists of bytes. |
| 11 | +abstract class BytesBuilder { |
| 12 | + /// Construct a new empty [BytesBuilder]. |
| 13 | + /// |
| 14 | + /// If [copy] is true (the default), the created builder is a *copying* |
| 15 | + /// builder. A copying builder maintains its own internal buffer and copies |
| 16 | + /// the bytes added to it eagerly. |
| 17 | + /// |
| 18 | + /// If [copy] set to false, the created builder assumes that lists added |
| 19 | + /// to it will not change. |
| 20 | + /// Any [Uint8List] added using [add] is kept until |
| 21 | + /// [toBytes] or [takeBytes] is called, |
| 22 | + /// and only then are their contents copied. |
| 23 | + /// A non-[Uint8List] may be copied eagerly. |
| 24 | + /// If only a single [Uint8List] is added to the builder, |
| 25 | + /// that list is returned by [toBytes] or [takeBytes] directly, without any copying. |
| 26 | + /// A list added to a non-copying builder *should not* change its content |
| 27 | + /// after being added, and it *must not* change its length after being added. |
| 28 | + /// (Normal [Uint8List]s are fixed length lists, but growing lists implementing |
| 29 | + /// [Uint8List] exist.) |
| 30 | + factory BytesBuilder({bool copy = true}) => |
| 31 | + copy ? _CopyingBytesBuilder() : _BytesBuilder(); |
| 32 | + |
| 33 | + /// Appends [bytes] to the current contents of this builder. |
| 34 | + /// |
| 35 | + /// Each value of [bytes] will be truncated |
| 36 | + /// to an 8-bit value in the range 0 .. 255. |
| 37 | + void add(List<int> bytes); |
| 38 | + |
| 39 | + /// Appends [byte] to the current contents of this builder. |
| 40 | + /// |
| 41 | + /// The [byte] will be truncated to an 8-bit value in the range 0 .. 255. |
| 42 | + void addByte(int byte); |
| 43 | + |
| 44 | + /// Returns the bytes currently contained in this builder and clears it. |
| 45 | + /// |
| 46 | + /// The returned list may be a view of a larger buffer. |
| 47 | + Uint8List takeBytes(); |
| 48 | + |
| 49 | + /// Returns a copy of the current byte contents of this builder. |
| 50 | + /// |
| 51 | + /// Leaves the contents of this builder intact. |
| 52 | + Uint8List toBytes(); |
| 53 | + |
| 54 | + /// The number of bytes in this builder. |
| 55 | + int get length; |
| 56 | + |
| 57 | + /// Whether the buffer is empty. |
| 58 | + bool get isEmpty; |
| 59 | + |
| 60 | + /// Whether the buffer is non-empty. |
| 61 | + bool get isNotEmpty; |
| 62 | + |
| 63 | + /// Clears the contents of this builder. |
| 64 | + /// |
| 65 | + /// The current contents are discarded and this builder becomes empty. |
| 66 | + void clear(); |
| 67 | +} |
| 68 | + |
| 69 | +/// A [BytesBuilder] which appends bytes to a growing internal buffer. |
| 70 | +class _CopyingBytesBuilder implements BytesBuilder { |
| 71 | + /// Initial size of internal buffer. |
| 72 | + static const int _initSize = 1024; |
| 73 | + |
| 74 | + /// Reusable empty [Uint8List]. |
| 75 | + /// |
| 76 | + /// Safe for reuse because a fixed-length empty list is immutable. |
| 77 | + static final _emptyList = Uint8List(0); |
| 78 | + |
| 79 | + /// Current count of bytes written to buffer. |
| 80 | + int _length = 0; |
| 81 | + |
| 82 | + /// Internal buffer accumulating bytes. |
| 83 | + /// |
| 84 | + /// Will grow as necessary |
| 85 | + Uint8List _buffer; |
| 86 | + |
| 87 | + _CopyingBytesBuilder() : _buffer = _emptyList; |
| 88 | + |
| 89 | + void add(List<int> bytes) { |
| 90 | + int byteCount = bytes.length; |
| 91 | + if (byteCount == 0) return; |
| 92 | + int required = _length + byteCount; |
| 93 | + if (_buffer.length < required) { |
| 94 | + _grow(required); |
| 95 | + } |
| 96 | + assert(_buffer.length >= required); |
| 97 | + if (bytes is Uint8List) { |
| 98 | + _buffer.setRange(_length, required, bytes); |
| 99 | + } else { |
| 100 | + for (int i = 0; i < byteCount; i++) { |
| 101 | + _buffer[_length + i] = bytes[i]; |
| 102 | + } |
| 103 | + } |
| 104 | + _length = required; |
| 105 | + } |
| 106 | + |
| 107 | + void addByte(int byte) { |
| 108 | + if (_buffer.length == _length) { |
| 109 | + // The grow algorithm always at least doubles. |
| 110 | + // If we added one to _length it would quadruple unnecessarily. |
| 111 | + _grow(_length); |
| 112 | + } |
| 113 | + assert(_buffer.length > _length); |
| 114 | + _buffer[_length] = byte; |
| 115 | + _length++; |
| 116 | + } |
| 117 | + |
| 118 | + void _grow(int required) { |
| 119 | + // We will create a list in the range of 2-4 times larger than |
| 120 | + // required. |
| 121 | + int newSize = required * 2; |
| 122 | + if (newSize < _initSize) { |
| 123 | + newSize = _initSize; |
| 124 | + } else { |
| 125 | + newSize = _pow2roundup(newSize); |
| 126 | + } |
| 127 | + var newBuffer = Uint8List(newSize); |
| 128 | + newBuffer.setRange(0, _buffer.length, _buffer); |
| 129 | + _buffer = newBuffer; |
| 130 | + } |
| 131 | + |
| 132 | + Uint8List takeBytes() { |
| 133 | + if (_length == 0) return _emptyList; |
| 134 | + var buffer = Uint8List.view(_buffer.buffer, _buffer.offsetInBytes, _length); |
| 135 | + _clear(); |
| 136 | + return buffer; |
| 137 | + } |
| 138 | + |
| 139 | + Uint8List toBytes() { |
| 140 | + if (_length == 0) return _emptyList; |
| 141 | + return Uint8List.fromList( |
| 142 | + Uint8List.view(_buffer.buffer, _buffer.offsetInBytes, _length)); |
| 143 | + } |
| 144 | + |
| 145 | + int get length => _length; |
| 146 | + |
| 147 | + bool get isEmpty => _length == 0; |
| 148 | + |
| 149 | + bool get isNotEmpty => _length != 0; |
| 150 | + |
| 151 | + void clear() { |
| 152 | + _clear(); |
| 153 | + } |
| 154 | + |
| 155 | + void _clear() { |
| 156 | + _length = 0; |
| 157 | + _buffer = _emptyList; |
| 158 | + } |
| 159 | + |
| 160 | + /// Rounds numbers <= 2^32 up to the nearest power of 2. |
| 161 | + static int _pow2roundup(int x) { |
| 162 | + assert(x > 0); |
| 163 | + --x; |
| 164 | + x |= x >> 1; |
| 165 | + x |= x >> 2; |
| 166 | + x |= x >> 4; |
| 167 | + x |= x >> 8; |
| 168 | + x |= x >> 16; |
| 169 | + return x + 1; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +/// A non-copying [BytesBuilder]. |
| 174 | +/// |
| 175 | +/// Accumulates lists of integers and lazily builds |
| 176 | +/// a collected list with all the bytes when requested. |
| 177 | +class _BytesBuilder implements BytesBuilder { |
| 178 | + int _length = 0; |
| 179 | + final List<Uint8List> _chunks = []; |
| 180 | + |
| 181 | + void add(List<int> bytes) { |
| 182 | + Uint8List typedBytes; |
| 183 | + if (bytes is Uint8List) { |
| 184 | + typedBytes = bytes; |
| 185 | + } else { |
| 186 | + typedBytes = Uint8List.fromList(bytes); |
| 187 | + } |
| 188 | + _chunks.add(typedBytes); |
| 189 | + _length += typedBytes.length; |
| 190 | + } |
| 191 | + |
| 192 | + void addByte(int byte) { |
| 193 | + // TODO(lrn): Optimize repeated `addByte` calls. |
| 194 | + _chunks.add(Uint8List(1)..[0] = byte); |
| 195 | + _length++; |
| 196 | + } |
| 197 | + |
| 198 | + Uint8List takeBytes() { |
| 199 | + if (_length == 0) return _CopyingBytesBuilder._emptyList; |
| 200 | + if (_chunks.length == 1) { |
| 201 | + var buffer = _chunks[0]; |
| 202 | + _clear(); |
| 203 | + return buffer; |
| 204 | + } |
| 205 | + var buffer = Uint8List(_length); |
| 206 | + int offset = 0; |
| 207 | + for (var chunk in _chunks) { |
| 208 | + buffer.setRange(offset, offset + chunk.length, chunk); |
| 209 | + offset += chunk.length; |
| 210 | + } |
| 211 | + _clear(); |
| 212 | + return buffer; |
| 213 | + } |
| 214 | + |
| 215 | + Uint8List toBytes() { |
| 216 | + if (_length == 0) return _CopyingBytesBuilder._emptyList; |
| 217 | + var buffer = Uint8List(_length); |
| 218 | + int offset = 0; |
| 219 | + for (var chunk in _chunks) { |
| 220 | + buffer.setRange(offset, offset + chunk.length, chunk); |
| 221 | + offset += chunk.length; |
| 222 | + } |
| 223 | + return buffer; |
| 224 | + } |
| 225 | + |
| 226 | + int get length => _length; |
| 227 | + |
| 228 | + bool get isEmpty => _length == 0; |
| 229 | + |
| 230 | + bool get isNotEmpty => _length != 0; |
| 231 | + |
| 232 | + void clear() { |
| 233 | + _clear(); |
| 234 | + } |
| 235 | + |
| 236 | + void _clear() { |
| 237 | + _length = 0; |
| 238 | + _chunks.clear(); |
| 239 | + } |
| 240 | +} |
0 commit comments