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

Unified Diff: mojo/public/dart/src/drain_data.dart

Issue 816113004: Dart: Adds a content handler and a test. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Address comments Created 5 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: mojo/public/dart/src/drain_data.dart
diff --git a/mojo/public/dart/src/drain_data.dart b/mojo/public/dart/src/drain_data.dart
new file mode 100644
index 0000000000000000000000000000000000000000..ffc56e5629434d3f5dc47817f7426422a30cf290
--- /dev/null
+++ b/mojo/public/dart/src/drain_data.dart
@@ -0,0 +1,68 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+part of core;
+
+class DataPipeDrainer {
+ MojoDataPipeConsumer _consumer;
+ MojoEventStream _eventStream;
+ List<ByteData> _dataList;
+ int _dataSize;
+
+ DataPipeDrainer(this._consumer) {
+ _eventStream = new MojoEventStream(_consumer.handle);
+ _dataList = new List();
+ _dataSize = 0;
+ }
+
+ MojoResult _doRead() {
+ ByteData thisRead = _consumer.beginRead();
+ if (thisRead == null) {
+ throw 'Data pipe beginRead failed: ${_consumer.status}';
+ }
+ _dataList.add(thisRead);
+ _dataSize += thisRead.lengthInBytes;
+ return _consumer.endRead(thisRead.lengthInBytes);
+ }
+
+ ByteData _concatData() {
+ var data = new ByteData(_dataSize);
+ int end = 0;
+ for (var chunk in _dataList) {
+ data.buffer.asUint8List().setRange(
+ end, end + chunk.lengthInBytes, chunk.buffer.asUint8List());
+ end += chunk.lengthInBytes;
+ }
+ return data;
+ }
+
+ Future<ByteData> drain() {
+ var completer = new Completer();
+ _eventStream.listen((List<int> event) {
+ var mojoSignals = new MojoHandleSignals(event[1]);
+ if (mojoSignals.isReadable) {
+ var result = _doRead();
+ if (!result.isOk) {
+ _eventStream.close();
+ _eventStream = null;
+ completer.complete(_concatData());
+ } else {
+ _eventStream.enableReadEvents();
+ }
+ } else if (mojoSignals.isPeerClosed) {
+ _eventStream.close();
+ _eventStream = null;
+ completer.complete(_concatData());
+ } else {
+ throw 'Unexpected handle event: $mojoSignals';
+ }
+ });
+ return completer.future;
+ }
+
+ static Future<ByteData> drainHandle(MojoDataPipeConsumer consumer) {
+ var drainer = new DataPipeDrainer(consumer);
+ return drainer.drain();
+ }
+}
« no previous file with comments | « mojo/public/dart/core.dart ('k') | mojo/public/tools/bindings/generators/dart_templates/interface_definition.tmpl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698