| 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:io'; | 9 import 'dart:io'; |
| 9 import 'dart:utf'; | 10 import 'dart:utf'; |
| 10 | 11 |
| 11 import 'asset_id.dart'; | 12 import 'asset_id.dart'; |
| 12 import 'utils.dart'; | 13 import 'utils.dart'; |
| 13 | 14 |
| 14 /// A blob of content. | 15 /// A blob of content. |
| 15 /// | 16 /// |
| 16 /// Assets may come from the file system, or as the output of a [Transformer]. | 17 /// Assets may come from the file system, or as the output of a [Transformer]. |
| 17 /// They are identified by [AssetId]. | 18 /// They are identified by [AssetId]. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 30 factory Asset.fromString(AssetId id, String content) => | 31 factory Asset.fromString(AssetId id, String content) => |
| 31 new _StringAsset(id, content); | 32 new _StringAsset(id, content); |
| 32 | 33 |
| 33 factory Asset.fromPath(AssetId id, String path) => | 34 factory Asset.fromPath(AssetId id, String path) => |
| 34 new _FileAsset(id, new File(path)); | 35 new _FileAsset(id, new File(path)); |
| 35 | 36 |
| 36 /// Returns the contents of the asset as a string. | 37 /// Returns the contents of the asset as a string. |
| 37 /// | 38 /// |
| 38 /// 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 |
| 39 /// returned and [encoding] is ignored. Otherwise, the binary data of the | 40 /// returned and [encoding] is ignored. Otherwise, the binary data of the |
| 40 /// asset is decoded using [encoding], which defaults to [Encoding.UTF_8]. | 41 /// asset is decoded using [encoding], which defaults to [UTF8]. |
| 41 Future<String> readAsString({Encoding encoding}); | 42 Future<String> readAsString({Encoding encoding}); |
| 42 | 43 |
| 43 /// Streams the binary contents of the asset. | 44 /// Streams the binary contents of the asset. |
| 44 /// | 45 /// |
| 45 /// 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. |
| 46 Stream<List<int>> read(); | 47 Stream<List<int>> read(); |
| 47 } | 48 } |
| 48 | 49 |
| 49 /// An asset whose data is stored in a list of bytes. | 50 /// An asset whose data is stored in a list of bytes. |
| 50 class _BinaryAsset extends Asset { | 51 class _BinaryAsset extends Asset { |
| 51 final List<int> _contents; | 52 final List<int> _contents; |
| 52 | 53 |
| 53 _BinaryAsset(AssetId id, this._contents) | 54 _BinaryAsset(AssetId id, this._contents) |
| 54 : super(id); | 55 : super(id); |
| 55 | 56 |
| 56 Future<String> readAsString({Encoding encoding}) { | 57 Future<String> readAsString({Encoding encoding}) { |
| 57 if (encoding == null) encoding = Encoding.UTF_8; | 58 if (encoding == null) encoding = UTF8; |
| 58 | 59 |
| 59 // TODO(rnystrom): When #6284 is fixed, just use that. Until then, only | 60 return new Future.value(encoding.decode(_contents)); |
| 60 // UTF-8 is supported. :( | |
| 61 if (encoding != Encoding.UTF_8) { | |
| 62 throw new UnsupportedError( | |
| 63 "${encoding.name} is not a supported encoding."); | |
| 64 } | |
| 65 | |
| 66 return new Future.value(decodeUtf8(_contents)); | |
| 67 } | 61 } |
| 68 | 62 |
| 69 Stream<List<int>> read() => new Future<List<int>>.value(_contents).asStream(); | 63 Stream<List<int>> read() => new Future<List<int>>.value(_contents).asStream(); |
| 70 | 64 |
| 71 String toString() { | 65 String toString() { |
| 72 var buffer = new StringBuffer(); | 66 var buffer = new StringBuffer(); |
| 73 buffer.write("Bytes ["); | 67 buffer.write("Bytes ["); |
| 74 | 68 |
| 75 // Don't show the whole list if it's long. | 69 // Don't show the whole list if it's long. |
| 76 if (_contents.length > 11) { | 70 if (_contents.length > 11) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 97 } | 91 } |
| 98 } | 92 } |
| 99 | 93 |
| 100 /// An asset backed by a file on the local file system. | 94 /// An asset backed by a file on the local file system. |
| 101 class _FileAsset extends Asset { | 95 class _FileAsset extends Asset { |
| 102 final File _file; | 96 final File _file; |
| 103 _FileAsset(AssetId id, this._file) | 97 _FileAsset(AssetId id, this._file) |
| 104 : super(id); | 98 : super(id); |
| 105 | 99 |
| 106 Future<String> readAsString({Encoding encoding}) { | 100 Future<String> readAsString({Encoding encoding}) { |
| 107 if (encoding == null) encoding = Encoding.UTF_8; | 101 if (encoding == null) encoding = UTF8; |
| 108 return _file.readAsString(encoding: encoding); | 102 return _file.readAsString(encoding: encoding); |
| 109 } | 103 } |
| 110 | 104 |
| 111 Stream<List<int>> read() => _file.openRead(); | 105 Stream<List<int>> read() => _file.openRead(); |
| 112 | 106 |
| 113 String toString() => 'File "${_file.path}"'; | 107 String toString() => 'File "${_file.path}"'; |
| 114 } | 108 } |
| 115 | 109 |
| 116 /// An asset whose data is stored in a string. | 110 /// An asset whose data is stored in a string. |
| 117 class _StringAsset extends Asset { | 111 class _StringAsset extends Asset { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 139 } | 133 } |
| 140 | 134 |
| 141 String _escape(String string) { | 135 String _escape(String string) { |
| 142 return string | 136 return string |
| 143 .replaceAll("\"", r'\"') | 137 .replaceAll("\"", r'\"') |
| 144 .replaceAll("\n", r"\n") | 138 .replaceAll("\n", r"\n") |
| 145 .replaceAll("\r", r"\r") | 139 .replaceAll("\r", r"\r") |
| 146 .replaceAll("\t", r"\t"); | 140 .replaceAll("\t", r"\t"); |
| 147 } | 141 } |
| 148 } | 142 } |
| OLD | NEW |