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

Side by Side 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: comment 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 part of core;
6
7 class DataPipeDrainer {
abarth-chromium 2015/01/18 22:19:01 We can resolve this in a later CL, but we should a
zra 2015/01/20 17:36:51 Acknowledged.
8 MojoDataPipeConsumer _consumer;
9 MojoEventStream _eventStream;
10 List<ByteData> _dataList;
11 int _dataSize;
12
13 DataPipeDrainer(this._consumer) {
14 _eventStream = new MojoEventStream(_consumer.handle);
15 _dataList = new List();
16 _dataSize = 0;
17 }
18
19 MojoResult _doRead() {
20 ByteData thisRead = _consumer.beginRead();
21 if (thisRead == null) {
22 throw 'Data pipe beginRead failed: ${_consumer.status}';
23 }
24 _dataList.add(thisRead);
25 _dataSize += thisRead.lengthInBytes;
26 return _consumer.endRead(thisRead.lengthInBytes);
27 }
28
29 ByteData _concatData() {
30 var data = new ByteData(_dataSize);
31 int end = 0;
32 for (var chunk in _dataList) {
33 data.buffer.asUint8List().setRange(
34 end, end + chunk.lengthInBytes, chunk.buffer.asUint8List());
35 end += chunk.lengthInBytes;
36 }
37 return data;
38 }
39
40 Future<ByteData> drain() {
41 var completer = new Completer();
42 _eventStream.listen((List<int> event) {
43 var mojoSignals = new MojoHandleSignals(event[1]);
44 if (mojoSignals.isReadable) {
45 var result = _doRead();
46 if (!result.isOk) {
47 _eventStream.close();
48 _eventStream = null;
49 completer.complete(_concatData());
50 } else {
51 _eventStream.enableReadEvents();
52 }
53 } else if (mojoSignals.isPeerClosed) {
54 _eventStream.close();
55 _eventStream = null;
56 completer.complete(_concatData());
57 } else {
58 throw 'Unexpected handle event: $mojoSignals';
59 }
60 });
61 return completer.future;
62 }
63
64 static Future<ByteData> drainHandle(MojoDataPipeConsumer consumer) {
65 var drainer = new DataPipeDrainer(consumer);
66 return drainer.drain();
67 }
68 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698