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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 _currentBlock = null; | 168 _currentBlock = null; |
169 } | 169 } |
170 // Resume reading unless we are already done. | 170 // Resume reading unless we are already done. |
171 if (_openedFile != null) _readBlock(); | 171 if (_openedFile != null) _readBlock(); |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 class _FileStreamConsumer extends StreamConsumer<List<int>> { | 175 class _FileStreamConsumer extends StreamConsumer<List<int>> { |
176 File _file; | 176 File _file; |
177 Future<RandomAccessFile> _openFuture; | 177 Future<RandomAccessFile> _openFuture; |
178 StreamSubscription _subscription; | |
179 | 178 |
180 _FileStreamConsumer(File this._file, FileMode mode) { | 179 _FileStreamConsumer(File this._file, FileMode mode) { |
181 _openFuture = _file.open(mode: mode); | 180 _openFuture = _file.open(mode: mode); |
182 } | 181 } |
183 | 182 |
184 Future<File> addStream(Stream<List<int>> stream) { | 183 Future<File> addStream(Stream<List<int>> stream) { |
185 Completer<File> completer = new Completer<File>(); | 184 Completer<File> completer = new Completer<File>.sync(); |
186 _openFuture | 185 _openFuture |
187 .then((openedFile) { | 186 .then((openedFile) { |
187 var _subscription; | |
188 void error(e, [StackTrace stackTrace]) { | 188 void error(e, [StackTrace stackTrace]) { |
189 _subscription.cancel(); | 189 _subscription.cancel(); |
190 openedFile.close(); | 190 openedFile.close(); |
191 completer.completeError(e, stackTrace); | 191 completer.completeError(e, stackTrace); |
192 } | 192 } |
193 _subscription = stream.listen( | 193 _subscription = stream.listen( |
194 (d) { | 194 (d) { |
195 _subscription.pause(); | 195 _subscription.pause(); |
196 try { | 196 try { |
197 openedFile.writeFrom(d, 0, d.length) | 197 openedFile.writeFrom(d, 0, d.length) |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
523 return _decodeLines(bytes, encoding); | 523 return _decodeLines(bytes, encoding); |
524 }); | 524 }); |
525 } | 525 } |
526 | 526 |
527 List<String> readAsLinesSync({Encoding encoding: UTF8}) => | 527 List<String> readAsLinesSync({Encoding encoding: UTF8}) => |
528 _decodeLines(readAsBytesSync(), encoding); | 528 _decodeLines(readAsBytesSync(), encoding); |
529 | 529 |
530 Future<File> writeAsBytes(List<int> bytes, | 530 Future<File> writeAsBytes(List<int> bytes, |
531 {FileMode mode: FileMode.WRITE, | 531 {FileMode mode: FileMode.WRITE, |
532 bool flush: false}) { | 532 bool flush: false}) { |
533 try { | 533 return open(mode: mode).then((file) { |
534 IOSink sink = openWrite(mode: mode); | 534 return file.writeFrom(bytes, 0, bytes.length) |
535 sink.add(bytes); | 535 .then((_) { |
536 if (flush) { | 536 if (flush) return file.flush().then((_) => this); |
537 sink.flush().then((_) => sink.close()); | 537 return this; |
538 } else { | 538 }) |
539 sink.close(); | 539 .whenComplete(file.close); |
540 } | 540 }); |
541 return sink.done.then((_) => this); | |
542 } catch (e) { | |
543 return new Future.error(e); | |
544 } | |
545 } | 541 } |
546 | 542 |
547 void writeAsBytesSync(List<int> bytes, | 543 void writeAsBytesSync(List<int> bytes, |
548 {FileMode mode: FileMode.WRITE, | 544 {FileMode mode: FileMode.WRITE, |
549 bool flush: false}) { | 545 bool flush: false}) { |
550 RandomAccessFile opened = openSync(mode: mode); | 546 RandomAccessFile opened = openSync(mode: mode); |
551 opened.writeFromSync(bytes, 0, bytes.length); | 547 opened.writeFromSync(bytes, 0, bytes.length); |
552 if (flush) opened.flushSync(); | 548 if (flush) opened.flushSync(); |
553 opened.closeSync(); | 549 opened.closeSync(); |
554 } | 550 } |
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
916 } | 912 } |
917 _asyncDispatched = true; | 913 _asyncDispatched = true; |
918 return _IOService.dispatch(request, data) | 914 return _IOService.dispatch(request, data) |
919 .whenComplete(() { | 915 .whenComplete(() { |
920 _asyncDispatched = false; | 916 _asyncDispatched = false; |
921 }); | 917 }); |
922 } | 918 } |
923 | 919 |
924 void _checkAvailable() { | 920 void _checkAvailable() { |
925 if (_asyncDispatched) { | 921 if (_asyncDispatched) { |
926 throw new FileSystemException("An async operation is currently pending", p ath); | 922 throw new FileSystemException("An async operation is currently pending", p ath); |
Søren Gjesse
2014/04/01 14:44:05
Not your change, but long line :-).
Anders Johnsen
2014/04/02 08:21:01
Pretty sure it originally was my change :)
| |
927 } | 923 } |
928 if (closed) { | 924 if (closed) { |
929 throw new FileSystemException("File closed", path); | 925 throw new FileSystemException("File closed", path); |
930 } | 926 } |
931 } | 927 } |
932 } | 928 } |
OLD | NEW |