| OLD | NEW |
| 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 _file = new File(name); | 7 _file = new File(name); |
| 8 _data = []; | 8 _data = []; |
| 9 _position = 0; | 9 _position = 0; |
| 10 _file.onError = _reportError; | 10 _file.onError = _reportError; |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 static flush(int id) native "File_Flush"; | 271 static flush(int id) native "File_Flush"; |
| 272 static int openStdio(int fd) native "File_OpenStdio"; | 272 static int openStdio(int fd) native "File_OpenStdio"; |
| 273 static SendPort newServicePort() native "File_NewServicePort"; | 273 static SendPort newServicePort() native "File_NewServicePort"; |
| 274 | 274 |
| 275 static bool checkedExists(String name) { | 275 static bool checkedExists(String name) { |
| 276 if (name is !String) { | 276 if (name is !String) { |
| 277 throw new IllegalArgumentException(); | 277 throw new IllegalArgumentException(); |
| 278 } | 278 } |
| 279 var result = exists(name); | 279 var result = exists(name); |
| 280 if (result is OSError) { | 280 if (result is OSError) { |
| 281 throw new FileIOException("Cannot check existence of file", result); | 281 throw new FileIOException("Cannot check existence of file '$name'", |
| 282 result); |
| 282 } | 283 } |
| 283 return result; | 284 return result; |
| 284 } | 285 } |
| 285 | 286 |
| 286 static int checkedOpen(String name, int mode) { | 287 static int checkedOpen(String name, int mode) { |
| 287 if (name is !String || mode is !int) { | 288 if (name is !String || mode is !int) { |
| 288 throw new IllegalArgumentException(); | 289 throw new IllegalArgumentException(); |
| 289 }; | 290 }; |
| 290 var result = open(name, mode); | 291 var result = open(name, mode); |
| 291 if (result is OSError) { | 292 if (result is OSError) { |
| 292 throw new FileIOException("Cannot open file $name", result); | 293 throw new FileIOException("Cannot open file '$name'", result); |
| 293 } | 294 } |
| 294 return result; | 295 return result; |
| 295 } | 296 } |
| 296 | 297 |
| 297 static bool checkedCreate(String name) { | 298 static bool checkedCreate(String name) { |
| 298 if (name is !String) { | 299 if (name is !String) { |
| 299 throw new IllegalArgumentException(); | 300 throw new IllegalArgumentException(); |
| 300 }; | 301 }; |
| 301 var result = create(name); | 302 var result = create(name); |
| 302 if (result is OSError) { | 303 if (result is OSError) { |
| 303 throw new FileIOException("Cannot create file", result); | 304 throw new FileIOException("Cannot create file '$name'", result); |
| 304 } | 305 } |
| 305 return true; | 306 return true; |
| 306 } | 307 } |
| 307 | 308 |
| 308 static bool checkedDelete(String name) { | 309 static bool checkedDelete(String name) { |
| 309 if (name is !String) { | 310 if (name is !String) { |
| 310 throw new IllegalArgumentException(); | 311 throw new IllegalArgumentException(); |
| 311 }; | 312 }; |
| 312 var result = delete(name); | 313 var result = delete(name); |
| 313 if (result is OSError) { | 314 if (result is OSError) { |
| 314 throw new FileIOException("Cannot delete file", result); | 315 throw new FileIOException("Cannot delete file '$name'", result); |
| 315 } | 316 } |
| 316 return true; | 317 return true; |
| 317 } | 318 } |
| 318 | 319 |
| 319 static String checkedFullPath(String name) { | 320 static String checkedFullPath(String name) { |
| 320 if (name is !String) { | 321 if (name is !String) { |
| 321 throw new IllegalArgumentException(); | 322 throw new IllegalArgumentException(); |
| 322 }; | 323 }; |
| 323 var result = fullPath(name); | 324 var result = fullPath(name); |
| 324 if (result is OSError) { | 325 if (result is OSError) { |
| 325 throw new FileIOException("Cannot retrieve full path", result); | 326 throw new FileIOException( |
| 327 "Cannot retrieve full path for file '$name'", result); |
| 326 } | 328 } |
| 327 return result; | 329 return result; |
| 328 } | 330 } |
| 329 | 331 |
| 330 static String checkedDirectory(String name) { | 332 static String checkedDirectory(String name) { |
| 331 if (name is !String) { | 333 if (name is !String) { |
| 332 throw new IllegalArgumentException(); | 334 throw new IllegalArgumentException(); |
| 333 } | 335 } |
| 334 var result = directory(name); | 336 var result = directory(name); |
| 335 if (result is OSError) { | 337 if (result is OSError) { |
| 336 throw new FileIOException("Cannot retrieve directory for file", result); | 338 throw new FileIOException( |
| 339 "Cannot retrieve directory for file '$name'", result); |
| 337 } | 340 } |
| 338 return result; | 341 return result; |
| 339 } | 342 } |
| 340 | 343 |
| 341 static int checkedLengthFromName(String name) { | 344 static int checkedLengthFromName(String name) { |
| 342 if (name is !String) { | 345 if (name is !String) { |
| 343 throw new IllegalArgumentException(); | 346 throw new IllegalArgumentException(); |
| 344 } | 347 } |
| 345 var result = lengthFromName(name); | 348 var result = lengthFromName(name); |
| 346 if (result is OSError) { | 349 if (result is OSError) { |
| 347 throw new FileIOException("Cannot retrieve length of file", result); | 350 throw new FileIOException( |
| 351 "Cannot retrieve length of file '$name'", result); |
| 348 } | 352 } |
| 349 return result; | 353 return result; |
| 350 } | 354 } |
| 351 | 355 |
| 352 static int checkReadWriteListArguments(int length, int offset, int bytes) { | 356 static int checkReadWriteListArguments(int length, int offset, int bytes) { |
| 353 if (offset < 0) return offset; | 357 if (offset < 0) return offset; |
| 354 if (bytes < 0) return bytes; | 358 if (bytes < 0) return bytes; |
| 355 if ((offset + bytes) > length) return offset + bytes; | 359 if ((offset + bytes) > length) return offset + bytes; |
| 356 return 0; | 360 return 0; |
| 357 } | 361 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 _handleErrorResponse(response, "Cannot create file"); | 441 _handleErrorResponse(response, "Cannot create file"); |
| 438 } else { | 442 } else { |
| 439 callback(); | 443 callback(); |
| 440 } | 444 } |
| 441 }); | 445 }); |
| 442 } | 446 } |
| 443 | 447 |
| 444 void createSync() { | 448 void createSync() { |
| 445 bool created = _FileUtils.checkedCreate(_name); | 449 bool created = _FileUtils.checkedCreate(_name); |
| 446 if (!created) { | 450 if (!created) { |
| 447 throw new FileIOException("Cannot create file: $_name"); | 451 throw new FileIOException("Cannot create file '$_name'"); |
| 448 } | 452 } |
| 449 } | 453 } |
| 450 | 454 |
| 451 void delete(void callback()) { | 455 void delete(void callback()) { |
| 452 _ensureFileService(); | 456 _ensureFileService(); |
| 453 List request = new List(2); | 457 List request = new List(2); |
| 454 request[0] = _FileUtils.kDeleteRequest; | 458 request[0] = _FileUtils.kDeleteRequest; |
| 455 request[1] = _name; | 459 request[1] = _name; |
| 456 _fileService.call(request).then((response) { | 460 _fileService.call(request).then((response) { |
| 457 if (_isErrorResponse(response)) { | 461 if (_isErrorResponse(response)) { |
| 458 _handleErrorResponse(response, "Cannot delete file"); | 462 _handleErrorResponse(response, "Cannot delete file '$_name'"); |
| 459 } else { | 463 } else { |
| 460 callback(); | 464 callback(); |
| 461 } | 465 } |
| 462 }); | 466 }); |
| 463 } | 467 } |
| 464 | 468 |
| 465 void deleteSync() { | 469 void deleteSync() { |
| 466 _FileUtils.checkedDelete(_name); | 470 _FileUtils.checkedDelete(_name); |
| 467 } | 471 } |
| 468 | 472 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 }); | 510 }); |
| 507 } | 511 } |
| 508 | 512 |
| 509 void length(void callback(int length)) { | 513 void length(void callback(int length)) { |
| 510 _ensureFileService(); | 514 _ensureFileService(); |
| 511 List request = new List(2); | 515 List request = new List(2); |
| 512 request[0] = _FileUtils.kLengthFromNameRequest; | 516 request[0] = _FileUtils.kLengthFromNameRequest; |
| 513 request[1] = _name; | 517 request[1] = _name; |
| 514 _fileService.call(request).then((response) { | 518 _fileService.call(request).then((response) { |
| 515 if (_isErrorResponse(response)) { | 519 if (_isErrorResponse(response)) { |
| 516 _handleErrorResponse(response, "Cannot retrieve length of file"); | 520 _handleErrorResponse(response, |
| 521 "Cannot retrieve length of file '$_name'"); |
| 517 } else { | 522 } else { |
| 518 callback(response); | 523 callback(response); |
| 519 } | 524 } |
| 520 }); | 525 }); |
| 521 } | 526 } |
| 522 | 527 |
| 523 int lengthSync() { | 528 int lengthSync() { |
| 524 var result = _FileUtils.checkedLengthFromName(_name); | 529 var result = _FileUtils.checkedLengthFromName(_name); |
| 525 if (result is OSError) { | 530 if (result is OSError) { |
| 526 throw new FileIOException("Cannot retrieve length of file", result); | 531 throw new FileIOException("Cannot retrieve length of file '$_name'", |
| 532 result); |
| 527 } | 533 } |
| 528 return result; | 534 return result; |
| 529 } | 535 } |
| 530 | 536 |
| 531 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { | 537 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { |
| 532 if (mode != FileMode.READ && | 538 if (mode != FileMode.READ && |
| 533 mode != FileMode.WRITE && | 539 mode != FileMode.WRITE && |
| 534 mode != FileMode.APPEND) { | 540 mode != FileMode.APPEND) { |
| 535 throw new FileIOException("Unknown file mode. Use FileMode.READ, " | 541 throw new FileIOException("Unknown file mode. Use FileMode.READ, " |
| 536 "FileMode.WRITE or FileMode.APPEND."); | 542 "FileMode.WRITE or FileMode.APPEND."); |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 | 681 |
| 676 SendPort _fileService; | 682 SendPort _fileService; |
| 677 } | 683 } |
| 678 | 684 |
| 679 | 685 |
| 680 class _RandomAccessFile extends _FileBase implements RandomAccessFile { | 686 class _RandomAccessFile extends _FileBase implements RandomAccessFile { |
| 681 _RandomAccessFile(int this._id, String this._name); | 687 _RandomAccessFile(int this._id, String this._name); |
| 682 | 688 |
| 683 void close(void callback()) { | 689 void close(void callback()) { |
| 684 if (_id == 0) { | 690 if (_id == 0) { |
| 685 _reportError(new FileIOException("Cannot close file: $_name")); | 691 _reportError(new FileIOException("Cannot close file '$_name'")); |
| 686 return; | 692 return; |
| 687 } | 693 } |
| 688 _ensureFileService(); | 694 _ensureFileService(); |
| 689 List request = new List(2); | 695 List request = new List(2); |
| 690 request[0] = _FileUtils.kCloseRequest; | 696 request[0] = _FileUtils.kCloseRequest; |
| 691 request[1] = _id; | 697 request[1] = _id; |
| 692 // Set the id_ to 0 (NULL) to ensure the no more async requests | 698 // Set the id_ to 0 (NULL) to ensure the no more async requests |
| 693 // can be issues for this file. | 699 // can be issues for this file. |
| 694 _id = 0; | 700 _id = 0; |
| 695 _fileService.call(request).then((result) { | 701 _fileService.call(request).then((result) { |
| 696 if (result != -1) { | 702 if (result != -1) { |
| 697 _id = result; | 703 _id = result; |
| 698 callback(); | 704 callback(); |
| 699 } else { | 705 } else { |
| 700 _reportError(new FileIOException("Cannot close file: $_name")); | 706 _reportError(new FileIOException("Cannot close file '$_name'")); |
| 701 } | 707 } |
| 702 }); | 708 }); |
| 703 } | 709 } |
| 704 | 710 |
| 705 void closeSync() { | 711 void closeSync() { |
| 706 var id = _FileUtils.close(_id); | 712 var id = _FileUtils.close(_id); |
| 707 if (id == -1) { | 713 if (id == -1) { |
| 708 throw new FileIOException("Cannot close file: $_name"); | 714 throw new FileIOException("Cannot close file '$_name'"); |
| 709 } | 715 } |
| 710 _id = id; | 716 _id = id; |
| 711 } | 717 } |
| 712 | 718 |
| 713 void readByte(void callback(int byte)) { | 719 void readByte(void callback(int byte)) { |
| 714 _ensureFileService(); | 720 _ensureFileService(); |
| 715 List request = new List(2); | 721 List request = new List(2); |
| 716 request[0] = _FileUtils.kReadByteRequest; | 722 request[0] = _FileUtils.kReadByteRequest; |
| 717 request[1] = _id; | 723 request[1] = _id; |
| 718 _fileService.call(request).then((response) { | 724 _fileService.call(request).then((response) { |
| 719 if (_isErrorResponse(response)) { | 725 if (_isErrorResponse(response)) { |
| 720 _handleErrorResponse(response, "readByte failed"); | 726 _handleErrorResponse(response, "readByte failed for file '$_name'"); |
| 721 } else { | 727 } else { |
| 722 callback(response); | 728 callback(response); |
| 723 } | 729 } |
| 724 }); | 730 }); |
| 725 } | 731 } |
| 726 | 732 |
| 727 int readByteSync() { | 733 int readByteSync() { |
| 728 _checkNotClosed(); | 734 _checkNotClosed(); |
| 729 var result = _FileUtils.readByte(_id); | 735 var result = _FileUtils.readByte(_id); |
| 730 if (result is OSError) { | 736 if (result is OSError) { |
| 731 throw new FileIOException("readByte failed", result); | 737 throw new FileIOException("readByte failed for file '$_name'", result); |
| 732 } | 738 } |
| 733 return result; | 739 return result; |
| 734 } | 740 } |
| 735 | 741 |
| 736 void readList(List<int> buffer, int offset, int bytes, | 742 void readList(List<int> buffer, int offset, int bytes, |
| 737 void callback(int read)) { | 743 void callback(int read)) { |
| 738 _ensureFileService(); | 744 _ensureFileService(); |
| 739 if (buffer is !List || offset is !int || bytes is !int) { | 745 if (buffer is !List || offset is !int || bytes is !int) { |
| 740 _reportError(new FileIOException("Invalid arguments to readList")); | 746 _reportError(new FileIOException( |
| 747 "Invalid arguments to readList for file '$_name'")); |
| 741 return; | 748 return; |
| 742 }; | 749 }; |
| 743 List request = new List(3); | 750 List request = new List(3); |
| 744 request[0] = _FileUtils.kReadListRequest; | 751 request[0] = _FileUtils.kReadListRequest; |
| 745 request[1] = _id; | 752 request[1] = _id; |
| 746 request[2] = bytes; | 753 request[2] = bytes; |
| 747 _fileService.call(request).then((response) { | 754 _fileService.call(request).then((response) { |
| 748 if (_isErrorResponse(response)) { | 755 if (_isErrorResponse(response)) { |
| 749 _handleErrorResponse(response, "readList failed"); | 756 _handleErrorResponse(response, "readList failed for file '$_name'"); |
| 750 } else { | 757 } else { |
| 751 var read = response[1]; | 758 var read = response[1]; |
| 752 var data = response[2]; | 759 var data = response[2]; |
| 753 buffer.setRange(offset, read, data); | 760 buffer.setRange(offset, read, data); |
| 754 callback(read); | 761 callback(read); |
| 755 } | 762 } |
| 756 }); | 763 }); |
| 757 } | 764 } |
| 758 | 765 |
| 759 int readListSync(List<int> buffer, int offset, int bytes) { | 766 int readListSync(List<int> buffer, int offset, int bytes) { |
| 760 _checkNotClosed(); | 767 _checkNotClosed(); |
| 761 if (buffer is !List || offset is !int || bytes is !int) { | 768 if (buffer is !List || offset is !int || bytes is !int) { |
| 762 throw new FileIOException("Invalid arguments to readList"); | 769 throw new FileIOException( |
| 770 "Invalid arguments to readList for file '$_name'"); |
| 763 } | 771 } |
| 764 if (bytes == 0) return 0; | 772 if (bytes == 0) return 0; |
| 765 int index = | 773 int index = |
| 766 _FileUtils.checkReadWriteListArguments(buffer.length, offset, bytes); | 774 _FileUtils.checkReadWriteListArguments(buffer.length, offset, bytes); |
| 767 if (index != 0) { | 775 if (index != 0) { |
| 768 throw new IndexOutOfRangeException(index); | 776 throw new IndexOutOfRangeException(index); |
| 769 } | 777 } |
| 770 var result = _FileUtils.readList(_id, buffer, offset, bytes); | 778 var result = _FileUtils.readList(_id, buffer, offset, bytes); |
| 771 if (result is OSError) { | 779 if (result is OSError) { |
| 772 throw new FileIOException("readList failed", result); | 780 throw new FileIOException("readList failed for file '$_name'", |
| 781 result); |
| 773 } | 782 } |
| 774 return result; | 783 return result; |
| 775 } | 784 } |
| 776 | 785 |
| 777 void writeByte(int value) { | 786 void writeByte(int value) { |
| 778 _ensureFileService(); | 787 _ensureFileService(); |
| 779 if (value is !int) { | 788 if (value is !int) { |
| 780 _reportError(new FileIOException("Invalid argument to writeByte")); | 789 _reportError(new FileIOException( |
| 790 "Invalid argument to writeByte for file '$_name'")); |
| 781 return; | 791 return; |
| 782 } | 792 } |
| 783 List request = new List(3); | 793 List request = new List(3); |
| 784 request[0] = _FileUtils.kWriteByteRequest; | 794 request[0] = _FileUtils.kWriteByteRequest; |
| 785 request[1] = _id; | 795 request[1] = _id; |
| 786 request[2] = value; | 796 request[2] = value; |
| 787 _writeEnqueued(); | 797 _writeEnqueued(); |
| 788 _fileService.call(request).then((response) { | 798 _fileService.call(request).then((response) { |
| 789 _writeCompleted(); | 799 _writeCompleted(); |
| 790 if (_isErrorResponse(response)) { | 800 if (_isErrorResponse(response)) { |
| 791 _handleErrorResponse(response, "writeByte failed"); | 801 _handleErrorResponse(response, "writeByte failed for file '$_name'"); |
| 792 } | 802 } |
| 793 }); | 803 }); |
| 794 } | 804 } |
| 795 | 805 |
| 796 int writeByteSync(int value) { | 806 int writeByteSync(int value) { |
| 797 _checkNotClosed(); | 807 _checkNotClosed(); |
| 798 if (value is !int) { | 808 if (value is !int) { |
| 799 throw new FileIOException("Invalid argument to writeByte"); | 809 throw new FileIOException( |
| 810 "Invalid argument to writeByte for file '$_name'"); |
| 800 } | 811 } |
| 801 var result = _FileUtils.writeByte(_id, value); | 812 var result = _FileUtils.writeByte(_id, value); |
| 802 if (result is OSError) { | 813 if (result is OSError) { |
| 803 throw new FileIOException("writeByte failed", result); | 814 throw new FileIOException("writeByte failed for file '$_name'", |
| 815 result); |
| 804 } | 816 } |
| 805 return result; | 817 return result; |
| 806 } | 818 } |
| 807 | 819 |
| 808 void writeList(List<int> buffer, int offset, int bytes) { | 820 void writeList(List<int> buffer, int offset, int bytes) { |
| 809 _ensureFileService(); | 821 _ensureFileService(); |
| 810 if (buffer is !List || offset is !int || bytes is !int) { | 822 if (buffer is !List || offset is !int || bytes is !int) { |
| 811 _reportError(new FileIOException("Invalid arguments to writeList")); | 823 _reportError(new FileIOException( |
| 824 "Invalid arguments to writeList for file '$_name'")); |
| 812 return; | 825 return; |
| 813 } | 826 } |
| 814 | 827 |
| 815 List result = | 828 List result = |
| 816 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); | 829 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); |
| 817 List outBuffer = result[0]; | 830 List outBuffer = result[0]; |
| 818 int outOffset = result[1]; | 831 int outOffset = result[1]; |
| 819 | 832 |
| 820 List request = new List(5); | 833 List request = new List(5); |
| 821 request[0] = _FileUtils.kWriteListRequest; | 834 request[0] = _FileUtils.kWriteListRequest; |
| 822 request[1] = _id; | 835 request[1] = _id; |
| 823 request[2] = outBuffer; | 836 request[2] = outBuffer; |
| 824 request[3] = outOffset; | 837 request[3] = outOffset; |
| 825 request[4] = bytes; | 838 request[4] = bytes; |
| 826 _writeEnqueued(); | 839 _writeEnqueued(); |
| 827 _fileService.call(request).then((response) { | 840 _fileService.call(request).then((response) { |
| 828 _writeCompleted(); | 841 _writeCompleted(); |
| 829 if (_isErrorResponse(response)) { | 842 if (_isErrorResponse(response)) { |
| 830 _handleErrorResponse(response, "writeList failed"); | 843 _handleErrorResponse(response, "writeList failed for file '$_name'"); |
| 831 } | 844 } |
| 832 }); | 845 }); |
| 833 } | 846 } |
| 834 | 847 |
| 835 int writeListSync(List<int> buffer, int offset, int bytes) { | 848 int writeListSync(List<int> buffer, int offset, int bytes) { |
| 836 _checkNotClosed(); | 849 _checkNotClosed(); |
| 837 if (buffer is !List || offset is !int || bytes is !int) { | 850 if (buffer is !List || offset is !int || bytes is !int) { |
| 838 throw new FileIOException("Invalid arguments to writeList"); | 851 throw new FileIOException( |
| 852 "Invalid arguments to writeList for file '$_name'"); |
| 839 } | 853 } |
| 840 if (bytes == 0) return 0; | 854 if (bytes == 0) return 0; |
| 841 int index = | 855 int index = |
| 842 _FileUtils.checkReadWriteListArguments(buffer.length, offset, bytes); | 856 _FileUtils.checkReadWriteListArguments(buffer.length, offset, bytes); |
| 843 if (index != 0) { | 857 if (index != 0) { |
| 844 throw new IndexOutOfRangeException(index); | 858 throw new IndexOutOfRangeException(index); |
| 845 } | 859 } |
| 846 var result = _FileUtils.writeList(_id, buffer, offset, bytes); | 860 var result = _FileUtils.writeList(_id, buffer, offset, bytes); |
| 847 if (result is OSError) { | 861 if (result is OSError) { |
| 848 throw new FileIOException("writeList failed", result); | 862 throw new FileIOException("writeList failed for file '$_name'", result); |
| 849 } | 863 } |
| 850 return result; | 864 return result; |
| 851 } | 865 } |
| 852 | 866 |
| 853 void writeString(String string, [Encoding encoding = Encoding.UTF_8]) { | 867 void writeString(String string, [Encoding encoding = Encoding.UTF_8]) { |
| 854 _ensureFileService(); | 868 _ensureFileService(); |
| 855 List request = new List(3); | 869 List request = new List(3); |
| 856 request[0] = _FileUtils.kWriteStringRequest; | 870 request[0] = _FileUtils.kWriteStringRequest; |
| 857 request[1] = _id; | 871 request[1] = _id; |
| 858 request[2] = string; | 872 request[2] = string; |
| 859 _writeEnqueued(); | 873 _writeEnqueued(); |
| 860 _fileService.call(request).then((response) { | 874 _fileService.call(request).then((response) { |
| 861 _writeCompleted(); | 875 _writeCompleted(); |
| 862 if (_isErrorResponse(response)) { | 876 if (_isErrorResponse(response)) { |
| 863 _handleErrorResponse(response, "writeString failed"); | 877 _handleErrorResponse(response, "writeString failed for file '$_name'"); |
| 864 } | 878 } |
| 865 }); | 879 }); |
| 866 } | 880 } |
| 867 | 881 |
| 868 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { | 882 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { |
| 869 _checkNotClosed(); | 883 _checkNotClosed(); |
| 870 var result = _FileUtils.checkedWriteString(_id, string); | 884 var result = _FileUtils.checkedWriteString(_id, string); |
| 871 if (result is OSError) { | 885 if (result is OSError) { |
| 872 throw new FileIOException("writeString failed", result); | 886 throw new FileIOException("writeString failed for file '$_name'", result); |
| 873 } | 887 } |
| 874 return result; | 888 return result; |
| 875 } | 889 } |
| 876 | 890 |
| 877 void position(void callback(int position)) { | 891 void position(void callback(int position)) { |
| 878 _ensureFileService(); | 892 _ensureFileService(); |
| 879 List request = new List(2); | 893 List request = new List(2); |
| 880 request[0] = _FileUtils.kPositionRequest; | 894 request[0] = _FileUtils.kPositionRequest; |
| 881 request[1] = _id; | 895 request[1] = _id; |
| 882 _fileService.call(request).then((response) { | 896 _fileService.call(request).then((response) { |
| 883 if (_isErrorResponse(response)) { | 897 if (_isErrorResponse(response)) { |
| 884 _handleErrorResponse(response, "position failed"); | 898 _handleErrorResponse(response, "position failed for file '$_name'"); |
| 885 } else { | 899 } else { |
| 886 callback(response); | 900 callback(response); |
| 887 } | 901 } |
| 888 }); | 902 }); |
| 889 } | 903 } |
| 890 | 904 |
| 891 int positionSync() { | 905 int positionSync() { |
| 892 _checkNotClosed(); | 906 _checkNotClosed(); |
| 893 var result = _FileUtils.position(_id); | 907 var result = _FileUtils.position(_id); |
| 894 if (result is OSError) { | 908 if (result is OSError) { |
| 895 throw new FileIOException("position failed", result); | 909 throw new FileIOException("position failed for file '$_name'", result); |
| 896 } | 910 } |
| 897 return result; | 911 return result; |
| 898 } | 912 } |
| 899 | 913 |
| 900 void setPosition(int position, void callback()) { | 914 void setPosition(int position, void callback()) { |
| 901 _ensureFileService(); | 915 _ensureFileService(); |
| 902 List request = new List(3); | 916 List request = new List(3); |
| 903 request[0] = _FileUtils.kSetPositionRequest; | 917 request[0] = _FileUtils.kSetPositionRequest; |
| 904 request[1] = _id; | 918 request[1] = _id; |
| 905 request[2] = position; | 919 request[2] = position; |
| 906 _fileService.call(request).then((response) { | 920 _fileService.call(request).then((response) { |
| 907 if (_isErrorResponse(response)) { | 921 if (_isErrorResponse(response)) { |
| 908 _handleErrorResponse(response, "setPosition failed"); | 922 _handleErrorResponse(response, "setPosition failed for file '$_name'"); |
| 909 } else { | 923 } else { |
| 910 callback(); | 924 callback(); |
| 911 } | 925 } |
| 912 }); | 926 }); |
| 913 } | 927 } |
| 914 | 928 |
| 915 void setPositionSync(int position) { | 929 void setPositionSync(int position) { |
| 916 _checkNotClosed(); | 930 _checkNotClosed(); |
| 917 var result = _FileUtils.setPosition(_id, position); | 931 var result = _FileUtils.setPosition(_id, position); |
| 918 if (result is OSError) { | 932 if (result is OSError) { |
| 919 throw new FileIOException("setPosition failed", result); | 933 throw new FileIOException("setPosition failed for file '$_name'", result); |
| 920 } | 934 } |
| 921 } | 935 } |
| 922 | 936 |
| 923 void truncate(int length, void callback()) { | 937 void truncate(int length, void callback()) { |
| 924 _ensureFileService(); | 938 _ensureFileService(); |
| 925 List request = new List(3); | 939 List request = new List(3); |
| 926 request[0] = _FileUtils.kTruncateRequest; | 940 request[0] = _FileUtils.kTruncateRequest; |
| 927 request[1] = _id; | 941 request[1] = _id; |
| 928 request[2] = length; | 942 request[2] = length; |
| 929 _fileService.call(request).then((response) { | 943 _fileService.call(request).then((response) { |
| 930 if (_isErrorResponse(response)) { | 944 if (_isErrorResponse(response)) { |
| 931 _handleErrorResponse(response, "truncate failed"); | 945 _handleErrorResponse(response, "truncate failed for file '$_name'"); |
| 932 } else { | 946 } else { |
| 933 callback(); | 947 callback(); |
| 934 } | 948 } |
| 935 }); | 949 }); |
| 936 } | 950 } |
| 937 | 951 |
| 938 void truncateSync(int length) { | 952 void truncateSync(int length) { |
| 939 _checkNotClosed(); | 953 _checkNotClosed(); |
| 940 var result = _FileUtils.truncate(_id, length); | 954 var result = _FileUtils.truncate(_id, length); |
| 941 if (result is OSError) { | 955 if (result is OSError) { |
| 942 throw new FileIOException("truncate failed", result); | 956 throw new FileIOException("truncate failed for file '$_name'", result); |
| 943 } | 957 } |
| 944 } | 958 } |
| 945 | 959 |
| 946 void length(void callback(int length)) { | 960 void length(void callback(int length)) { |
| 947 _ensureFileService(); | 961 _ensureFileService(); |
| 948 List request = new List(2); | 962 List request = new List(2); |
| 949 request[0] = _FileUtils.kLengthRequest; | 963 request[0] = _FileUtils.kLengthRequest; |
| 950 request[1] = _id; | 964 request[1] = _id; |
| 951 _fileService.call(request).then((response) { | 965 _fileService.call(request).then((response) { |
| 952 if (_isErrorResponse(response)) { | 966 if (_isErrorResponse(response)) { |
| 953 _handleErrorResponse(response, "length failed"); | 967 _handleErrorResponse(response, "length failed for file '$_name'"); |
| 954 } else { | 968 } else { |
| 955 callback(response); | 969 callback(response); |
| 956 } | 970 } |
| 957 }); | 971 }); |
| 958 } | 972 } |
| 959 | 973 |
| 960 int lengthSync() { | 974 int lengthSync() { |
| 961 _checkNotClosed(); | 975 _checkNotClosed(); |
| 962 var result = _FileUtils.length(_id); | 976 var result = _FileUtils.length(_id); |
| 963 if (result is OSError) { | 977 if (result is OSError) { |
| 964 throw new FileIOException("length failed", result); | 978 throw new FileIOException("length failed for file '$_name'", result); |
| 965 } | 979 } |
| 966 return result; | 980 return result; |
| 967 } | 981 } |
| 968 | 982 |
| 969 void flush(void callback()) { | 983 void flush(void callback()) { |
| 970 _ensureFileService(); | 984 _ensureFileService(); |
| 971 List request = new List(2); | 985 List request = new List(2); |
| 972 request[0] = _FileUtils.kFlushRequest; | 986 request[0] = _FileUtils.kFlushRequest; |
| 973 request[1] = _id; | 987 request[1] = _id; |
| 974 _fileService.call(request).then((response) { | 988 _fileService.call(request).then((response) { |
| 975 if (_isErrorResponse(response)) { | 989 if (_isErrorResponse(response)) { |
| 976 _handleErrorResponse(response, "flush failed"); | 990 _handleErrorResponse(response, "flush failed for file '$_name'"); |
| 977 } else { | 991 } else { |
| 978 callback(); | 992 callback(); |
| 979 } | 993 } |
| 980 }); | 994 }); |
| 981 } | 995 } |
| 982 | 996 |
| 983 void flushSync() { | 997 void flushSync() { |
| 984 _checkNotClosed(); | 998 _checkNotClosed(); |
| 985 var result = _FileUtils.flush(_id); | 999 var result = _FileUtils.flush(_id); |
| 986 if (result is OSError) { | 1000 if (result is OSError) { |
| 987 throw new FileIOException("flush failed", result); | 1001 throw new FileIOException("flush failed for file '$_name'", result); |
| 988 } | 1002 } |
| 989 } | 1003 } |
| 990 | 1004 |
| 991 String get name() => _name; | 1005 String get name() => _name; |
| 992 | 1006 |
| 993 void set onNoPendingWrites(void handler()) { | 1007 void set onNoPendingWrites(void handler()) { |
| 994 _onNoPendingWrites = handler; | 1008 _onNoPendingWrites = handler; |
| 995 if (_pendingWrites == 0) { | 1009 if (_pendingWrites == 0) { |
| 996 _noPendingWriteTimer = new Timer(0, (t) { | 1010 _noPendingWriteTimer = new Timer(0, (t) { |
| 997 if (_onNoPendingWrites != null) _onNoPendingWrites(); | 1011 if (_onNoPendingWrites != null) _onNoPendingWrites(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1015 | 1029 |
| 1016 void _writeCompleted() { | 1030 void _writeCompleted() { |
| 1017 _pendingWrites--; | 1031 _pendingWrites--; |
| 1018 if (_pendingWrites == 0 && _onNoPendingWrites != null) { | 1032 if (_pendingWrites == 0 && _onNoPendingWrites != null) { |
| 1019 _onNoPendingWrites(); | 1033 _onNoPendingWrites(); |
| 1020 } | 1034 } |
| 1021 } | 1035 } |
| 1022 | 1036 |
| 1023 void _checkNotClosed() { | 1037 void _checkNotClosed() { |
| 1024 if (_id == 0) { | 1038 if (_id == 0) { |
| 1025 throw new FileIOException("File closed"); | 1039 throw new FileIOException("File closed '$_name'"); |
| 1026 } | 1040 } |
| 1027 } | 1041 } |
| 1028 | 1042 |
| 1029 String _name; | 1043 String _name; |
| 1030 int _id; | 1044 int _id; |
| 1031 int _pendingWrites = 0; | 1045 int _pendingWrites = 0; |
| 1032 | 1046 |
| 1033 SendPort _fileService; | 1047 SendPort _fileService; |
| 1034 | 1048 |
| 1035 Timer _noPendingWriteTimer; | 1049 Timer _noPendingWriteTimer; |
| 1036 | 1050 |
| 1037 Function _onNoPendingWrites; | 1051 Function _onNoPendingWrites; |
| 1038 } | 1052 } |
| OLD | NEW |