| 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..05a2202dfa612aa62c94d3d57b82f2941bd83ef1 100644
|
| --- a/pkg/barback/lib/src/asset.dart
|
| +++ b/pkg/barback/lib/src/asset.dart
|
| @@ -6,8 +6,10 @@ library barback.asset;
|
|
|
| import 'dart:async';
|
| import 'dart:io';
|
| +import 'dart:utf';
|
|
|
| import 'asset_id.dart';
|
| +import 'utils.dart';
|
|
|
| /// A blob of content.
|
| ///
|
| @@ -19,39 +21,80 @@ 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);
|
|
|
| - // 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;
|
| + Future<String> readAsString({Encoding encoding}) {
|
| + if (encoding == null) encoding = Encoding.UTF_8;
|
| +
|
| + // 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 [");
|
| +
|
| + // Don't show the whole list if it's long.
|
| + if (_contents.length > 11) {
|
| + for (var i = 0; i < 5; i++) {
|
| + buffer.write(byteToHex(_contents[i]));
|
| + buffer.write(" ");
|
| + }
|
| +
|
| + buffer.write("...");
|
| +
|
| + for (var i = _contents.length - 5; i < _contents.length; i++) {
|
| + buffer.write(" ");
|
| + buffer.write(byteToHex(_contents[i]));
|
| + }
|
| + } else {
|
| + for (var i = 0; i < _contents.length; i++) {
|
| + if (i > 0) buffer.write(" ");
|
| + buffer.write(byteToHex(_contents[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 +103,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 +138,6 @@ class _StringAsset extends Asset {
|
| return 'String "$contents"';
|
| }
|
|
|
| - Object serialize() => ["string", id.serialize(), _contents];
|
| -
|
| String _escape(String string) {
|
| return string
|
| .replaceAll("\"", r'\"')
|
|
|