OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Helper functionality to make working with IO easier. | 5 /// Helper functionality to make working with IO easier. |
6 library io; | 6 library io; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 | 592 |
593 _OutputStreamConsumer(this._outputStream); | 593 _OutputStreamConsumer(this._outputStream); |
594 | 594 |
595 Future consume(Stream<List<int>> stream) { | 595 Future consume(Stream<List<int>> stream) { |
596 // TODO(nweiz): we have to manually keep track of whether or not the | 596 // TODO(nweiz): we have to manually keep track of whether or not the |
597 // completer has completed since the output stream could signal an error | 597 // completer has completed since the output stream could signal an error |
598 // after close() has been called but before it has shut down internally. See | 598 // after close() has been called but before it has shut down internally. See |
599 // the following TODO. | 599 // the following TODO. |
600 var completed = false; | 600 var completed = false; |
601 var completer = new Completer(); | 601 var completer = new Completer(); |
602 stream.listen((data) => _outputStream.write(data), onDone: () { | 602 stream.listen((data) { |
| 603 // Writing empty data to a closed stream can cause errors. |
| 604 if (data.isEmpty) return; |
| 605 |
| 606 // TODO(nweiz): remove this try/catch when issue 7836 is fixed. |
| 607 try { |
| 608 _outputStream.write(data); |
| 609 } catch (e, stack) { |
| 610 if (!completed) completer.completeError(e, stack); |
| 611 completed = true; |
| 612 } |
| 613 }, onDone: () { |
603 _outputStream.close(); | 614 _outputStream.close(); |
604 // TODO(nweiz): wait until _outputStream.onClosed is called once issue | 615 // TODO(nweiz): wait until _outputStream.onClosed is called once issue |
605 // 7761 is fixed. | 616 // 7761 is fixed. |
606 if (!completed) completer.complete(null); | 617 if (!completed) completer.complete(null); |
607 completed = true; | 618 completed = true; |
608 }); | 619 }); |
609 | 620 |
610 _outputStream.onError = (e) { | 621 _outputStream.onError = (e) { |
611 if (!completed) completer.completeError(e); | 622 if (!completed) completer.completeError(e); |
612 completed = true; | 623 completed = true; |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1017 Directory _getDirectory(entry) { | 1028 Directory _getDirectory(entry) { |
1018 if (entry is Directory) return entry; | 1029 if (entry is Directory) return entry; |
1019 return new Directory(entry); | 1030 return new Directory(entry); |
1020 } | 1031 } |
1021 | 1032 |
1022 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 1033 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
1023 Uri _getUri(uri) { | 1034 Uri _getUri(uri) { |
1024 if (uri is Uri) return uri; | 1035 if (uri is Uri) return uri; |
1025 return new Uri.fromString(uri); | 1036 return new Uri.fromString(uri); |
1026 } | 1037 } |
OLD | NEW |