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

Side by Side Diff: mojo/public/dart/mojo/lib/src/drain_data.dart

Issue 1441033002: Move mojo and mojom from mojo/public/dart to mojo/dart/packages (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 1 month 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
« no previous file with comments | « mojo/public/dart/mojo/lib/src/data_pipe.dart ('k') | mojo/public/dart/mojo/lib/src/enum.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 {
8 MojoDataPipeConsumer _consumer;
9 MojoEventSubscription _eventSubscription;
10 List<ByteData> _dataList;
11 int _dataSize;
12
13 DataPipeDrainer(this._consumer) {
14 _eventSubscription = new MojoEventSubscription(_consumer.handle);
15 _dataList = new List();
16 _dataSize = 0;
17 }
18
19 ByteData _copy(ByteData byteData) => new ByteData.view(
20 new Uint8List.fromList(byteData.buffer.asUint8List()).buffer);
21
22 MojoResult _doRead() {
23 ByteData thisRead = _consumer.beginRead();
24 if (thisRead == null) {
25 throw 'Data pipe beginRead failed: ${_consumer.status}';
26 }
27 _dataList.add(_copy(thisRead));
28 _dataSize += thisRead.lengthInBytes;
29 return _consumer.endRead(thisRead.lengthInBytes);
30 }
31
32 ByteData _concatData() {
33 var data = new ByteData(_dataSize);
34 int end = 0;
35 for (var chunk in _dataList) {
36 data.buffer
37 .asUint8List()
38 .setRange(end, end + chunk.lengthInBytes, chunk.buffer.asUint8List());
39 end += chunk.lengthInBytes;
40 }
41 return data;
42 }
43
44 Future<ByteData> drain() {
45 var completer = new Completer();
46 _eventSubscription.subscribe((List<int> event) {
47 var mojoSignals = new MojoHandleSignals(event[1]);
48 if (mojoSignals.isReadable) {
49 var result = _doRead();
50 if (!result.isOk) {
51 _eventSubscription.close();
52 _eventSubscription = null;
53 completer.complete(_concatData());
54 } else {
55 _eventSubscription.enableReadEvents();
56 }
57 } else if (mojoSignals.isPeerClosed) {
58 _eventSubscription.close();
59 _eventSubscription = null;
60 completer.complete(_concatData());
61 } else {
62 throw 'Unexpected handle event: $mojoSignals';
63 }
64 });
65 return completer.future;
66 }
67
68 static Future<ByteData> drainHandle(MojoDataPipeConsumer consumer) {
69 var drainer = new DataPipeDrainer(consumer);
70 return drainer.drain();
71 }
72 }
OLDNEW
« no previous file with comments | « mojo/public/dart/mojo/lib/src/data_pipe.dart ('k') | mojo/public/dart/mojo/lib/src/enum.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698