| OLD | NEW |
| (Empty) |
| 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.io; | |
| 6 | |
| 7 /** | |
| 8 * Builds a list of bytes, allowing bytes and lists of bytes to be added at the | |
| 9 * end. | |
| 10 * | |
| 11 * Used to efficiently collect bytes and lists of bytes. | |
| 12 */ | |
| 13 abstract class BytesBuilder { | |
| 14 /** | |
| 15 * Construct a new empty [BytesBuilder]. | |
| 16 * | |
| 17 * If [copy] is true, the data is always copied when added to the list. If | |
| 18 * it [copy] is false, the data is only copied if needed. That means that if | |
| 19 * the lists are changed after added to the [BytesBuilder], it may effect the | |
| 20 * output. Default is `true`. | |
| 21 */ | |
| 22 factory BytesBuilder({bool copy: true}) { | |
| 23 if (copy) { | |
| 24 return new _CopyingBytesBuilder(); | |
| 25 } else { | |
| 26 return new _BytesBuilder(); | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 /** | |
| 31 * Appends [bytes] to the current contents of the builder. | |
| 32 * | |
| 33 * Each value of [bytes] will be bit-representation truncated to the range | |
| 34 * 0 .. 255. | |
| 35 */ | |
| 36 void add(List<int> bytes); | |
| 37 | |
| 38 /** | |
| 39 * Append [byte] to the current contents of the builder. | |
| 40 * | |
| 41 * The [byte] will be bit-representation truncated to the range 0 .. 255. | |
| 42 */ | |
| 43 void addByte(int byte); | |
| 44 | |
| 45 /** | |
| 46 * Returns the contents of `this` and clears `this`. | |
| 47 * | |
| 48 * The list returned is a view of the internal buffer, limited to the | |
| 49 * [length]. | |
| 50 */ | |
| 51 List<int> takeBytes(); | |
| 52 | |
| 53 /** | |
| 54 * Returns a copy of the current contents of the builder. | |
| 55 * | |
| 56 * Leaves the contents of the builder intact. | |
| 57 */ | |
| 58 List<int> toBytes(); | |
| 59 | |
| 60 /** | |
| 61 * The number of bytes in the builder. | |
| 62 */ | |
| 63 int get length; | |
| 64 | |
| 65 /** | |
| 66 * Returns `true` if the buffer is empty. | |
| 67 */ | |
| 68 bool get isEmpty; | |
| 69 | |
| 70 /** | |
| 71 * Returns `true` if the buffer is not empty. | |
| 72 */ | |
| 73 bool get isNotEmpty; | |
| 74 | |
| 75 /** | |
| 76 * Clear the contents of the builder. | |
| 77 */ | |
| 78 void clear(); | |
| 79 } | |
| 80 | |
| 81 | |
| 82 class _CopyingBytesBuilder implements BytesBuilder { | |
| 83 // Start with 1024 bytes. | |
| 84 static const int _INIT_SIZE = 1024; | |
| 85 | |
| 86 int _length = 0; | |
| 87 Uint8List _buffer; | |
| 88 | |
| 89 void add(List<int> bytes) { | |
| 90 int bytesLength = bytes.length; | |
| 91 if (bytesLength == 0) return; | |
| 92 int required = _length + bytesLength; | |
| 93 if (_buffer == null) { | |
| 94 int size = _pow2roundup(required); | |
| 95 size = max(size, _INIT_SIZE); | |
| 96 _buffer = new Uint8List(size); | |
| 97 } else if (_buffer.length < required) { | |
| 98 // We will create a list in the range of 2-4 times larger than | |
| 99 // required. | |
| 100 int size = _pow2roundup(required) * 2; | |
| 101 var newBuffer = new Uint8List(size); | |
| 102 newBuffer.setRange(0, _buffer.length, _buffer); | |
| 103 _buffer = newBuffer; | |
| 104 } | |
| 105 assert(_buffer.length >= required); | |
| 106 if (bytes is Uint8List) { | |
| 107 _buffer.setRange(_length, required, bytes); | |
| 108 } else { | |
| 109 for (int i = 0; i < bytesLength; i++) { | |
| 110 _buffer[_length + i] = bytes[i]; | |
| 111 } | |
| 112 } | |
| 113 _length = required; | |
| 114 } | |
| 115 | |
| 116 void addByte(int byte) { add([byte]); } | |
| 117 | |
| 118 List<int> takeBytes() { | |
| 119 if (_buffer == null) return new Uint8List(0); | |
| 120 var buffer = new Uint8List.view(_buffer.buffer, 0, _length); | |
| 121 clear(); | |
| 122 return buffer; | |
| 123 } | |
| 124 | |
| 125 List<int> toBytes() { | |
| 126 if (_buffer == null) return new Uint8List(0); | |
| 127 return new Uint8List.fromList( | |
| 128 new Uint8List.view(_buffer.buffer, 0, _length)); | |
| 129 } | |
| 130 | |
| 131 int get length => _length; | |
| 132 | |
| 133 bool get isEmpty => _length == 0; | |
| 134 | |
| 135 bool get isNotEmpty => _length != 0; | |
| 136 | |
| 137 void clear() { | |
| 138 _length = 0; | |
| 139 _buffer = null; | |
| 140 } | |
| 141 | |
| 142 int _pow2roundup(int x) { | |
| 143 --x; | |
| 144 x |= x >> 1; | |
| 145 x |= x >> 2; | |
| 146 x |= x >> 4; | |
| 147 x |= x >> 8; | |
| 148 x |= x >> 16; | |
| 149 return x + 1; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 | |
| 154 class _BytesBuilder implements BytesBuilder { | |
| 155 int _length = 0; | |
| 156 final _chunks = <List<int>>[]; | |
| 157 | |
| 158 void add(List<int> bytes) { | |
| 159 if (bytes is! Uint8List) { | |
| 160 bytes = new Uint8List.fromList(bytes); | |
| 161 } | |
| 162 _chunks.add(bytes); | |
| 163 _length += bytes.length; | |
| 164 } | |
| 165 | |
| 166 void addByte(int byte) { add([byte]); } | |
| 167 | |
| 168 List<int> takeBytes() { | |
| 169 if (_chunks.length == 0) return new Uint8List(0); | |
| 170 if (_chunks.length == 1) { | |
| 171 var buffer = _chunks.single; | |
| 172 clear(); | |
| 173 return buffer; | |
| 174 } | |
| 175 var buffer = new Uint8List(_length); | |
| 176 int offset = 0; | |
| 177 for (var chunk in _chunks) { | |
| 178 buffer.setRange(offset, offset + chunk.length, chunk); | |
| 179 offset += chunk.length; | |
| 180 } | |
| 181 clear(); | |
| 182 return buffer; | |
| 183 } | |
| 184 | |
| 185 List<int> toBytes() { | |
| 186 if (_chunks.length == 0) return new Uint8List(0); | |
| 187 var buffer = new Uint8List(_length); | |
| 188 int offset = 0; | |
| 189 for (var chunk in _chunks) { | |
| 190 buffer.setRange(offset, offset + chunk.length, chunk); | |
| 191 offset += chunk.length; | |
| 192 } | |
| 193 return buffer; | |
| 194 } | |
| 195 | |
| 196 int get length => _length; | |
| 197 | |
| 198 bool get isEmpty => _length == 0; | |
| 199 | |
| 200 bool get isNotEmpty => _length != 0; | |
| 201 | |
| 202 void clear() { | |
| 203 _length = 0; | |
| 204 _chunks.clear(); | |
| 205 } | |
| 206 } | |
| OLD | NEW |