Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Unified Diff: pkg/barback/lib/src/asset.dart

Issue 23469003: Add an AssetStream class to Barback. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/barback/lib/src/package_graph.dart » ('j') | pkg/barback/lib/src/stream_replayer.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/barback/lib/src/asset.dart
diff --git a/pkg/barback/lib/src/asset.dart b/pkg/barback/lib/src/asset.dart
index db7a5ea2ca8c0788511ee1eed80f8c504faaab72..19bad388f487768e3ef0872291c7b6b78faee706 100644
--- a/pkg/barback/lib/src/asset.dart
+++ b/pkg/barback/lib/src/asset.dart
@@ -10,6 +10,7 @@ import 'dart:io';
import 'dart:utf';
import 'asset_id.dart';
+import 'stream_replayer.dart';
import 'utils.dart';
/// A blob of content.
@@ -34,6 +35,12 @@ abstract class Asset {
factory Asset.fromPath(AssetId id, String path) =>
new _FileAsset(id, new File(path));
+ /// Creates an asset from a stream.
+ ///
+ /// This immediately starts draining [stream].
+ factory Asset.fromStream(AssetId id, Stream<List<int>> stream) =>
+ new _StreamAsset(id, stream);
+
/// Returns the contents of the asset as a string.
///
/// If the asset was created from a [String] the original string is always
@@ -140,3 +147,24 @@ class _StringAsset extends Asset {
.replaceAll("\t", r"\t");
}
}
+
+/// An asset whose data is available from a stream.
+class _StreamAsset extends Asset {
+ /// A stream replayer that records and replays the contents of the input
+ /// stream.
+ final StreamReplayer<List<int>> _replayer;
+
+ _StreamAsset(AssetId id, Stream<List<int>> stream)
+ : _replayer = new StreamReplayer(stream),
+ super(id);
+
+ Future<String> readAsString({Encoding encoding}) {
+ if (encoding == null) encoding = UTF8;
+ return _replayer.getReplay().toList()
+ .then((chunks) => encoding.decode(flatten(chunks)));
+ }
+
+ Stream<List<int>> read() => _replayer.getReplay();
+
+ String toString() => "Stream";
+}
« no previous file with comments | « no previous file | pkg/barback/lib/src/package_graph.dart » ('j') | pkg/barback/lib/src/stream_replayer.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698