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.asset; | 5 library barback.asset.asset; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:convert'; | 9 import 'dart:convert'; |
10 | 10 |
11 import 'asset_id.dart'; | 11 import 'asset_id.dart'; |
12 import 'internal_asset.dart'; | 12 import 'internal_asset.dart'; |
13 | 13 |
14 /// A blob of content. | 14 /// A blob of content. |
15 /// | 15 /// |
16 /// Assets may come from the file system, or as the output of a [Transformer]. | 16 /// Assets may come from the file system, or as the output of a [Transformer]. |
17 /// They are identified by [AssetId]. | 17 /// They are identified by [AssetId]. |
18 /// | 18 /// |
19 /// Custom implementations of [Asset] are not currently supported. | 19 /// Custom implementations of [Asset] are not currently supported. |
20 abstract class Asset { | 20 abstract class Asset { |
21 /// The ID for this asset. | 21 /// The ID for this asset. |
22 final AssetId id; | 22 final AssetId id; |
23 | 23 |
24 factory Asset.fromBytes(AssetId id, List<int> bytes) => | 24 factory Asset.fromBytes(AssetId id, List<int> bytes) => |
25 new BinaryAsset(id, bytes); | 25 new BinaryAsset(id, bytes); |
26 | 26 |
27 factory Asset.fromFile(AssetId id, File file) => | 27 factory Asset.fromFile(AssetId id, File file) => new FileAsset(id, file.path); |
28 new FileAsset(id, file.path); | |
29 | 28 |
30 factory Asset.fromString(AssetId id, String content) => | 29 factory Asset.fromString(AssetId id, String content) => |
31 new StringAsset(id, content); | 30 new StringAsset(id, content); |
32 | 31 |
33 factory Asset.fromPath(AssetId id, String path) => | 32 factory Asset.fromPath(AssetId id, String path) => new FileAsset(id, path); |
34 new FileAsset(id, path); | |
35 | 33 |
36 factory Asset.fromStream(AssetId id, Stream<List<int>> stream) => | 34 factory Asset.fromStream(AssetId id, Stream<List<int>> stream) => |
37 new StreamAsset(id, stream); | 35 new StreamAsset(id, stream); |
38 | 36 |
39 /// Returns the contents of the asset as a string. | 37 /// Returns the contents of the asset as a string. |
40 /// | 38 /// |
41 /// If the asset was created from a [String] the original string is always | 39 /// If the asset was created from a [String] the original string is always |
42 /// returned and [encoding] is ignored. Otherwise, the binary data of the | 40 /// returned and [encoding] is ignored. Otherwise, the binary data of the |
43 /// asset is decoded using [encoding], which defaults to [UTF8]. | 41 /// asset is decoded using [encoding], which defaults to [UTF8]. |
44 Future<String> readAsString({Encoding encoding}); | 42 Future<String> readAsString({Encoding encoding}); |
45 | 43 |
46 /// Streams the binary contents of the asset. | 44 /// Streams the binary contents of the asset. |
47 /// | 45 /// |
48 /// If the asset was created from a [String], this returns its UTF-8 encoding. | 46 /// If the asset was created from a [String], this returns its UTF-8 encoding. |
49 Stream<List<int>> read(); | 47 Stream<List<int>> read(); |
50 } | 48 } |
OLD | NEW |