| 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 class _FileInputStream extends _BaseDataInputStream implements InputStream { | 5 class _FileInputStream extends _BaseDataInputStream implements InputStream { |
| 6 _FileInputStream(String name) | 6 _FileInputStream(String name) |
| 7 : _data = const [], | 7 : _data = const [], |
| 8 _position = 0, | 8 _position = 0, |
| 9 _filePosition = 0 { | 9 _filePosition = 0 { |
| 10 var file = new File(name); | 10 var file = new File(name); |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 void flush() { | 210 void flush() { |
| 211 if (_file == null) { | 211 if (_file == null) { |
| 212 _pendingOperations.add(_PendingOperation.FLUSH); | 212 _pendingOperations.add(_PendingOperation.FLUSH); |
| 213 } else { | 213 } else { |
| 214 _file.flush().then((ignored) => null); | 214 _file.flush().then((ignored) => null); |
| 215 } | 215 } |
| 216 } | 216 } |
| 217 | 217 |
| 218 | 218 |
| 219 void close() { | 219 void close() { |
| 220 _streamMarkedClosed = true; |
| 220 if (_file == null) { | 221 if (_file == null) { |
| 221 _pendingOperations.add(_PendingOperation.CLOSE); | 222 _pendingOperations.add(_PendingOperation.CLOSE); |
| 222 } else if (!_streamMarkedClosed) { | 223 } else if (!_closeCallbackScheduled) { |
| 223 _file.close().then((ignore) { | 224 _file.close().then((ignore) { |
| 224 if (_onClosed != null) _onClosed(); | 225 if (_onClosed != null) _onClosed(); |
| 225 }); | 226 }); |
| 226 _streamMarkedClosed = true; | 227 _closeCallbackScheduled = true; |
| 227 } | 228 } |
| 228 } | 229 } |
| 229 | 230 |
| 230 void set onNoPendingWrites(void callback()) { | 231 void set onNoPendingWrites(void callback()) { |
| 231 _onNoPendingWrites = callback; | 232 _onNoPendingWrites = callback; |
| 232 if (((_pendingOperations == null) || (_pendingOperations.length == 0)) && | 233 if ((_pendingOperations == null || _pendingOperations.length == 0) && |
| 233 (outstandingWrites == 0) && | 234 outstandingWrites == 0 && |
| 234 !_streamMarkedClosed && | 235 !_streamMarkedClosed && |
| 235 (_onNoPendingWrites != null)) { | 236 _onNoPendingWrites != null) { |
| 236 new Timer(0, (t) { | 237 new Timer(0, (t) { |
| 237 if (_onNoPendingWrites != null) { | 238 if (_onNoPendingWrites != null) { |
| 238 _onNoPendingWrites(); | 239 _onNoPendingWrites(); |
| 239 } | 240 } |
| 240 }); | 241 }); |
| 241 } | 242 } |
| 242 } | 243 } |
| 243 | 244 |
| 244 void set onClosed(void callback()) { | 245 void set onClosed(void callback()) { |
| 245 _onClosed = callback; | 246 _onClosed = callback; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 259 } | 260 } |
| 260 }); | 261 }); |
| 261 _pendingOperations = null; | 262 _pendingOperations = null; |
| 262 } | 263 } |
| 263 | 264 |
| 264 void _write(List<int> buffer, int offset, int len) { | 265 void _write(List<int> buffer, int offset, int len) { |
| 265 outstandingWrites++; | 266 outstandingWrites++; |
| 266 var writeListFuture = _file.writeList(buffer, offset, len); | 267 var writeListFuture = _file.writeList(buffer, offset, len); |
| 267 writeListFuture.then((ignore) { | 268 writeListFuture.then((ignore) { |
| 268 outstandingWrites--; | 269 outstandingWrites--; |
| 269 if ((outstandingWrites == 0) && | 270 if (outstandingWrites == 0 && |
| 270 !_streamMarkedClosed && | 271 !_streamMarkedClosed && |
| 271 (_onNoPendingWrites != null)) { | 272 _onNoPendingWrites != null) { |
| 272 _onNoPendingWrites(); | 273 _onNoPendingWrites(); |
| 273 } | 274 } |
| 274 }); | 275 }); |
| 275 writeListFuture.handleException((e) { | 276 writeListFuture.handleException((e) { |
| 276 outstandingWrites--; | 277 outstandingWrites--; |
| 277 _reportError(e); | 278 _reportError(e); |
| 278 return true; | 279 return true; |
| 279 }); | 280 }); |
| 280 } | 281 } |
| 281 | 282 |
| 283 bool get closed() => _streamMarkedClosed; |
| 284 |
| 282 RandomAccessFile _file; | 285 RandomAccessFile _file; |
| 283 | 286 |
| 284 // When this is set to true the stream is marked closed. When a | 287 // When this is set to true the stream is marked closed. When a |
| 285 // stream is marked closed no more data can be written. | 288 // stream is marked closed no more data can be written. |
| 286 bool _streamMarkedClosed = false; | 289 bool _streamMarkedClosed = false; |
| 287 | 290 |
| 288 // When this is set to true the close callback has been called and | 291 // When this is set to true, the close callback has been scheduled and the |
| 289 // the stream is fully closed. | 292 // stream will be fully closed once it's called. |
| 290 bool _closeCallbackCalled = false; | 293 bool _closeCallbackScheduled = false; |
| 291 | 294 |
| 292 // Number of writes that have not yet completed. | 295 // Number of writes that have not yet completed. |
| 293 int outstandingWrites = 0; | 296 int outstandingWrites = 0; |
| 294 | 297 |
| 295 // List of pending writes that were issued before the underlying | 298 // List of pending writes that were issued before the underlying |
| 296 // file was successfully opened. | 299 // file was successfully opened. |
| 297 List _pendingOperations; | 300 List _pendingOperations; |
| 298 | 301 |
| 299 Function _onNoPendingWrites; | 302 Function _onNoPendingWrites; |
| 300 Function _onClosed; | 303 Function _onClosed; |
| (...skipping 857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1158 new FileIOException("File closed '$_name'")); | 1161 new FileIOException("File closed '$_name'")); |
| 1159 }); | 1162 }); |
| 1160 return completer.future; | 1163 return completer.future; |
| 1161 } | 1164 } |
| 1162 | 1165 |
| 1163 final String _name; | 1166 final String _name; |
| 1164 int _id; | 1167 int _id; |
| 1165 | 1168 |
| 1166 SendPort _fileService; | 1169 SendPort _fileService; |
| 1167 } | 1170 } |
| OLD | NEW |