| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 // Read the file in blocks of size 64k. | 7 // Read the file in blocks of size 64k. |
| 8 const int _BLOCK_SIZE = 64 * 1024; | 8 const int _BLOCK_SIZE = 64 * 1024; |
| 9 | 9 |
| 10 | 10 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 void onDone(), | 40 void onDone(), |
| 41 bool cancelOnError}) { | 41 bool cancelOnError}) { |
| 42 return _controller.stream.listen(onData, | 42 return _controller.stream.listen(onData, |
| 43 onError: onError, | 43 onError: onError, |
| 44 onDone: onDone, | 44 onDone: onDone, |
| 45 cancelOnError: cancelOnError); | 45 cancelOnError: cancelOnError); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void _setupController() { | 48 void _setupController() { |
| 49 _controller = new StreamController<List<int>>( | 49 _controller = new StreamController<List<int>>( |
| 50 onSubscriptionStateChange: _onSubscriptionStateChange, | 50 onListen: _start, |
| 51 onPauseStateChange: _onPauseStateChange); | 51 onPause: () => _paused = true, |
| 52 onResume: _resume, |
| 53 onCancel: () { |
| 54 _unsubscribed = true; |
| 55 _closeFile(); |
| 56 }); |
| 52 } | 57 } |
| 53 | 58 |
| 54 Future _closeFile() { | 59 Future _closeFile() { |
| 55 Future closeFuture; | 60 Future closeFuture; |
| 56 if (_openedFile != null) { | 61 if (_openedFile != null) { |
| 57 Future closeFuture = _openedFile.close(); | 62 Future closeFuture = _openedFile.close(); |
| 58 _openedFile = null; | 63 _openedFile = null; |
| 59 return closeFuture; | 64 return closeFuture; |
| 60 } else { | 65 } else { |
| 61 return new Future.immediate(null); | 66 return new Future.immediate(null); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 | 126 |
| 122 void _resume() { | 127 void _resume() { |
| 123 _paused = false; | 128 _paused = false; |
| 124 if (_currentBlock != null) { | 129 if (_currentBlock != null) { |
| 125 _controller.add(_currentBlock); | 130 _controller.add(_currentBlock); |
| 126 _currentBlock = null; | 131 _currentBlock = null; |
| 127 } | 132 } |
| 128 // Resume reading unless we are already done. | 133 // Resume reading unless we are already done. |
| 129 if (_openedFile != null) _readBlock(); | 134 if (_openedFile != null) _readBlock(); |
| 130 } | 135 } |
| 131 | |
| 132 void _onSubscriptionStateChange() { | |
| 133 if (_controller.hasListener) { | |
| 134 _start(); | |
| 135 } else { | |
| 136 _unsubscribed = true; | |
| 137 _closeFile(); | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 void _onPauseStateChange() { | |
| 142 if (_controller.isPaused) { | |
| 143 _paused = true; | |
| 144 } else { | |
| 145 _resume(); | |
| 146 } | |
| 147 } | |
| 148 } | 136 } |
| 149 | 137 |
| 150 class _FileStreamConsumer extends StreamConsumer<List<int>> { | 138 class _FileStreamConsumer extends StreamConsumer<List<int>> { |
| 151 File _file; | 139 File _file; |
| 152 Future<RandomAccessFile> _openFuture; | 140 Future<RandomAccessFile> _openFuture; |
| 153 StreamSubscription _subscription; | 141 StreamSubscription _subscription; |
| 154 | 142 |
| 155 _FileStreamConsumer(File this._file, FileMode mode) { | 143 _FileStreamConsumer(File this._file, FileMode mode) { |
| 156 _openFuture = _file.open(mode: mode); | 144 _openFuture = _file.open(mode: mode); |
| 157 } | 145 } |
| (...skipping 892 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1050 new FileIOException("File closed '$_path'")); | 1038 new FileIOException("File closed '$_path'")); |
| 1051 }); | 1039 }); |
| 1052 return completer.future; | 1040 return completer.future; |
| 1053 } | 1041 } |
| 1054 | 1042 |
| 1055 final String _path; | 1043 final String _path; |
| 1056 int _id; | 1044 int _id; |
| 1057 | 1045 |
| 1058 SendPort _fileService; | 1046 SendPort _fileService; |
| 1059 } | 1047 } |
| OLD | NEW |