| 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 /** | 7 /** |
| 8 * The modes in which a File can be opened. | 8 * The modes in which a File can be opened. |
| 9 */ | 9 */ |
| 10 class FileMode { | 10 class FileMode { |
| 11 /// The mode for opening a file only for reading. | 11 /// The mode for opening a file only for reading. |
| 12 static const READ = const FileMode._internal(0); | 12 static const READ = const FileMode._internal(0); |
| 13 |
| 13 /// Mode for opening a file for reading and writing. The file is | 14 /// Mode for opening a file for reading and writing. The file is |
| 14 /// overwritten if it already exists. The file is created if it does not | 15 /// overwritten if it already exists. The file is created if it does not |
| 15 /// already exist. | 16 /// already exist. |
| 16 static const WRITE = const FileMode._internal(1); | 17 static const WRITE = const FileMode._internal(1); |
| 18 |
| 17 /// Mode for opening a file for reading and writing to the | 19 /// Mode for opening a file for reading and writing to the |
| 18 /// end of it. The file is created if it does not already exist. | 20 /// end of it. The file is created if it does not already exist. |
| 19 static const APPEND = const FileMode._internal(2); | 21 static const APPEND = const FileMode._internal(2); |
| 22 |
| 20 /// Mode for opening a file for writing *only*. The file is | 23 /// Mode for opening a file for writing *only*. The file is |
| 21 /// overwritten if it already exists. The file is created if it does not | 24 /// overwritten if it already exists. The file is created if it does not |
| 22 /// already exist. | 25 /// already exist. |
| 23 static const WRITE_ONLY = const FileMode._internal(3); | 26 static const WRITE_ONLY = const FileMode._internal(3); |
| 27 |
| 24 /// Mode for opening a file for writing *only* to the | 28 /// Mode for opening a file for writing *only* to the |
| 25 /// end of it. The file is created if it does not already exist. | 29 /// end of it. The file is created if it does not already exist. |
| 26 static const WRITE_ONLY_APPEND = const FileMode._internal(4); | 30 static const WRITE_ONLY_APPEND = const FileMode._internal(4); |
| 27 final int _mode; | 31 final int _mode; |
| 28 | 32 |
| 29 const FileMode._internal(this._mode); | 33 const FileMode._internal(this._mode); |
| 30 } | 34 } |
| 31 | 35 |
| 32 /// The mode for opening a file only for reading. | 36 /// The mode for opening a file only for reading. |
| 33 const READ = FileMode.READ; | 37 const READ = FileMode.READ; |
| 38 |
| 34 /// The mode for opening a file for reading and writing. The file is | 39 /// The mode for opening a file for reading and writing. The file is |
| 35 /// overwritten if it already exists. The file is created if it does not | 40 /// overwritten if it already exists. The file is created if it does not |
| 36 /// already exist. | 41 /// already exist. |
| 37 const WRITE = FileMode.WRITE; | 42 const WRITE = FileMode.WRITE; |
| 43 |
| 38 /// The mode for opening a file for reading and writing to the | 44 /// The mode for opening a file for reading and writing to the |
| 39 /// end of it. The file is created if it does not already exist. | 45 /// end of it. The file is created if it does not already exist. |
| 40 const APPEND = FileMode.APPEND; | 46 const APPEND = FileMode.APPEND; |
| 47 |
| 41 /// Mode for opening a file for writing *only*. The file is | 48 /// Mode for opening a file for writing *only*. The file is |
| 42 /// overwritten if it already exists. The file is created if it does not | 49 /// overwritten if it already exists. The file is created if it does not |
| 43 /// already exist. | 50 /// already exist. |
| 44 const WRITE_ONLY = FileMode.WRITE_ONLY; | 51 const WRITE_ONLY = FileMode.WRITE_ONLY; |
| 52 |
| 45 /// Mode for opening a file for writing *only* to the | 53 /// Mode for opening a file for writing *only* to the |
| 46 /// end of it. The file is created if it does not already exist. | 54 /// end of it. The file is created if it does not already exist. |
| 47 const WRITE_ONLY_APPEND = FileMode.WRITE_ONLY_APPEND; | 55 const WRITE_ONLY_APPEND = FileMode.WRITE_ONLY_APPEND; |
| 48 | 56 |
| 49 | |
| 50 /// Type of lock when requesting a lock on a file. | 57 /// Type of lock when requesting a lock on a file. |
| 51 enum FileLock { | 58 enum FileLock { |
| 52 /// Shared file lock. | 59 /// Shared file lock. |
| 53 SHARED, | 60 SHARED, |
| 61 |
| 54 /// Exclusive file lock. | 62 /// Exclusive file lock. |
| 55 EXCLUSIVE, | 63 EXCLUSIVE, |
| 64 |
| 56 /// Blocking shared file lock. | 65 /// Blocking shared file lock. |
| 57 BLOCKING_SHARED, | 66 BLOCKING_SHARED, |
| 67 |
| 58 /// Blocking exclusive file lock. | 68 /// Blocking exclusive file lock. |
| 59 BLOCKING_EXCLUSIVE, | 69 BLOCKING_EXCLUSIVE, |
| 60 } | 70 } |
| 61 | 71 |
| 62 /** | 72 /** |
| 63 * A reference to a file on the file system. | 73 * A reference to a file on the file system. |
| 64 * | 74 * |
| 65 * A File instance is an object that holds a [path] on which operations can | 75 * A File instance is an object that holds a [path] on which operations can |
| 66 * be performed. | 76 * be performed. |
| 67 * You can get the parent directory of the file using the getter [parent], | 77 * You can get the parent directory of the file using the getter [parent], |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 /** | 260 /** |
| 251 * Renames this file. Returns a `Future<File>` that completes | 261 * Renames this file. Returns a `Future<File>` that completes |
| 252 * with a [File] instance for the renamed file. | 262 * with a [File] instance for the renamed file. |
| 253 * | 263 * |
| 254 * If [newPath] identifies an existing file, that file is | 264 * If [newPath] identifies an existing file, that file is |
| 255 * replaced. If [newPath] identifies an existing directory, the | 265 * replaced. If [newPath] identifies an existing directory, the |
| 256 * operation fails and the future completes with an exception. | 266 * operation fails and the future completes with an exception. |
| 257 */ | 267 */ |
| 258 Future<File> rename(String newPath); | 268 Future<File> rename(String newPath); |
| 259 | 269 |
| 260 /** | 270 /** |
| 261 * Synchronously renames this file. Returns a [File] | 271 * Synchronously renames this file. Returns a [File] |
| 262 * instance for the renamed file. | 272 * instance for the renamed file. |
| 263 * | 273 * |
| 264 * If [newPath] identifies an existing file, that file is | 274 * If [newPath] identifies an existing file, that file is |
| 265 * replaced. If [newPath] identifies an existing directory the | 275 * replaced. If [newPath] identifies an existing directory the |
| 266 * operation fails and an exception is thrown. | 276 * operation fails and an exception is thrown. |
| 267 */ | 277 */ |
| 268 File renameSync(String newPath); | 278 File renameSync(String newPath); |
| 269 | 279 |
| 270 /** | 280 /** |
| 271 * Copy this file. Returns a `Future<File>` that completes | 281 * Copy this file. Returns a `Future<File>` that completes |
| 272 * with a [File] instance for the copied file. | 282 * with a [File] instance for the copied file. |
| 273 * | 283 * |
| 274 * If [newPath] identifies an existing file, that file is | 284 * If [newPath] identifies an existing file, that file is |
| 275 * replaced. If [newPath] identifies an existing directory, the | 285 * replaced. If [newPath] identifies an existing directory, the |
| 276 * operation fails and the future completes with an exception. | 286 * operation fails and the future completes with an exception. |
| 277 */ | 287 */ |
| 278 Future<File> copy(String newPath); | 288 Future<File> copy(String newPath); |
| 279 | 289 |
| 280 /** | 290 /** |
| 281 * Synchronously copy this file. Returns a [File] | 291 * Synchronously copy this file. Returns a [File] |
| 282 * instance for the copied file. | 292 * instance for the copied file. |
| 283 * | 293 * |
| 284 * If [newPath] identifies an existing file, that file is | 294 * If [newPath] identifies an existing file, that file is |
| 285 * replaced. If [newPath] identifies an existing directory the | 295 * replaced. If [newPath] identifies an existing directory the |
| 286 * operation fails and an exception is thrown. | 296 * operation fails and an exception is thrown. |
| 287 */ | 297 */ |
| 288 File copySync(String newPath); | 298 File copySync(String newPath); |
| 289 | 299 |
| 290 /** | 300 /** |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 * | 444 * |
| 435 * * [FileMode.WRITE]: truncates the file to length zero. | 445 * * [FileMode.WRITE]: truncates the file to length zero. |
| 436 * * [FileMode.APPEND]: sets the initial write position to the end | 446 * * [FileMode.APPEND]: sets the initial write position to the end |
| 437 * of the file. | 447 * of the file. |
| 438 * | 448 * |
| 439 * When writing strings through the returned [IOSink] the encoding | 449 * When writing strings through the returned [IOSink] the encoding |
| 440 * specified using [encoding] will be used. The returned [IOSink] | 450 * specified using [encoding] will be used. The returned [IOSink] |
| 441 * has an `encoding` property which can be changed after the | 451 * has an `encoding` property which can be changed after the |
| 442 * [IOSink] has been created. | 452 * [IOSink] has been created. |
| 443 */ | 453 */ |
| 444 IOSink openWrite({FileMode mode: FileMode.WRITE, | 454 IOSink openWrite({FileMode mode: FileMode.WRITE, Encoding encoding: UTF8}); |
| 445 Encoding encoding: UTF8}); | |
| 446 | 455 |
| 447 /** | 456 /** |
| 448 * Read the entire file contents as a list of bytes. Returns a | 457 * Read the entire file contents as a list of bytes. Returns a |
| 449 * `Future<List<int>>` that completes with the list of bytes that | 458 * `Future<List<int>>` that completes with the list of bytes that |
| 450 * is the contents of the file. | 459 * is the contents of the file. |
| 451 */ | 460 */ |
| 452 Future<List<int>> readAsBytes(); | 461 Future<List<int>> readAsBytes(); |
| 453 | 462 |
| 454 /** | 463 /** |
| 455 * Synchronously read the entire file contents as a list of bytes. | 464 * Synchronously read the entire file contents as a list of bytes. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 * the entire operation has completed. | 509 * the entire operation has completed. |
| 501 * | 510 * |
| 502 * By default [writeAsBytes] creates the file for writing and truncates the | 511 * By default [writeAsBytes] creates the file for writing and truncates the |
| 503 * file if it already exists. In order to append the bytes to an existing | 512 * file if it already exists. In order to append the bytes to an existing |
| 504 * file, pass [FileMode.APPEND] as the optional mode parameter. | 513 * file, pass [FileMode.APPEND] as the optional mode parameter. |
| 505 * | 514 * |
| 506 * If the argument [flush] is set to `true`, the data written will be | 515 * If the argument [flush] is set to `true`, the data written will be |
| 507 * flushed to the file system before the returned future completes. | 516 * flushed to the file system before the returned future completes. |
| 508 */ | 517 */ |
| 509 Future<File> writeAsBytes(List<int> bytes, | 518 Future<File> writeAsBytes(List<int> bytes, |
| 510 {FileMode mode: FileMode.WRITE, | 519 {FileMode mode: FileMode.WRITE, bool flush: false}); |
| 511 bool flush: false}); | |
| 512 | 520 |
| 513 /** | 521 /** |
| 514 * Synchronously write a list of bytes to a file. | 522 * Synchronously write a list of bytes to a file. |
| 515 * | 523 * |
| 516 * Opens the file, writes the list of bytes to it and closes the file. | 524 * Opens the file, writes the list of bytes to it and closes the file. |
| 517 * | 525 * |
| 518 * By default [writeAsBytesSync] creates the file for writing and truncates | 526 * By default [writeAsBytesSync] creates the file for writing and truncates |
| 519 * the file if it already exists. In order to append the bytes to an existing | 527 * the file if it already exists. In order to append the bytes to an existing |
| 520 * file, pass [FileMode.APPEND] as the optional mode parameter. | 528 * file, pass [FileMode.APPEND] as the optional mode parameter. |
| 521 * | 529 * |
| 522 * If the [flush] argument is set to `true` data written will be | 530 * If the [flush] argument is set to `true` data written will be |
| 523 * flushed to the file system before returning. | 531 * flushed to the file system before returning. |
| 524 * | 532 * |
| 525 * Throws a [FileSystemException] if the operation fails. | 533 * Throws a [FileSystemException] if the operation fails. |
| 526 */ | 534 */ |
| 527 void writeAsBytesSync(List<int> bytes, | 535 void writeAsBytesSync(List<int> bytes, |
| 528 {FileMode mode: FileMode.WRITE, | 536 {FileMode mode: FileMode.WRITE, bool flush: false}); |
| 529 bool flush: false}); | |
| 530 | 537 |
| 531 /** | 538 /** |
| 532 * Write a string to a file. | 539 * Write a string to a file. |
| 533 * | 540 * |
| 534 * Opens the file, writes the string in the given encoding, and closes the | 541 * Opens the file, writes the string in the given encoding, and closes the |
| 535 * file. Returns a `Future<File>` that completes with this [File] object | 542 * file. Returns a `Future<File>` that completes with this [File] object |
| 536 * once the entire operation has completed. | 543 * once the entire operation has completed. |
| 537 * | 544 * |
| 538 * By default [writeAsString] creates the file for writing and truncates the | 545 * By default [writeAsString] creates the file for writing and truncates the |
| 539 * file if it already exists. In order to append the bytes to an existing | 546 * file if it already exists. In order to append the bytes to an existing |
| 540 * file, pass [FileMode.APPEND] as the optional mode parameter. | 547 * file, pass [FileMode.APPEND] as the optional mode parameter. |
| 541 * | 548 * |
| 542 * If the argument [flush] is set to `true`, the data written will be | 549 * If the argument [flush] is set to `true`, the data written will be |
| 543 * flushed to the file system before the returned future completes. | 550 * flushed to the file system before the returned future completes. |
| 544 * | 551 * |
| 545 */ | 552 */ |
| 546 Future<File> writeAsString(String contents, | 553 Future<File> writeAsString(String contents, |
| 547 {FileMode mode: FileMode.WRITE, | 554 {FileMode mode: FileMode.WRITE, |
| 548 Encoding encoding: UTF8, | 555 Encoding encoding: UTF8, |
| 549 bool flush: false}); | 556 bool flush: false}); |
| 550 | 557 |
| 551 /** | 558 /** |
| 552 * Synchronously write a string to a file. | 559 * Synchronously write a string to a file. |
| 553 * | 560 * |
| 554 * Opens the file, writes the string in the given encoding, and closes the | 561 * Opens the file, writes the string in the given encoding, and closes the |
| 555 * file. | 562 * file. |
| 556 * | 563 * |
| 557 * By default [writeAsStringSync] creates the file for writing and | 564 * By default [writeAsStringSync] creates the file for writing and |
| 558 * truncates the file if it already exists. In order to append the bytes | 565 * truncates the file if it already exists. In order to append the bytes |
| 559 * to an existing file, pass [FileMode.APPEND] as the optional mode | 566 * to an existing file, pass [FileMode.APPEND] as the optional mode |
| 560 * parameter. | 567 * parameter. |
| 561 * | 568 * |
| 562 * If the [flush] argument is set to `true` data written will be | 569 * If the [flush] argument is set to `true` data written will be |
| 563 * flushed to the file system before returning. | 570 * flushed to the file system before returning. |
| 564 * | 571 * |
| 565 * Throws a [FileSystemException] if the operation fails. | 572 * Throws a [FileSystemException] if the operation fails. |
| 566 */ | 573 */ |
| 567 void writeAsStringSync(String contents, | 574 void writeAsStringSync(String contents, |
| 568 {FileMode mode: FileMode.WRITE, | 575 {FileMode mode: FileMode.WRITE, |
| 569 Encoding encoding: UTF8, | 576 Encoding encoding: UTF8, |
| 570 bool flush: false}); | 577 bool flush: false}); |
| 571 | 578 |
| 572 /** | 579 /** |
| 573 * Get the path of the file. | 580 * Get the path of the file. |
| 574 */ | 581 */ |
| 575 String get path; | 582 String get path; |
| 576 } | 583 } |
| 577 | 584 |
| 578 | |
| 579 /** | 585 /** |
| 580 * `RandomAccessFile` provides random access to the data in a | 586 * `RandomAccessFile` provides random access to the data in a |
| 581 * file. | 587 * file. |
| 582 * | 588 * |
| 583 * `RandomAccessFile` objects are obtained by calling the | 589 * `RandomAccessFile` objects are obtained by calling the |
| 584 * `open` method on a [File] object. | 590 * `open` method on a [File] object. |
| 585 * | 591 * |
| 586 * A `RandomAccessFile` have both asynchronous and synchronous | 592 * A `RandomAccessFile` have both asynchronous and synchronous |
| 587 * methods. The asynchronous methods all return a `Future` | 593 * methods. The asynchronous methods all return a `Future` |
| 588 * whereas the synchronous methods will return the result directly, | 594 * whereas the synchronous methods will return the result directly, |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 int writeByteSync(int value); | 680 int writeByteSync(int value); |
| 675 | 681 |
| 676 /** | 682 /** |
| 677 * Writes from a [List<int>] to the file. It will read the buffer from index | 683 * Writes from a [List<int>] to the file. It will read the buffer from index |
| 678 * [start] to index [end]. If [start] is omitted, it'll start from index 0. | 684 * [start] to index [end]. If [start] is omitted, it'll start from index 0. |
| 679 * If [end] is omitted, it will write to end of [buffer]. | 685 * If [end] is omitted, it will write to end of [buffer]. |
| 680 * | 686 * |
| 681 * Returns a `Future<RandomAccessFile>` that completes with this | 687 * Returns a `Future<RandomAccessFile>` that completes with this |
| 682 * [RandomAccessFile] when the write completes. | 688 * [RandomAccessFile] when the write completes. |
| 683 */ | 689 */ |
| 684 Future<RandomAccessFile> writeFrom( | 690 Future<RandomAccessFile> writeFrom(List<int> buffer, |
| 685 List<int> buffer, [int start = 0, int end]); | 691 [int start = 0, int end]); |
| 686 | 692 |
| 687 /** | 693 /** |
| 688 * Synchronously writes from a [List<int>] to the file. It will read the | 694 * Synchronously writes from a [List<int>] to the file. It will read the |
| 689 * buffer from index [start] to index [end]. If [start] is omitted, it'll | 695 * buffer from index [start] to index [end]. If [start] is omitted, it'll |
| 690 * start from index 0. If [end] is omitted, it will write to the end of | 696 * start from index 0. If [end] is omitted, it will write to the end of |
| 691 * [buffer]. | 697 * [buffer]. |
| 692 * | 698 * |
| 693 * Throws a [FileSystemException] if the operation fails. | 699 * Throws a [FileSystemException] if the operation fails. |
| 694 */ | 700 */ |
| 695 void writeFromSync(List<int> buffer, [int start = 0, int end]); | 701 void writeFromSync(List<int> buffer, [int start = 0, int end]); |
| 696 | 702 |
| 697 /** | 703 /** |
| 698 * Writes a string to the file using the given [Encoding]. Returns a | 704 * Writes a string to the file using the given [Encoding]. Returns a |
| 699 * `Future<RandomAccessFile>` that completes with this | 705 * `Future<RandomAccessFile>` that completes with this |
| 700 * RandomAccessFile when the write completes. | 706 * RandomAccessFile when the write completes. |
| 701 */ | 707 */ |
| 702 Future<RandomAccessFile> writeString(String string, | 708 Future<RandomAccessFile> writeString(String string, |
| 703 {Encoding encoding: UTF8}); | 709 {Encoding encoding: UTF8}); |
| 704 | 710 |
| 705 /** | 711 /** |
| 706 * Synchronously writes a single string to the file using the given | 712 * Synchronously writes a single string to the file using the given |
| 707 * [Encoding]. | 713 * [Encoding]. |
| 708 * | 714 * |
| 709 * Throws a [FileSystemException] if the operation fails. | 715 * Throws a [FileSystemException] if the operation fails. |
| 710 */ | 716 */ |
| 711 void writeStringSync(String string, | 717 void writeStringSync(String string, {Encoding encoding: UTF8}); |
| 712 {Encoding encoding: UTF8}); | |
| 713 | 718 |
| 714 /** | 719 /** |
| 715 * Gets the current byte position in the file. Returns a | 720 * Gets the current byte position in the file. Returns a |
| 716 * `Future<int>` that completes with the position. | 721 * `Future<int>` that completes with the position. |
| 717 */ | 722 */ |
| 718 Future<int> position(); | 723 Future<int> position(); |
| 719 | 724 |
| 720 /** | 725 /** |
| 721 * Synchronously gets the current byte position in the file. | 726 * Synchronously gets the current byte position in the file. |
| 722 * | 727 * |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 892 * Returns a human-readable string for this RandomAccessFile instance. | 897 * Returns a human-readable string for this RandomAccessFile instance. |
| 893 */ | 898 */ |
| 894 String toString(); | 899 String toString(); |
| 895 | 900 |
| 896 /** | 901 /** |
| 897 * Gets the path of the file underlying this RandomAccessFile. | 902 * Gets the path of the file underlying this RandomAccessFile. |
| 898 */ | 903 */ |
| 899 String get path; | 904 String get path; |
| 900 } | 905 } |
| 901 | 906 |
| 902 | |
| 903 /** | 907 /** |
| 904 * Exception thrown when a file operation fails. | 908 * Exception thrown when a file operation fails. |
| 905 */ | 909 */ |
| 906 class FileSystemException implements IOException { | 910 class FileSystemException implements IOException { |
| 907 /** | 911 /** |
| 908 * Message describing the error. This does not include any detailed | 912 * Message describing the error. This does not include any detailed |
| 909 * information form the underlying OS error. Check [osError] for | 913 * information form the underlying OS error. Check [osError] for |
| 910 * that information. | 914 * that information. |
| 911 */ | 915 */ |
| 912 final String message; | 916 final String message; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 945 sb.write(": $osError"); | 949 sb.write(": $osError"); |
| 946 if (path != null) { | 950 if (path != null) { |
| 947 sb.write(", path = '$path'"); | 951 sb.write(", path = '$path'"); |
| 948 } | 952 } |
| 949 } else if (path != null) { | 953 } else if (path != null) { |
| 950 sb.write(": $path"); | 954 sb.write(": $path"); |
| 951 } | 955 } |
| 952 return sb.toString(); | 956 return sb.toString(); |
| 953 } | 957 } |
| 954 } | 958 } |
| OLD | NEW |