OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 /** | 7 /** |
8 * Builds a list of bytes, allowing bytes and lists of bytes to be added at the | 8 * Builds a list of bytes, allowing bytes and lists of bytes to be added at the |
9 * end. | 9 * end. |
10 * | 10 * |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 * Returns `true` if the buffer is not empty. | 71 * Returns `true` if the buffer is not empty. |
72 */ | 72 */ |
73 bool get isNotEmpty; | 73 bool get isNotEmpty; |
74 | 74 |
75 /** | 75 /** |
76 * Clear the contents of the builder. | 76 * Clear the contents of the builder. |
77 */ | 77 */ |
78 void clear(); | 78 void clear(); |
79 } | 79 } |
80 | 80 |
81 | |
82 class _CopyingBytesBuilder implements BytesBuilder { | 81 class _CopyingBytesBuilder implements BytesBuilder { |
83 // Start with 1024 bytes. | 82 // Start with 1024 bytes. |
84 static const int _INIT_SIZE = 1024; | 83 static const int _INIT_SIZE = 1024; |
85 | 84 |
86 static final _emptyList = new Uint8List(0); | 85 static final _emptyList = new Uint8List(0); |
87 | 86 |
88 int _length = 0; | 87 int _length = 0; |
89 Uint8List _buffer; | 88 Uint8List _buffer; |
90 | 89 |
91 _CopyingBytesBuilder([int initialCapacity = 0]) | 90 _CopyingBytesBuilder([int initialCapacity = 0]) |
92 : _buffer = (initialCapacity <= 0) | 91 : _buffer = (initialCapacity <= 0) |
93 ? _emptyList | 92 ? _emptyList |
94 : new Uint8List(_pow2roundup(initialCapacity)); | 93 : new Uint8List(_pow2roundup(initialCapacity)); |
95 | 94 |
96 void add(List<int> bytes) { | 95 void add(List<int> bytes) { |
97 int bytesLength = bytes.length; | 96 int bytesLength = bytes.length; |
98 if (bytesLength == 0) return; | 97 if (bytesLength == 0) return; |
99 int required = _length + bytesLength; | 98 int required = _length + bytesLength; |
100 if (_buffer.length < required) { | 99 if (_buffer.length < required) { |
101 _grow(required); | 100 _grow(required); |
102 } | 101 } |
103 assert(_buffer.length >= required); | 102 assert(_buffer.length >= required); |
104 if (bytes is Uint8List) { | 103 if (bytes is Uint8List) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 --x; | 164 --x; |
166 x |= x >> 1; | 165 x |= x >> 1; |
167 x |= x >> 2; | 166 x |= x >> 2; |
168 x |= x >> 4; | 167 x |= x >> 4; |
169 x |= x >> 8; | 168 x |= x >> 8; |
170 x |= x >> 16; | 169 x |= x >> 16; |
171 return x + 1; | 170 return x + 1; |
172 } | 171 } |
173 } | 172 } |
174 | 173 |
175 | |
176 class _BytesBuilder implements BytesBuilder { | 174 class _BytesBuilder implements BytesBuilder { |
177 int _length = 0; | 175 int _length = 0; |
178 final List<Uint8List> _chunks = []; | 176 final List<Uint8List> _chunks = []; |
179 | 177 |
180 void add(List<int> bytes) { | 178 void add(List<int> bytes) { |
181 Uint8List typedBytes; | 179 Uint8List typedBytes; |
182 if (bytes is Uint8List) { | 180 if (bytes is Uint8List) { |
183 typedBytes = bytes; | 181 typedBytes = bytes; |
184 } else { | 182 } else { |
185 typedBytes = new Uint8List.fromList(bytes); | 183 typedBytes = new Uint8List.fromList(bytes); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 223 |
226 bool get isEmpty => _length == 0; | 224 bool get isEmpty => _length == 0; |
227 | 225 |
228 bool get isNotEmpty => _length != 0; | 226 bool get isNotEmpty => _length != 0; |
229 | 227 |
230 void clear() { | 228 void clear() { |
231 _length = 0; | 229 _length = 0; |
232 _chunks.clear(); | 230 _chunks.clear(); |
233 } | 231 } |
234 } | 232 } |
OLD | NEW |