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.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 | 10 |
| 11 import 'asset.dart'; |
11 import 'asset_id.dart'; | 12 import 'asset_id.dart'; |
12 import 'file_pool.dart'; | 13 import 'file_pool.dart'; |
| 14 import 'serialize.dart'; |
13 import 'stream_replayer.dart'; | 15 import 'stream_replayer.dart'; |
14 import 'utils.dart'; | 16 import 'utils.dart'; |
15 | 17 |
16 /// A blob of content. | 18 /// Serialize an asset to a form that's safe to send across isolates. |
17 /// | 19 Map serializeAsset(Asset asset) { |
18 /// Assets may come from the file system, or as the output of a [Transformer]. | 20 var id = serializeId(asset.id); |
19 /// They are identified by [AssetId]. | 21 if (asset is BinaryAsset) { |
20 abstract class Asset { | 22 // If [_contents] is a custom subclass of List, it won't be serializable, so |
21 /// The ID for this asset. | 23 // we have to convert it to a built-in [List] first. |
22 final AssetId id; | 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(); |
23 | 27 |
24 Asset(this.id); | 28 return { |
| 29 'type': 'binary', |
| 30 'id': id, |
| 31 'contents': contents |
| 32 }; |
| 33 } else if (asset is FileAsset) { |
| 34 return { |
| 35 'type': 'file', |
| 36 'id': id, |
| 37 'path': asset._path |
| 38 }; |
| 39 } else if (asset is StringAsset) { |
| 40 return { |
| 41 'type': 'string', |
| 42 'id': id, |
| 43 'contents': asset._contents |
| 44 }; |
| 45 } else { |
| 46 // [asset] is probably a [StreamAsset], but it's possible that the user has |
| 47 // created a custom subclass, in which case we just serialize the stream |
| 48 // anyway. |
| 49 return { |
| 50 'type': 'stream', |
| 51 'id': id, |
| 52 'stream': serializeStream(asset.read()) |
| 53 }; |
| 54 } |
| 55 } |
25 | 56 |
26 factory Asset.fromBytes(AssetId id, List<int> bytes) => | 57 /// Deserialize an asset from the form returned by [serialize]. |
27 new _BinaryAsset(id, bytes); | 58 Asset deserializeAsset(Map asset) { |
28 | 59 var id = deserializeId(asset['id']); |
29 factory Asset.fromFile(AssetId id, File file) => | 60 switch (asset['type']) { |
30 new _FileAsset(id, file); | 61 case 'binary': return new BinaryAsset(id, asset['contents']); |
31 | 62 case 'file': return new FileAsset(id, asset['path']); |
32 factory Asset.fromString(AssetId id, String content) => | 63 case 'string': return new StringAsset(id, asset['contents']); |
33 new _StringAsset(id, content); | 64 case 'stream': |
34 | 65 return new StreamAsset(id, deserializeStream(asset['stream'])); |
35 factory Asset.fromPath(AssetId id, String path) => | 66 default: |
36 new _FileAsset(id, new File(path)); | 67 throw new FormatException('Unknown asset type "${asset['type']}".'); |
37 | 68 } |
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 | |
44 /// Returns the contents of the asset as a string. | |
45 /// | |
46 /// If the asset was created from a [String] the original string is always | |
47 /// returned and [encoding] is ignored. Otherwise, the binary data of the | |
48 /// asset is decoded using [encoding], which defaults to [UTF8]. | |
49 Future<String> readAsString({Encoding encoding}); | |
50 | |
51 /// Streams the binary contents of the asset. | |
52 /// | |
53 /// If the asset was created from a [String], this returns its UTF-8 encoding. | |
54 Stream<List<int>> read(); | |
55 } | 69 } |
56 | 70 |
57 /// An asset whose data is stored in a list of bytes. | 71 /// An asset whose data is stored in a list of bytes. |
58 class _BinaryAsset extends Asset { | 72 class BinaryAsset implements Asset { |
| 73 final AssetId id; |
| 74 |
59 final List<int> _contents; | 75 final List<int> _contents; |
60 | 76 |
61 _BinaryAsset(AssetId id, this._contents) | 77 BinaryAsset(this.id, this._contents); |
62 : super(id); | |
63 | 78 |
64 Future<String> readAsString({Encoding encoding}) { | 79 Future<String> readAsString({Encoding encoding}) { |
65 if (encoding == null) encoding = UTF8; | 80 if (encoding == null) encoding = UTF8; |
66 | 81 |
67 return new Future.value(encoding.decode(_contents)); | 82 return new Future.value(encoding.decode(_contents)); |
68 } | 83 } |
69 | 84 |
70 Stream<List<int>> read() => new Future<List<int>>.value(_contents).asStream(); | 85 Stream<List<int>> read() => new Future<List<int>>.value(_contents).asStream(); |
71 | 86 |
72 String toString() { | 87 String toString() { |
(...skipping 19 matching lines...) Expand all Loading... |
92 buffer.write(byteToHex(_contents[i])); | 107 buffer.write(byteToHex(_contents[i])); |
93 } | 108 } |
94 } | 109 } |
95 | 110 |
96 buffer.write("]"); | 111 buffer.write("]"); |
97 return buffer.toString(); | 112 return buffer.toString(); |
98 } | 113 } |
99 } | 114 } |
100 | 115 |
101 /// An asset backed by a file on the local file system. | 116 /// An asset backed by a file on the local file system. |
102 class _FileAsset extends Asset { | 117 class FileAsset implements Asset { |
| 118 final AssetId id; |
| 119 |
103 /// Use a [FilePool] to handle reads so we can try to cope with running out | 120 /// Use a [FilePool] to handle reads so we can try to cope with running out |
104 /// of file descriptors more gracefully. | 121 /// of file descriptors more gracefully. |
105 static final _pool = new FilePool(); | 122 static final _pool = new FilePool(); |
106 | 123 |
107 final File _file; | 124 final String _path; |
108 _FileAsset(AssetId id, this._file) | 125 FileAsset(this.id, this._path); |
109 : super(id); | |
110 | 126 |
111 Future<String> readAsString({Encoding encoding}) { | 127 Future<String> readAsString({Encoding encoding}) { |
112 if (encoding == null) encoding = UTF8; | 128 if (encoding == null) encoding = UTF8; |
113 return _pool.readAsString(_file, encoding); | 129 return _pool.readAsString(_path, encoding); |
114 } | 130 } |
115 | 131 |
116 Stream<List<int>> read() => _pool.openRead(_file); | 132 Stream<List<int>> read() => _pool.openRead(_path); |
117 | 133 |
118 String toString() => 'File "${_file.path}"'; | 134 String toString() => 'File "${_path}"'; |
119 } | 135 } |
120 | 136 |
121 /// An asset whose data is stored in a string. | 137 /// An asset whose data is stored in a string. |
122 class _StringAsset extends Asset { | 138 class StringAsset implements Asset { |
| 139 final AssetId id; |
| 140 |
123 final String _contents; | 141 final String _contents; |
124 | 142 |
125 _StringAsset(AssetId id, this._contents) | 143 StringAsset(this.id, this._contents); |
126 : super(id); | |
127 | 144 |
128 Future<String> readAsString({Encoding encoding}) => | 145 Future<String> readAsString({Encoding encoding}) => |
129 new Future.value(_contents); | 146 new Future.value(_contents); |
130 | 147 |
131 Stream<List<int>> read() => | 148 Stream<List<int>> read() => |
132 new Future<List<int>>.value(UTF8.encode(_contents)).asStream(); | 149 new Future<List<int>>.value(UTF8.encode(_contents)).asStream(); |
133 | 150 |
134 String toString() { | 151 String toString() { |
135 // Don't show the whole string if it's long. | 152 // Don't show the whole string if it's long. |
136 var contents = _contents; | 153 var contents = _contents; |
137 if (contents.length > 40) { | 154 if (contents.length > 40) { |
138 contents = contents.substring(0, 20) + " ... " + | 155 contents = contents.substring(0, 20) + " ... " + |
139 contents.substring(contents.length - 20); | 156 contents.substring(contents.length - 20); |
140 } | 157 } |
141 | 158 |
142 contents = _escape(contents); | 159 contents = _escape(contents); |
143 return 'String "$contents"'; | 160 return 'String "$contents"'; |
144 } | 161 } |
145 | 162 |
146 String _escape(String string) { | 163 String _escape(String string) { |
147 return string | 164 return string |
148 .replaceAll("\"", r'\"') | 165 .replaceAll("\"", r'\"') |
149 .replaceAll("\n", r"\n") | 166 .replaceAll("\n", r"\n") |
150 .replaceAll("\r", r"\r") | 167 .replaceAll("\r", r"\r") |
151 .replaceAll("\t", r"\t"); | 168 .replaceAll("\t", r"\t"); |
152 } | 169 } |
153 } | 170 } |
154 | 171 |
155 /// An asset whose data is available from a stream. | 172 /// An asset whose data is available from a stream. |
156 class _StreamAsset extends Asset { | 173 class StreamAsset implements Asset { |
| 174 final AssetId id; |
| 175 |
157 /// A stream replayer that records and replays the contents of the input | 176 /// A stream replayer that records and replays the contents of the input |
158 /// stream. | 177 /// stream. |
159 final StreamReplayer<List<int>> _replayer; | 178 final StreamReplayer<List<int>> _replayer; |
160 | 179 |
161 _StreamAsset(AssetId id, Stream<List<int>> stream) | 180 StreamAsset(this.id, Stream<List<int>> stream) |
162 : _replayer = new StreamReplayer(stream), | 181 : _replayer = new StreamReplayer(stream); |
163 super(id); | |
164 | 182 |
165 Future<String> readAsString({Encoding encoding}) { | 183 Future<String> readAsString({Encoding encoding}) { |
166 if (encoding == null) encoding = UTF8; | 184 if (encoding == null) encoding = UTF8; |
167 return _replayer.getReplay().toList() | 185 return _replayer.getReplay().toList() |
168 .then((chunks) => encoding.decode(flatten(chunks))); | 186 .then((chunks) => encoding.decode(flatten(chunks))); |
169 } | 187 } |
170 | 188 |
171 Stream<List<int>> read() => _replayer.getReplay(); | 189 Stream<List<int>> read() => _replayer.getReplay(); |
172 | 190 |
173 String toString() => "Stream"; | 191 String toString() => "Stream"; |
174 } | 192 } |
OLD | NEW |