| 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 // This is a copy of "dart:io"'s BytesBuilder implementation, from | 5 // This is a copy of "dart:io"'s BytesBuilder implementation, from |
| 6 // sdk/lib/io/bytes_builder.dart. It's copied here to make it available to | 6 // sdk/lib/io/bytes_builder.dart. It's copied here to make it available to |
| 7 // non-"dart:io" applications (issue 18348). | 7 // non-"dart:io" applications (issue 18348). |
| 8 // | 8 // |
| 9 // Because it's copied directly, there are no modifications from the original. | 9 // Because it's copied directly, there are no modifications from the original. |
| 10 // | 10 // |
| 11 // This is up-to-date as of sdk revision | 11 // This is up-to-date as of sdk revision |
| 12 // 86227840d75d974feb238f8b3c59c038b99c05cf. | 12 // e41fb4cafd6052157dbc1490d437045240f4773f. |
| 13 |
| 13 import 'dart:math'; | 14 import 'dart:math'; |
| 14 import 'dart:typed_data'; | 15 import 'dart:typed_data'; |
| 15 | 16 |
| 16 /** | 17 /** |
| 17 * Builds a list of bytes, allowing bytes and lists of bytes to be added at the | 18 * Builds a list of bytes, allowing bytes and lists of bytes to be added at the |
| 18 * end. | 19 * end. |
| 19 * | 20 * |
| 20 * Used to efficiently collect bytes and lists of bytes. | 21 * Used to efficiently collect bytes and lists of bytes. |
| 21 */ | 22 */ |
| 22 abstract class BytesBuilder { | 23 abstract class BytesBuilder { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 if (bytes is Uint8List) { | 116 if (bytes is Uint8List) { |
| 116 _buffer.setRange(_length, required, bytes); | 117 _buffer.setRange(_length, required, bytes); |
| 117 } else { | 118 } else { |
| 118 for (int i = 0; i < bytesLength; i++) { | 119 for (int i = 0; i < bytesLength; i++) { |
| 119 _buffer[_length + i] = bytes[i]; | 120 _buffer[_length + i] = bytes[i]; |
| 120 } | 121 } |
| 121 } | 122 } |
| 122 _length = required; | 123 _length = required; |
| 123 } | 124 } |
| 124 | 125 |
| 125 void addByte(int byte) => add([byte]); | 126 void addByte(int byte) { add([byte]); } |
| 126 | 127 |
| 127 List<int> takeBytes() { | 128 List<int> takeBytes() { |
| 128 if (_buffer == null) return new Uint8List(0); | 129 if (_buffer == null) return new Uint8List(0); |
| 129 var buffer = new Uint8List.view(_buffer.buffer, 0, _length); | 130 var buffer = new Uint8List.view(_buffer.buffer, 0, _length); |
| 130 clear(); | 131 clear(); |
| 131 return buffer; | 132 return buffer; |
| 132 } | 133 } |
| 133 | 134 |
| 134 List<int> toBytes() { | 135 List<int> toBytes() { |
| 135 if (_buffer == null) return new Uint8List(0); | 136 if (_buffer == null) return new Uint8List(0); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 155 x |= x >> 4; | 156 x |= x >> 4; |
| 156 x |= x >> 8; | 157 x |= x >> 8; |
| 157 x |= x >> 16; | 158 x |= x >> 16; |
| 158 return x + 1; | 159 return x + 1; |
| 159 } | 160 } |
| 160 } | 161 } |
| 161 | 162 |
| 162 | 163 |
| 163 class _BytesBuilder implements BytesBuilder { | 164 class _BytesBuilder implements BytesBuilder { |
| 164 int _length = 0; | 165 int _length = 0; |
| 165 final List _chunks = []; | 166 final _chunks = <List<int>>[]; |
| 166 | 167 |
| 167 void add(List<int> bytes) { | 168 void add(List<int> bytes) { |
| 168 if (bytes is! Uint8List) { | 169 if (bytes is! Uint8List) { |
| 169 bytes = new Uint8List.fromList(bytes); | 170 bytes = new Uint8List.fromList(bytes); |
| 170 } | 171 } |
| 171 _chunks.add(bytes); | 172 _chunks.add(bytes); |
| 172 _length += bytes.length; | 173 _length += bytes.length; |
| 173 } | 174 } |
| 174 | 175 |
| 175 void addByte(int byte) => add([byte]); | 176 void addByte(int byte) { add([byte]); } |
| 176 | 177 |
| 177 List<int> takeBytes() { | 178 List<int> takeBytes() { |
| 178 if (_chunks.length == 0) return new Uint8List(0); | 179 if (_chunks.length == 0) return new Uint8List(0); |
| 179 if (_chunks.length == 1) { | 180 if (_chunks.length == 1) { |
| 180 var buffer = _chunks.single; | 181 var buffer = _chunks.single; |
| 181 clear(); | 182 clear(); |
| 182 return buffer; | 183 return buffer; |
| 183 } | 184 } |
| 184 var buffer = new Uint8List(_length); | 185 var buffer = new Uint8List(_length); |
| 185 int offset = 0; | 186 int offset = 0; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 206 | 207 |
| 207 bool get isEmpty => _length == 0; | 208 bool get isEmpty => _length == 0; |
| 208 | 209 |
| 209 bool get isNotEmpty => _length != 0; | 210 bool get isNotEmpty => _length != 0; |
| 210 | 211 |
| 211 void clear() { | 212 void clear() { |
| 212 _length = 0; | 213 _length = 0; |
| 213 _chunks.clear(); | 214 _chunks.clear(); |
| 214 } | 215 } |
| 215 } | 216 } |
| OLD | NEW |