| 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'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| 11 import 'asset_id.dart'; | 11 import 'asset_id.dart'; |
| 12 import 'file_pool.dart'; |
| 12 import 'stream_replayer.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; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 } | 93 } |
| 93 } | 94 } |
| 94 | 95 |
| 95 buffer.write("]"); | 96 buffer.write("]"); |
| 96 return buffer.toString(); | 97 return buffer.toString(); |
| 97 } | 98 } |
| 98 } | 99 } |
| 99 | 100 |
| 100 /// An asset backed by a file on the local file system. | 101 /// An asset backed by a file on the local file system. |
| 101 class _FileAsset extends Asset { | 102 class _FileAsset extends Asset { |
| 103 /// Use a [FilePool] to handle reads so we can try to cope with running out |
| 104 /// of file descriptors more gracefully. |
| 105 static final _pool = new FilePool(); |
| 106 |
| 102 final File _file; | 107 final File _file; |
| 103 _FileAsset(AssetId id, this._file) | 108 _FileAsset(AssetId id, this._file) |
| 104 : super(id); | 109 : super(id); |
| 105 | 110 |
| 106 Future<String> readAsString({Encoding encoding}) { | 111 Future<String> readAsString({Encoding encoding}) { |
| 107 if (encoding == null) encoding = UTF8; | 112 if (encoding == null) encoding = UTF8; |
| 108 return _file.readAsString(encoding: encoding); | 113 return _pool.readAsString(_file, encoding); |
| 109 } | 114 } |
| 110 | 115 |
| 111 Stream<List<int>> read() => _file.openRead(); | 116 Stream<List<int>> read() => _pool.openRead(_file); |
| 112 | 117 |
| 113 String toString() => 'File "${_file.path}"'; | 118 String toString() => 'File "${_file.path}"'; |
| 114 } | 119 } |
| 115 | 120 |
| 116 /// An asset whose data is stored in a string. | 121 /// An asset whose data is stored in a string. |
| 117 class _StringAsset extends Asset { | 122 class _StringAsset extends Asset { |
| 118 final String _contents; | 123 final String _contents; |
| 119 | 124 |
| 120 _StringAsset(AssetId id, this._contents) | 125 _StringAsset(AssetId id, this._contents) |
| 121 : super(id); | 126 : super(id); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 Future<String> readAsString({Encoding encoding}) { | 165 Future<String> readAsString({Encoding encoding}) { |
| 161 if (encoding == null) encoding = UTF8; | 166 if (encoding == null) encoding = UTF8; |
| 162 return _replayer.getReplay().toList() | 167 return _replayer.getReplay().toList() |
| 163 .then((chunks) => encoding.decode(flatten(chunks))); | 168 .then((chunks) => encoding.decode(flatten(chunks))); |
| 164 } | 169 } |
| 165 | 170 |
| 166 Stream<List<int>> read() => _replayer.getReplay(); | 171 Stream<List<int>> read() => _replayer.getReplay(); |
| 167 | 172 |
| 168 String toString() => "Stream"; | 173 String toString() => "Stream"; |
| 169 } | 174 } |
| OLD | NEW |