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

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

Issue 1132063007: Rationalize Dart mojo and sky package structure (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 7 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
« no previous file with comments | « mojo/public/dart/src/data_pipe.dart ('k') | mojo/public/dart/src/event_stream.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 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 ByteData _copy(ByteData byteData) =>
20 new ByteData.view(
21 new Uint8List.fromList(byteData.buffer.asUint8List()).buffer);
22
23 MojoResult _doRead() {
24 ByteData thisRead = _consumer.beginRead();
25 if (thisRead == null) {
26 throw 'Data pipe beginRead failed: ${_consumer.status}';
27 }
28 _dataList.add(_copy(thisRead));
29 _dataSize += thisRead.lengthInBytes;
30 return _consumer.endRead(thisRead.lengthInBytes);
31 }
32
33 ByteData _concatData() {
34 var data = new ByteData(_dataSize);
35 int end = 0;
36 for (var chunk in _dataList) {
37 data.buffer.asUint8List().setRange(
38 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 _eventStream.listen((List<int> event) {
47 var mojoSignals = new MojoHandleSignals(event[1]);
48 if (mojoSignals.isReadable) {
49 var result = _doRead();
50 if (!result.isOk) {
51 _eventStream.close();
52 _eventStream = null;
53 completer.complete(_concatData());
54 } else {
55 _eventStream.enableReadEvents();
56 }
57 } else if (mojoSignals.isPeerClosed) {
58 _eventStream.close();
59 _eventStream = 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/src/data_pipe.dart ('k') | mojo/public/dart/src/event_stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698