| 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.asset.internal_asset; | 5 library barback.asset.internal_asset; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
| 10 | 10 |
| 11 import 'package:async/async.dart'; |
| 12 import 'package:collection/collection.dart'; |
| 13 |
| 11 import '../serialize.dart'; | 14 import '../serialize.dart'; |
| 12 import '../utils.dart'; | 15 import '../utils.dart'; |
| 13 import '../utils/file_pool.dart'; | 16 import '../utils/file_pool.dart'; |
| 14 import '../utils/stream_replayer.dart'; | 17 import '../utils/stream_replayer.dart'; |
| 15 import 'asset.dart'; | 18 import 'asset.dart'; |
| 16 import 'asset_id.dart'; | 19 import 'asset_id.dart'; |
| 17 | 20 |
| 18 /// Serialize an asset to a form that's safe to send across isolates. | 21 /// Serialize an asset to a form that's safe to send across isolates. |
| 19 Map serializeAsset(Asset asset) { | 22 Map serializeAsset(Asset asset) { |
| 20 var id = serializeId(asset.id); | 23 var id = serializeId(asset.id); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 45 'id': id, | 48 'id': id, |
| 46 'stream': serializeStream(asset.read()) | 49 'stream': serializeStream(asset.read()) |
| 47 }; | 50 }; |
| 48 } | 51 } |
| 49 } | 52 } |
| 50 | 53 |
| 51 /// Deserialize an asset from the form returned by [serialize]. | 54 /// Deserialize an asset from the form returned by [serialize]. |
| 52 Asset deserializeAsset(Map asset) { | 55 Asset deserializeAsset(Map asset) { |
| 53 var id = deserializeId(asset['id']); | 56 var id = deserializeId(asset['id']); |
| 54 switch (asset['type']) { | 57 switch (asset['type']) { |
| 55 case 'binary': return new BinaryAsset(id, asset['contents']); | 58 case 'binary': |
| 59 return new BinaryAsset( |
| 60 id, DelegatingList.typed(asset['contents'] as List)); |
| 56 case 'file': return new FileAsset(id, asset['path']); | 61 case 'file': return new FileAsset(id, asset['path']); |
| 57 case 'string': return new StringAsset(id, asset['contents']); | 62 case 'string': return new StringAsset(id, asset['contents']); |
| 58 case 'stream': | 63 case 'stream': |
| 59 return new StreamAsset(id, deserializeStream(asset['stream'])); | 64 return new StreamAsset( |
| 65 id, DelegatingStream.typed(deserializeStream(asset['stream']))); |
| 60 default: | 66 default: |
| 61 throw new FormatException('Unknown asset type "${asset['type']}".'); | 67 throw new FormatException('Unknown asset type "${asset['type']}".'); |
| 62 } | 68 } |
| 63 } | 69 } |
| 64 | 70 |
| 65 /// An asset whose data is stored in a list of bytes. | 71 /// An asset whose data is stored in a list of bytes. |
| 66 class BinaryAsset implements Asset { | 72 class BinaryAsset implements Asset { |
| 67 final AssetId id; | 73 final AssetId id; |
| 68 | 74 |
| 69 final Uint8List _contents; | 75 final Uint8List _contents; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 176 |
| 171 /// A stream replayer that records and replays the contents of the input | 177 /// A stream replayer that records and replays the contents of the input |
| 172 /// stream. | 178 /// stream. |
| 173 final StreamReplayer<List<int>> _replayer; | 179 final StreamReplayer<List<int>> _replayer; |
| 174 | 180 |
| 175 StreamAsset(this.id, Stream<List<int>> stream) | 181 StreamAsset(this.id, Stream<List<int>> stream) |
| 176 : _replayer = new StreamReplayer(stream); | 182 : _replayer = new StreamReplayer(stream); |
| 177 | 183 |
| 178 Future<String> readAsString({Encoding encoding}) { | 184 Future<String> readAsString({Encoding encoding}) { |
| 179 if (encoding == null) encoding = UTF8; | 185 if (encoding == null) encoding = UTF8; |
| 180 return _replayer.getReplay().toList() | 186 return _replayer.getReplay().expand((chunk) => chunk).toList() |
| 181 .then((chunks) => encoding.decode(flatten(chunks))); | 187 .then((bytes) => encoding.decode(bytes)); |
| 182 } | 188 } |
| 183 | 189 |
| 184 Stream<List<int>> read() => _replayer.getReplay(); | 190 Stream<List<int>> read() => _replayer.getReplay(); |
| 185 | 191 |
| 186 String toString() => "Stream"; | 192 String toString() => "Stream"; |
| 187 } | 193 } |
| OLD | NEW |