| 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 library barback.internal_asset; | 5 library barback.internal_asset; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'dart:typed_data'; |
| 10 | 11 |
| 11 import 'asset.dart'; | 12 import 'asset.dart'; |
| 12 import 'asset_id.dart'; | 13 import 'asset_id.dart'; |
| 13 import 'file_pool.dart'; | 14 import 'file_pool.dart'; |
| 14 import 'serialize.dart'; | 15 import 'serialize.dart'; |
| 15 import 'stream_replayer.dart'; | 16 import 'stream_replayer.dart'; |
| 16 import 'utils.dart'; | 17 import 'utils.dart'; |
| 17 | 18 |
| 18 /// Serialize an asset to a form that's safe to send across isolates. | 19 /// Serialize an asset to a form that's safe to send across isolates. |
| 19 Map serializeAsset(Asset asset) { | 20 Map serializeAsset(Asset asset) { |
| 20 var id = serializeId(asset.id); | 21 var id = serializeId(asset.id); |
| 21 if (asset is BinaryAsset) { | 22 if (asset is BinaryAsset) { |
| 22 // If [_contents] is a custom subclass of List, it won't be serializable, so | |
| 23 // we have to convert it to a built-in [List] first. | |
| 24 // TODO(nweiz): Send a typed array if that works after issue 14703 is fixed. | |
| 25 var contents = asset._contents.runtimeType == List ? | |
| 26 asset._contents : asset._contents.toList(); | |
| 27 | |
| 28 return { | 23 return { |
| 29 'type': 'binary', | 24 'type': 'binary', |
| 30 'id': id, | 25 'id': id, |
| 31 'contents': contents | 26 'contents': asset._contents |
| 32 }; | 27 }; |
| 33 } else if (asset is FileAsset) { | 28 } else if (asset is FileAsset) { |
| 34 return { | 29 return { |
| 35 'type': 'file', | 30 'type': 'file', |
| 36 'id': id, | 31 'id': id, |
| 37 'path': asset._path | 32 'path': asset._path |
| 38 }; | 33 }; |
| 39 } else if (asset is StringAsset) { | 34 } else if (asset is StringAsset) { |
| 40 return { | 35 return { |
| 41 'type': 'string', | 36 'type': 'string', |
| (...skipping 23 matching lines...) Expand all Loading... |
| 65 return new StreamAsset(id, deserializeStream(asset['stream'])); | 60 return new StreamAsset(id, deserializeStream(asset['stream'])); |
| 66 default: | 61 default: |
| 67 throw new FormatException('Unknown asset type "${asset['type']}".'); | 62 throw new FormatException('Unknown asset type "${asset['type']}".'); |
| 68 } | 63 } |
| 69 } | 64 } |
| 70 | 65 |
| 71 /// An asset whose data is stored in a list of bytes. | 66 /// An asset whose data is stored in a list of bytes. |
| 72 class BinaryAsset implements Asset { | 67 class BinaryAsset implements Asset { |
| 73 final AssetId id; | 68 final AssetId id; |
| 74 | 69 |
| 75 final List<int> _contents; | 70 final Uint8List _contents; |
| 76 | 71 |
| 77 BinaryAsset(this.id, this._contents); | 72 BinaryAsset(this.id, List<int> contents) |
| 73 : _contents = toUint8List(contents); |
| 78 | 74 |
| 79 Future<String> readAsString({Encoding encoding}) { | 75 Future<String> readAsString({Encoding encoding}) { |
| 80 if (encoding == null) encoding = UTF8; | 76 if (encoding == null) encoding = UTF8; |
| 81 | 77 |
| 82 return new Future.value(encoding.decode(_contents)); | 78 return new Future.value(encoding.decode(_contents)); |
| 83 } | 79 } |
| 84 | 80 |
| 85 Stream<List<int>> read() => new Future<List<int>>.value(_contents).asStream(); | 81 Stream<List<int>> read() => new Future<List<int>>.value(_contents).asStream(); |
| 86 | 82 |
| 87 String toString() { | 83 String toString() { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 Future<String> readAsString({Encoding encoding}) { | 179 Future<String> readAsString({Encoding encoding}) { |
| 184 if (encoding == null) encoding = UTF8; | 180 if (encoding == null) encoding = UTF8; |
| 185 return _replayer.getReplay().toList() | 181 return _replayer.getReplay().toList() |
| 186 .then((chunks) => encoding.decode(flatten(chunks))); | 182 .then((chunks) => encoding.decode(flatten(chunks))); |
| 187 } | 183 } |
| 188 | 184 |
| 189 Stream<List<int>> read() => _replayer.getReplay(); | 185 Stream<List<int>> read() => _replayer.getReplay(); |
| 190 | 186 |
| 191 String toString() => "Stream"; | 187 String toString() => "Stream"; |
| 192 } | 188 } |
| OLD | NEW |