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

Unified Diff: lib/src/byte_collector.dart

Issue 2649233006: Add `byteCollector` stream transformer and `collectBytes` function. (Closed)
Patch Set: Address comments. Created 3 years, 11 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 | « lib/async.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/byte_collector.dart
diff --git a/lib/src/byte_collector.dart b/lib/src/byte_collector.dart
new file mode 100644
index 0000000000000000000000000000000000000000..3b4f075034f853275ab587434e1147568c3b47e2
--- /dev/null
+++ b/lib/src/byte_collector.dart
@@ -0,0 +1,42 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import "dart:async";
+import "dart:typed_data";
+
+/// Collects an asynchronous sequence of byte lists into a single list of bytes.
+///
+/// If the [source] stream emits an error event,
+/// the collection fails and the returned future completes with the same error.
+///
+/// If any of the input data are not valid bytes, they will be truncated to
+/// an eight-bit unsigned value in the resulting list.
+Future<Uint8List> collectBytes(Stream<List<int>> source) {
+ var byteLists = List<List<int>>[];
+ var length = 0;
+ var completer = new Completer<Uint8List>.sync();
+ source.listen(
+ (bytes) {
+ byteLists.add(bytes);
+ length += bytes.length;
+ },
+ onError: completer.completeError,
+ onDone: () {
+ completer.complete(_collect(length, byteLists));
+ },
+ cancelOnError: true);
+ return completer.future;
+}
+
+// Join a lists of bytes with a known total length into a single [Uint8List].
+Uint8List _collect(int length, List<List<int>> byteLists) {
+ var result = new Uint8List(length);
+ int i = 0;
+ for (var byteList in byteLists) {
+ var end = i + byteList.length;
+ result.setRange(i, end, byteList);
+ i = end;
+ }
+ return result;
+}
« no previous file with comments | « lib/async.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698