Chromium Code Reviews| Index: pkg/barback/lib/src/asset.dart |
| diff --git a/pkg/barback/lib/src/asset.dart b/pkg/barback/lib/src/asset.dart |
| index 83def7408156e78863f86359befafeaf503b80f0..9c48bdc83922ec605b5cbc6b4540d9b9fc123933 100644 |
| --- a/pkg/barback/lib/src/asset.dart |
| +++ b/pkg/barback/lib/src/asset.dart |
| @@ -6,6 +6,7 @@ library barback.asset; |
| import 'dart:async'; |
| import 'dart:io'; |
| +import 'dart:utf'; |
| import 'asset_id.dart'; |
| @@ -19,39 +20,87 @@ abstract class Asset { |
| Asset(this.id); |
| - factory Asset.fromFile(AssetId id, File file) { |
| - return new _FileAsset(id, file); |
| - } |
| + factory Asset.fromBytes(AssetId id, List<int> bytes) => |
| + new _BinaryAsset(id, bytes); |
| - factory Asset.fromString(AssetId id, String content) { |
| - return new _StringAsset(id, content); |
| - } |
| + factory Asset.fromFile(AssetId id, File file) => |
| + new _FileAsset(id, file); |
| - factory Asset.fromPath(AssetId id, String path) { |
| - return new _FileAsset(id, new File(path)); |
| - } |
| + factory Asset.fromString(AssetId id, String content) => |
| + new _StringAsset(id, content); |
| + |
| + factory Asset.fromPath(AssetId id, String path) => |
| + new _FileAsset(id, new File(path)); |
| + |
| + /// Returns the contents of the asset as a string. |
| + /// |
| + /// If the asset was created from a [String] the original string is always |
| + /// returned and [encoding] is ignored. Otherwise, the binary data of the |
| + /// asset is decoded using [encoding], which defaults to [Encoding.UTF_8]. |
| + Future<String> readAsString({Encoding encoding}); |
| + |
| + /// Streams the binary contents of the asset. |
| + /// |
| + /// If the asset was created from a [String], this returns its UTF-8 encoding. |
| + Stream<List<int>> read(); |
| +} |
| + |
| +/// An asset whose data is stored in a list of bytes. |
| +class _BinaryAsset extends Asset { |
| + final List<int> _contents; |
| + |
| + _BinaryAsset(AssetId id, this._contents) |
| + : super(id); |
| + |
| + Future<String> readAsString({Encoding encoding}) { |
| + if (encoding == null) encoding = Encoding.UTF_8; |
| - // TODO(rnystrom): This prevents users from defining their own |
| - // implementations of Asset. Use serialization package instead. |
| - factory Asset.deserialize(data) { |
| - // TODO(rnystrom): Handle errors. |
| - var id = new AssetId.parse(data[1]); |
| - switch (data[0]) { |
| - case "file": return new _FileAsset(id, new File(data[2])); break; |
| - case "string": return new _StringAsset(id, data[2]); break; |
| + // TODO(rnystrom): When #6284 is fixed, just use that. Until then, only |
| + // UTF-8 is supported. :( |
| + if (encoding != Encoding.UTF_8) { |
| + throw new UnsupportedError( |
| + "${encoding.name} is not a supported encoding."); |
| } |
| + |
| + return new Future.value(decodeUtf8(_contents)); |
| } |
| - /// Returns the contents of the asset as a string. |
| - // TODO(rnystrom): Figure out how binary assets should be handled. |
| - Future<String> readAsString(); |
| + Stream<List<int>> read() => new Future<List<int>>.value(_contents).asStream(); |
| - /// Streams the contents of the asset. |
| - Stream<List<int>> read(); |
| + String toString() { |
| + var buffer = new StringBuffer(); |
| + buffer.write("Bytes ["); |
| + |
| + writeByte(int i) { |
| + var byte = _contents[i]; |
| + var digits = "0123456789abcdef"; |
| + buffer.write(digits[(byte ~/ 16) % 16]); |
| + buffer.write(digits[byte % 16]); |
|
nweiz
2013/07/08 22:50:46
Add an intToHex function in utils.
Bob Nystrom
2013/07/08 23:26:04
Done.
|
| + } |
| + |
| + // Don't show the whole list if it's long. |
| + if (_contents.length > 10) { |
|
nweiz
2013/07/08 22:50:46
It's weird that an 11-byte buffer is abbreviated t
Bob Nystrom
2013/07/08 23:26:04
I suppose so, but I think this is tolerable balanc
nweiz
2013/07/09 02:00:01
All you have to do is change "> 10" to "> 11".
Bob Nystrom
2013/07/09 16:26:03
Done.
|
| + for (var i = 0; i < 5; i++) { |
| + writeByte(i); |
| + buffer.write(" "); |
| + } |
| + |
| + buffer.write("..."); |
| + |
| + for (var i = _contents.length - 5; i < _contents.length; i++) { |
| + buffer.write(" "); |
| + writeByte(i); |
| + } |
| + } else { |
| + for (var i = 0; i < _contents.length; i++) { |
| + if (i > 0) buffer.write(" "); |
| + writeByte(i); |
| + } |
| + } |
| - /// Serializes this [Asset] to an object that can be sent across isolates |
| - /// and passed to [deserialize]. |
| - Object serialize(); |
| + buffer.write("]"); |
| + return buffer.toString(); |
| + } |
| } |
| /// An asset backed by a file on the local file system. |
| @@ -60,26 +109,28 @@ class _FileAsset extends Asset { |
| _FileAsset(AssetId id, this._file) |
| : super(id); |
| - Future<String> readAsString() => _file.readAsString(); |
| + Future<String> readAsString({Encoding encoding}) { |
| + if (encoding == null) encoding = Encoding.UTF_8; |
| + return _file.readAsString(encoding: encoding); |
| + } |
| + |
| Stream<List<int>> read() => _file.openRead(); |
| String toString() => 'File "${_file.path}"'; |
| - |
| - Object serialize() => ["file", id.serialize(), _file.path]; |
| } |
| /// An asset whose data is stored in a string. |
| -// TODO(rnystrom): Have something similar for in-memory binary assets. |
| class _StringAsset extends Asset { |
| final String _contents; |
| _StringAsset(AssetId id, this._contents) |
| : super(id); |
| - Future<String> readAsString() => new Future.value(_contents); |
| + Future<String> readAsString({Encoding encoding}) => |
| + new Future.value(_contents); |
| - // TODO(rnystrom): Implement this and handle encoding. |
| - Stream<List<int>> read() => throw new UnimplementedError(); |
| + Stream<List<int>> read() => |
| + new Future<List<int>>.value(encodeUtf8(_contents)).asStream(); |
| String toString() { |
| // Don't show the whole string if it's long. |
| @@ -93,8 +144,6 @@ class _StringAsset extends Asset { |
| return 'String "$contents"'; |
| } |
| - Object serialize() => ["string", id.serialize(), _contents]; |
| - |
| String _escape(String string) { |
| return string |
| .replaceAll("\"", r'\"') |