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

Unified Diff: pkg/http/lib/src/byte_stream.dart

Issue 11825010: Update pkg/http to use the new async APIs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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
Index: pkg/http/lib/src/byte_stream.dart
diff --git a/pkg/http/lib/src/byte_stream.dart b/pkg/http/lib/src/byte_stream.dart
new file mode 100644
index 0000000000000000000000000000000000000000..a8efdd335a76dd04928d06468f16a57fe9a24712
--- /dev/null
+++ b/pkg/http/lib/src/byte_stream.dart
@@ -0,0 +1,39 @@
+// Copyright (c) 2013, 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.
+
+library byte_stream;
+
+import 'dart:async';
+import 'dart:io';
+import 'dart:scalarlist';
+
+import 'utils.dart';
+
+/// A stream of bytes representing a single chunked piece of data.
Bob Nystrom 2013/01/08 23:50:49 Clarify that it's a stream of chunks of bytes, and
nweiz 2013/01/09 00:52:11 Done.
+class ByteStream extends StreamView<List<int>> {
+ static var _id = 0;
+ var id = _id++;
Bob Nystrom 2013/01/08 23:50:49 Debug code?
nweiz 2013/01/09 00:52:11 Oops, removed.
+
+ ByteStream(Stream<List<int>> stream)
+ : super(stream);
+
+ /// Returns a single-subscription byte stream that will emit the given bytes
+ /// in a single chunk.
+ factory ByteStream.fromBytes(List<int> bytes) =>
+ new ByteStream(streamFromIterable([bytes]));
+
+ /// Collects the data of this stream in a [Uint8List].
+ Future<Uint8List> toBytes() {
+ /// TODO(nweiz): use BufferList when issue 6409 is fixed.
+ return reduce(<int>[], (buffer, chunk) {
+ buffer.addAll(chunk);
+ return buffer;
+ }).then(toUint8List);
+ }
+
+ /// Collect the data of this stream in a [String], decoded according to
+ /// [encoding], which defaults to `Encoding.UTF_8`.
+ Future<String> bytesToString([Encoding encoding=Encoding.UTF_8]) =>
+ toBytes().then((bytes) => decodeString(bytes, encoding));
+}

Powered by Google App Engine
This is Rietveld 408576698