| 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; | 5 library barback.asset; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert' show Encoding, UTF8; | 8 import 'dart:convert' show Encoding, UTF8; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'dart:utf'; | 10 import 'dart:utf'; |
| 11 | 11 |
| 12 import 'asset_id.dart'; | 12 import 'asset_id.dart'; |
| 13 import 'stream_replayer.dart'; |
| 13 import 'utils.dart'; | 14 import 'utils.dart'; |
| 14 | 15 |
| 15 /// A blob of content. | 16 /// A blob of content. |
| 16 /// | 17 /// |
| 17 /// Assets may come from the file system, or as the output of a [Transformer]. | 18 /// Assets may come from the file system, or as the output of a [Transformer]. |
| 18 /// They are identified by [AssetId]. | 19 /// They are identified by [AssetId]. |
| 19 abstract class Asset { | 20 abstract class Asset { |
| 20 /// The ID for this asset. | 21 /// The ID for this asset. |
| 21 final AssetId id; | 22 final AssetId id; |
| 22 | 23 |
| 23 Asset(this.id); | 24 Asset(this.id); |
| 24 | 25 |
| 25 factory Asset.fromBytes(AssetId id, List<int> bytes) => | 26 factory Asset.fromBytes(AssetId id, List<int> bytes) => |
| 26 new _BinaryAsset(id, bytes); | 27 new _BinaryAsset(id, bytes); |
| 27 | 28 |
| 28 factory Asset.fromFile(AssetId id, File file) => | 29 factory Asset.fromFile(AssetId id, File file) => |
| 29 new _FileAsset(id, file); | 30 new _FileAsset(id, file); |
| 30 | 31 |
| 31 factory Asset.fromString(AssetId id, String content) => | 32 factory Asset.fromString(AssetId id, String content) => |
| 32 new _StringAsset(id, content); | 33 new _StringAsset(id, content); |
| 33 | 34 |
| 34 factory Asset.fromPath(AssetId id, String path) => | 35 factory Asset.fromPath(AssetId id, String path) => |
| 35 new _FileAsset(id, new File(path)); | 36 new _FileAsset(id, new File(path)); |
| 36 | 37 |
| 38 /// Creates an asset from a stream. |
| 39 /// |
| 40 /// This immediately starts draining [stream]. |
| 41 factory Asset.fromStream(AssetId id, Stream<List<int>> stream) => |
| 42 new _StreamAsset(id, stream); |
| 43 |
| 37 /// Returns the contents of the asset as a string. | 44 /// Returns the contents of the asset as a string. |
| 38 /// | 45 /// |
| 39 /// If the asset was created from a [String] the original string is always | 46 /// If the asset was created from a [String] the original string is always |
| 40 /// returned and [encoding] is ignored. Otherwise, the binary data of the | 47 /// returned and [encoding] is ignored. Otherwise, the binary data of the |
| 41 /// asset is decoded using [encoding], which defaults to [UTF8]. | 48 /// asset is decoded using [encoding], which defaults to [UTF8]. |
| 42 Future<String> readAsString({Encoding encoding}); | 49 Future<String> readAsString({Encoding encoding}); |
| 43 | 50 |
| 44 /// Streams the binary contents of the asset. | 51 /// Streams the binary contents of the asset. |
| 45 /// | 52 /// |
| 46 /// If the asset was created from a [String], this returns its UTF-8 encoding. | 53 /// If the asset was created from a [String], this returns its UTF-8 encoding. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 } | 140 } |
| 134 | 141 |
| 135 String _escape(String string) { | 142 String _escape(String string) { |
| 136 return string | 143 return string |
| 137 .replaceAll("\"", r'\"') | 144 .replaceAll("\"", r'\"') |
| 138 .replaceAll("\n", r"\n") | 145 .replaceAll("\n", r"\n") |
| 139 .replaceAll("\r", r"\r") | 146 .replaceAll("\r", r"\r") |
| 140 .replaceAll("\t", r"\t"); | 147 .replaceAll("\t", r"\t"); |
| 141 } | 148 } |
| 142 } | 149 } |
| 150 |
| 151 /// An asset whose data is available from a stream. |
| 152 class _StreamAsset extends Asset { |
| 153 /// A stream replayer that records and replays the contents of the input |
| 154 /// stream. |
| 155 final StreamReplayer<List<int>> _replayer; |
| 156 |
| 157 _StreamAsset(AssetId id, Stream<List<int>> stream) |
| 158 : _replayer = new StreamReplayer(stream), |
| 159 super(id); |
| 160 |
| 161 Future<String> readAsString({Encoding encoding}) { |
| 162 if (encoding == null) encoding = UTF8; |
| 163 return _replayer.getReplay().toList() |
| 164 .then((chunks) => encoding.decode(flatten(chunks))); |
| 165 } |
| 166 |
| 167 Stream<List<int>> read() => _replayer.getReplay(); |
| 168 |
| 169 String toString() => "Stream"; |
| 170 } |
| OLD | NEW |