| 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 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 }) | 199 }) |
| 200 .catchError((e) { | 200 .catchError((e) { |
| 201 completer.completeError(e); | 201 completer.completeError(e); |
| 202 }); | 202 }); |
| 203 return completer.future; | 203 return completer.future; |
| 204 } | 204 } |
| 205 | 205 |
| 206 Future<File> close() { | 206 Future<File> close() { |
| 207 return _openFuture.then((openedFile) => openedFile.close()); | 207 return _openFuture.then((openedFile) => openedFile.close()); |
| 208 } | 208 } |
| 209 |
| 210 Future flush() { |
| 211 return _openFuture |
| 212 .then((openedFile) => openedFile.flush()) |
| 213 .then((randomAccessfile) => new Future.value(null)); |
| 214 } |
| 209 } | 215 } |
| 210 | 216 |
| 211 | 217 |
| 212 // Class for encapsulating the native implementation of files. | 218 // Class for encapsulating the native implementation of files. |
| 213 class _File extends FileSystemEntity implements File { | 219 class _File extends FileSystemEntity implements File { |
| 214 final String path; | 220 final String path; |
| 215 | 221 |
| 216 // Constructor for file. | 222 // Constructor for file. |
| 217 _File(String this.path) { | 223 _File(String this.path) { |
| 218 if (path is! String) { | 224 if (path is! String) { |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 return readAsBytes().then((bytes) { | 481 return readAsBytes().then((bytes) { |
| 476 return _decodeLines(bytes, encoding); | 482 return _decodeLines(bytes, encoding); |
| 477 }); | 483 }); |
| 478 } | 484 } |
| 479 | 485 |
| 480 List<String> readAsLinesSync({Encoding encoding: UTF8}) { | 486 List<String> readAsLinesSync({Encoding encoding: UTF8}) { |
| 481 return _decodeLines(readAsBytesSync(), encoding); | 487 return _decodeLines(readAsBytesSync(), encoding); |
| 482 } | 488 } |
| 483 | 489 |
| 484 Future<File> writeAsBytes(List<int> bytes, | 490 Future<File> writeAsBytes(List<int> bytes, |
| 485 {FileMode mode: FileMode.WRITE}) { | 491 {FileMode mode: FileMode.WRITE, |
| 492 bool flush: false}) { |
| 486 try { | 493 try { |
| 487 IOSink sink = openWrite(mode: mode); | 494 IOSink sink = openWrite(mode: mode); |
| 488 sink.add(bytes); | 495 sink.add(bytes); |
| 489 sink.close(); | 496 if (flush) { |
| 497 sink.flush().then((_) => sink.close()); |
| 498 } else { |
| 499 sink.close(); |
| 500 } |
| 490 return sink.done.then((_) => this); | 501 return sink.done.then((_) => this); |
| 491 } catch (e) { | 502 } catch (e) { |
| 492 return new Future.error(e); | 503 return new Future.error(e); |
| 493 } | 504 } |
| 494 } | 505 } |
| 495 | 506 |
| 496 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { | 507 void writeAsBytesSync(List<int> bytes, |
| 508 {FileMode mode: FileMode.WRITE, |
| 509 bool flush: false}) { |
| 497 RandomAccessFile opened = openSync(mode: mode); | 510 RandomAccessFile opened = openSync(mode: mode); |
| 498 opened.writeFromSync(bytes, 0, bytes.length); | 511 opened.writeFromSync(bytes, 0, bytes.length); |
| 512 if (flush) opened.flushSync(); |
| 499 opened.closeSync(); | 513 opened.closeSync(); |
| 500 } | 514 } |
| 501 | 515 |
| 502 Future<File> writeAsString(String contents, | 516 Future<File> writeAsString(String contents, |
| 503 {FileMode mode: FileMode.WRITE, | 517 {FileMode mode: FileMode.WRITE, |
| 504 Encoding encoding: UTF8}) { | 518 Encoding encoding: UTF8, |
| 519 bool flush: false}) { |
| 505 try { | 520 try { |
| 506 return writeAsBytes(encoding.encode(contents), mode: mode); | 521 return writeAsBytes(encoding.encode(contents), mode: mode); |
| 507 } catch (e) { | 522 } catch (e) { |
| 508 return new Future.error(e); | 523 return new Future.error(e); |
| 509 } | 524 } |
| 510 } | 525 } |
| 511 | 526 |
| 512 void writeAsStringSync(String contents, | 527 void writeAsStringSync(String contents, |
| 513 {FileMode mode: FileMode.WRITE, | 528 {FileMode mode: FileMode.WRITE, |
| 514 Encoding encoding: UTF8}) { | 529 Encoding encoding: UTF8, |
| 530 bool flush: false}) { |
| 515 writeAsBytesSync(encoding.encode(contents), mode: mode); | 531 writeAsBytesSync(encoding.encode(contents), mode: mode); |
| 516 } | 532 } |
| 517 | 533 |
| 518 String toString() => "File: '$path'"; | 534 String toString() => "File: '$path'"; |
| 519 | 535 |
| 520 static throwIfError(Object result, String msg, String path) { | 536 static throwIfError(Object result, String msg, String path) { |
| 521 if (result is OSError) { | 537 if (result is OSError) { |
| 522 throw new FileSystemException(msg, path, result); | 538 throw new FileSystemException(msg, path, result); |
| 523 } | 539 } |
| 524 } | 540 } |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 867 | 883 |
| 868 void _checkAvailable() { | 884 void _checkAvailable() { |
| 869 if (_asyncDispatched) { | 885 if (_asyncDispatched) { |
| 870 throw new FileSystemException("An async operation is currently pending", p
ath); | 886 throw new FileSystemException("An async operation is currently pending", p
ath); |
| 871 } | 887 } |
| 872 if (closed) { | 888 if (closed) { |
| 873 throw new FileSystemException("File closed", path); | 889 throw new FileSystemException("File closed", path); |
| 874 } | 890 } |
| 875 } | 891 } |
| 876 } | 892 } |
| OLD | NEW |