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 unsubscribeOnError}) { | 41 bool unsubscribeOnError}) { |
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 unsubscribeOnError: unsubscribeOnError); | 45 unsubscribeOnError: unsubscribeOnError); |
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 896 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1054 new FileIOException("File closed '$_path'")); | 1042 new FileIOException("File closed '$_path'")); |
1055 }); | 1043 }); |
1056 return completer.future; | 1044 return completer.future; |
1057 } | 1045 } |
1058 | 1046 |
1059 final String _path; | 1047 final String _path; |
1060 int _id; | 1048 int _id; |
1061 | 1049 |
1062 SendPort _fileService; | 1050 SendPort _fileService; |
1063 } | 1051 } |
OLD | NEW |