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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 }) | 189 }) |
190 .catchError((e) { | 190 .catchError((e) { |
191 completer.completeError(e); | 191 completer.completeError(e); |
192 }); | 192 }); |
193 return completer.future; | 193 return completer.future; |
194 } | 194 } |
195 | 195 |
196 Future<File> close() { | 196 Future<File> close() { |
197 return _openFuture.then((openedFile) => openedFile.close()); | 197 return _openFuture.then((openedFile) => openedFile.close()); |
198 } | 198 } |
| 199 |
| 200 Future flush() { |
| 201 return _openFuture |
| 202 .then((openedFile) => openedFile.flush()) |
| 203 .then((randomAccessfile) => new Future.value(null)); |
| 204 } |
199 } | 205 } |
200 | 206 |
201 | 207 |
202 const int _EXISTS_REQUEST = 0; | 208 const int _EXISTS_REQUEST = 0; |
203 const int _CREATE_REQUEST = 1; | 209 const int _CREATE_REQUEST = 1; |
204 const int _DELETE_REQUEST = 2; | 210 const int _DELETE_REQUEST = 2; |
205 const int _RENAME_REQUEST = 3; | 211 const int _RENAME_REQUEST = 3; |
206 const int _OPEN_REQUEST = 4; | 212 const int _OPEN_REQUEST = 4; |
207 const int _FULL_PATH_REQUEST = 5; | 213 const int _FULL_PATH_REQUEST = 5; |
208 const int _CLOSE_REQUEST = 6; | 214 const int _CLOSE_REQUEST = 6; |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 return readAsBytes().then((bytes) { | 539 return readAsBytes().then((bytes) { |
534 return _decodeLines(bytes, encoding); | 540 return _decodeLines(bytes, encoding); |
535 }); | 541 }); |
536 } | 542 } |
537 | 543 |
538 List<String> readAsLinesSync({Encoding encoding: Encoding.UTF_8}) { | 544 List<String> readAsLinesSync({Encoding encoding: Encoding.UTF_8}) { |
539 return _decodeLines(readAsBytesSync(), encoding); | 545 return _decodeLines(readAsBytesSync(), encoding); |
540 } | 546 } |
541 | 547 |
542 Future<File> writeAsBytes(List<int> bytes, | 548 Future<File> writeAsBytes(List<int> bytes, |
543 {FileMode mode: FileMode.WRITE}) { | 549 {FileMode mode: FileMode.WRITE, |
| 550 bool flush: false}) { |
544 try { | 551 try { |
545 IOSink sink = openWrite(mode: mode); | 552 IOSink sink = openWrite(mode: mode); |
546 sink.add(bytes); | 553 sink.add(bytes); |
547 sink.close(); | 554 if (flush) { |
| 555 sink.flush().then((_) => sink.close()); |
| 556 } else { |
| 557 sink.close(); |
| 558 } |
548 return sink.done.then((_) => this); | 559 return sink.done.then((_) => this); |
549 } catch (e) { | 560 } catch (e) { |
550 return new Future.error(e); | 561 return new Future.error(e); |
551 } | 562 } |
552 } | 563 } |
553 | 564 |
554 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { | 565 void writeAsBytesSync(List<int> bytes, |
| 566 {FileMode mode: FileMode.WRITE, |
| 567 bool flush: false}) { |
555 RandomAccessFile opened = openSync(mode: mode); | 568 RandomAccessFile opened = openSync(mode: mode); |
556 opened.writeFromSync(bytes, 0, bytes.length); | 569 opened.writeFromSync(bytes, 0, bytes.length); |
| 570 if (flush) opened.flushSync(); |
557 opened.closeSync(); | 571 opened.closeSync(); |
558 } | 572 } |
559 | 573 |
560 Future<File> writeAsString(String contents, | 574 Future<File> writeAsString(String contents, |
561 {FileMode mode: FileMode.WRITE, | 575 {FileMode mode: FileMode.WRITE, |
562 Encoding encoding: Encoding.UTF_8}) { | 576 Encoding encoding: Encoding.UTF_8, |
| 577 bool flush: false}) { |
563 try { | 578 try { |
564 return writeAsBytes(_encodeString(contents, encoding), mode: mode); | 579 return writeAsBytes( |
| 580 _encodeString(contents, encoding), mode: mode, flush: flush); |
565 } catch (e) { | 581 } catch (e) { |
566 return new Future.error(e); | 582 return new Future.error(e); |
567 } | 583 } |
568 } | 584 } |
569 | 585 |
570 void writeAsStringSync(String contents, | 586 void writeAsStringSync(String contents, |
571 {FileMode mode: FileMode.WRITE, | 587 {FileMode mode: FileMode.WRITE, |
572 Encoding encoding: Encoding.UTF_8}) { | 588 Encoding encoding: Encoding.UTF_8, |
573 writeAsBytesSync(_encodeString(contents, encoding), mode: mode); | 589 bool flush: false}) { |
| 590 writeAsBytesSync( |
| 591 _encodeString(contents, encoding), mode: mode, flush: flush); |
574 } | 592 } |
575 | 593 |
576 String get path => _path; | 594 String get path => _path; |
577 | 595 |
578 String toString() => "File: '$path'"; | 596 String toString() => "File: '$path'"; |
579 | 597 |
580 void _ensureFileService() { | 598 void _ensureFileService() { |
581 if (_fileService == null) { | 599 if (_fileService == null) { |
582 _fileService = _FileUtils._newServicePort(); | 600 _fileService = _FileUtils._newServicePort(); |
583 } | 601 } |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
986 | 1004 |
987 Future _closedException() { | 1005 Future _closedException() { |
988 return new Future.error(new FileException("File closed", _path)); | 1006 return new Future.error(new FileException("File closed", _path)); |
989 } | 1007 } |
990 | 1008 |
991 final String _path; | 1009 final String _path; |
992 int _id; | 1010 int _id; |
993 | 1011 |
994 SendPort _fileService; | 1012 SendPort _fileService; |
995 } | 1013 } |
OLD | NEW |