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

Side by Side Diff: runtime/bin/file_impl.dart

Issue 10832188: Add an OutputStream.closed getter. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 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 | Annotate | Revision Log
OLDNEW
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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 } 207 }
208 208
209 209
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
Bill Hesse 2012/08/08 14:21:11 This will all need some work if we want writes aft
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 (!_closeCallbackCalled) {
Bill Hesse 2012/08/08 14:21:11 Call this _closeCallbackScheduled.
nweiz 2012/08/09 19:39:52 Done.
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 _closeCallbackCalled = 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) {
(...skipping 24 matching lines...) Expand all
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)) {
Bill Hesse 2012/08/08 14:21:11 Extra parentheses.
nweiz 2012/08/09 19:39:52 Done.
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 called and
Bill Hesse 2012/08/08 14:21:11 Change the comment and the name to closeCallbackSc
nweiz 2012/08/09 19:39:52 Done.
289 // the stream is fully closed. 292 // the stream is fully closed.
290 bool _closeCallbackCalled = false; 293 bool _closeCallbackCalled = 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
(...skipping 859 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698