| 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 var file = new File(name); | 7 var file = new File(name); |
| 8 _data = []; | 8 _data = []; |
| 9 _position = 0; | 9 _position = 0; |
| 10 var chained = file.open(FileMode.READ).chain((openedFile) { | 10 var chained = file.open(FileMode.READ).chain((openedFile) { |
| (...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 } | 415 } |
| 416 } | 416 } |
| 417 | 417 |
| 418 // Class for encapsulating the native implementation of files. | 418 // Class for encapsulating the native implementation of files. |
| 419 class _File extends _FileBase implements File { | 419 class _File extends _FileBase implements File { |
| 420 // Constructor for file. | 420 // Constructor for file. |
| 421 _File(String this._name); | 421 _File(String this._name); |
| 422 | 422 |
| 423 Future<bool> exists() { | 423 Future<bool> exists() { |
| 424 _ensureFileService(); | 424 _ensureFileService(); |
| 425 Completer<bool> completer = new Completer<bool>(); | |
| 426 List request = new List(2); | 425 List request = new List(2); |
| 427 request[0] = _FileUtils.kExistsRequest; | 426 request[0] = _FileUtils.kExistsRequest; |
| 428 request[1] = _name; | 427 request[1] = _name; |
| 429 _fileService.call(request).then((response) { | 428 return _fileService.call(request).transform((response) { |
| 430 if (_isErrorResponse(response)) { | 429 if (_isErrorResponse(response)) { |
| 431 var e = _exceptionFromResponse(response, "Cannot open file '$_name'"); | 430 throw _exceptionFromResponse(response, "Cannot open file '$_name'"); |
| 432 completer.completeException(e); | |
| 433 } else { | |
| 434 completer.complete(response); | |
| 435 } | 431 } |
| 432 return response; |
| 436 }); | 433 }); |
| 437 return completer.future; | |
| 438 } | 434 } |
| 439 | 435 |
| 440 bool existsSync() { | 436 bool existsSync() { |
| 441 return _FileUtils.checkedExists(_name); | 437 return _FileUtils.checkedExists(_name); |
| 442 } | 438 } |
| 443 | 439 |
| 444 Future<File> create() { | 440 Future<File> create() { |
| 445 _ensureFileService(); | 441 _ensureFileService(); |
| 446 Completer<File> completer = new Completer<File>(); | |
| 447 List request = new List(2); | 442 List request = new List(2); |
| 448 request[0] = _FileUtils.kCreateRequest; | 443 request[0] = _FileUtils.kCreateRequest; |
| 449 request[1] = _name; | 444 request[1] = _name; |
| 450 _fileService.call(request).then((response) { | 445 return _fileService.call(request).transform((response) { |
| 451 if (_isErrorResponse(response)) { | 446 if (_isErrorResponse(response)) { |
| 452 var e = _exceptionFromResponse(response, "Cannot create file '$_name'"); | 447 throw _exceptionFromResponse(response, "Cannot create file '$_name'"); |
| 453 completer.completeException(e); | |
| 454 } else { | |
| 455 completer.complete(this); | |
| 456 } | 448 } |
| 449 return this; |
| 457 }); | 450 }); |
| 458 return completer.future; | |
| 459 } | 451 } |
| 460 | 452 |
| 461 void createSync() { | 453 void createSync() { |
| 462 bool created = _FileUtils.checkedCreate(_name); | 454 bool created = _FileUtils.checkedCreate(_name); |
| 463 if (!created) { | 455 if (!created) { |
| 464 throw new FileIOException("Cannot create file '$_name'"); | 456 throw new FileIOException("Cannot create file '$_name'"); |
| 465 } | 457 } |
| 466 } | 458 } |
| 467 | 459 |
| 468 Future<File> delete() { | 460 Future<File> delete() { |
| 469 _ensureFileService(); | 461 _ensureFileService(); |
| 470 Completer<File> completer = new Completer<File>(); | |
| 471 List request = new List(2); | 462 List request = new List(2); |
| 472 request[0] = _FileUtils.kDeleteRequest; | 463 request[0] = _FileUtils.kDeleteRequest; |
| 473 request[1] = _name; | 464 request[1] = _name; |
| 474 _fileService.call(request).then((response) { | 465 return _fileService.call(request).transform((response) { |
| 475 if (_isErrorResponse(response)) { | 466 if (_isErrorResponse(response)) { |
| 476 var e = _exceptionFromResponse(response, "Cannot delete file '$_name'"); | 467 throw _exceptionFromResponse(response, "Cannot delete file '$_name'"); |
| 477 completer.completeException(e); | |
| 478 } else { | |
| 479 completer.complete(this); | |
| 480 } | 468 } |
| 469 return this; |
| 481 }); | 470 }); |
| 482 return completer.future; | |
| 483 } | 471 } |
| 484 | 472 |
| 485 void deleteSync() { | 473 void deleteSync() { |
| 486 _FileUtils.checkedDelete(_name); | 474 _FileUtils.checkedDelete(_name); |
| 487 } | 475 } |
| 488 | 476 |
| 489 Future<Directory> directory() { | 477 Future<Directory> directory() { |
| 490 _ensureFileService(); | 478 _ensureFileService(); |
| 491 Completer<Directory> completer = new Completer<Directory>(); | |
| 492 List request = new List(2); | 479 List request = new List(2); |
| 493 request[0] = _FileUtils.kDirectoryRequest; | 480 request[0] = _FileUtils.kDirectoryRequest; |
| 494 request[1] = _name; | 481 request[1] = _name; |
| 495 _fileService.call(request).then((response) { | 482 return _fileService.call(request).transform((response) { |
| 496 if (_isErrorResponse(response)) { | 483 if (_isErrorResponse(response)) { |
| 497 var e = _exceptionFromResponse(response, | 484 throw _exceptionFromResponse(response, |
| 498 "Cannot retrieve directory for " | 485 "Cannot retrieve directory for " |
| 499 "file '$_name'"); | 486 "file '$_name'"); |
| 500 completer.completeException(e); | |
| 501 } else { | |
| 502 completer.complete(new Directory(response)); | |
| 503 } | 487 } |
| 488 return new Directory(response); |
| 504 }); | 489 }); |
| 505 return completer.future; | |
| 506 } | 490 } |
| 507 | 491 |
| 508 Directory directorySync() { | 492 Directory directorySync() { |
| 509 _FileUtils.checkedDirectory(_name); | 493 _FileUtils.checkedDirectory(_name); |
| 510 return new Directory(_FileUtils.directory(_name)); | 494 return new Directory(_FileUtils.directory(_name)); |
| 511 } | 495 } |
| 512 | 496 |
| 513 Future<RandomAccessFile> open([FileMode mode = FileMode.READ]) { | 497 Future<RandomAccessFile> open([FileMode mode = FileMode.READ]) { |
| 514 _ensureFileService(); | 498 _ensureFileService(); |
| 515 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 499 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 516 if (mode != FileMode.READ && | 500 if (mode != FileMode.READ && |
| 517 mode != FileMode.WRITE && | 501 mode != FileMode.WRITE && |
| 518 mode != FileMode.APPEND) { | 502 mode != FileMode.APPEND) { |
| 519 new Timer(0, (t) { | 503 new Timer(0, (t) { |
| 520 completer.completeException(new IllegalArgumentException()); | 504 completer.completeException(new IllegalArgumentException()); |
| 521 }); | 505 }); |
| 522 return completer.future; | 506 return completer.future; |
| 523 } | 507 } |
| 524 List request = new List(3); | 508 List request = new List(3); |
| 525 request[0] = _FileUtils.kOpenRequest; | 509 request[0] = _FileUtils.kOpenRequest; |
| 526 request[1] = _name; | 510 request[1] = _name; |
| 527 request[2] = mode._mode; // Direct int value for serialization. | 511 request[2] = mode._mode; // Direct int value for serialization. |
| 528 _fileService.call(request).then((response) { | 512 return _fileService.call(request).transform((response) { |
| 529 if (_isErrorResponse(response)) { | 513 if (_isErrorResponse(response)) { |
| 530 var e = _exceptionFromResponse(response, "Cannot open file '$_name'"); | 514 throw _exceptionFromResponse(response, "Cannot open file '$_name'"); |
| 531 completer.completeException(e); | |
| 532 } else { | |
| 533 completer.complete(new _RandomAccessFile(response, _name)); | |
| 534 } | 515 } |
| 516 return new _RandomAccessFile(response, _name); |
| 535 }); | 517 }); |
| 536 return completer.future; | |
| 537 } | 518 } |
| 538 | 519 |
| 539 Future<int> length() { | 520 Future<int> length() { |
| 540 _ensureFileService(); | 521 _ensureFileService(); |
| 541 Completer<int> completer = new Completer<int>(); | |
| 542 List request = new List(2); | 522 List request = new List(2); |
| 543 request[0] = _FileUtils.kLengthFromNameRequest; | 523 request[0] = _FileUtils.kLengthFromNameRequest; |
| 544 request[1] = _name; | 524 request[1] = _name; |
| 545 _fileService.call(request).then((response) { | 525 return _fileService.call(request).transform((response) { |
| 546 if (_isErrorResponse(response)) { | 526 if (_isErrorResponse(response)) { |
| 547 var e = _exceptionFromResponse(response, | 527 throw _exceptionFromResponse(response, |
| 548 "Cannot retrieve length of " | 528 "Cannot retrieve length of " |
| 549 "file '$_name'"); | 529 "file '$_name'"); |
| 550 completer.completeException(e); | |
| 551 } else { | |
| 552 completer.complete(response); | |
| 553 } | 530 } |
| 531 return response; |
| 554 }); | 532 }); |
| 555 return completer.future; | |
| 556 } | 533 } |
| 557 | 534 |
| 558 int lengthSync() { | 535 int lengthSync() { |
| 559 var result = _FileUtils.checkedLengthFromName(_name); | 536 var result = _FileUtils.checkedLengthFromName(_name); |
| 560 if (result is OSError) { | 537 if (result is OSError) { |
| 561 throw new FileIOException("Cannot retrieve length of file '$_name'", | 538 throw new FileIOException("Cannot retrieve length of file '$_name'", |
| 562 result); | 539 result); |
| 563 } | 540 } |
| 564 return result; | 541 return result; |
| 565 } | 542 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 579 static RandomAccessFile _openStdioSync(int fd) { | 556 static RandomAccessFile _openStdioSync(int fd) { |
| 580 var id = _FileUtils.openStdio(fd); | 557 var id = _FileUtils.openStdio(fd); |
| 581 if (id == 0) { | 558 if (id == 0) { |
| 582 throw new FileIOException("Cannot open stdio file for: $fd"); | 559 throw new FileIOException("Cannot open stdio file for: $fd"); |
| 583 } | 560 } |
| 584 return new _RandomAccessFile(id, ""); | 561 return new _RandomAccessFile(id, ""); |
| 585 } | 562 } |
| 586 | 563 |
| 587 Future<String> fullPath() { | 564 Future<String> fullPath() { |
| 588 _ensureFileService(); | 565 _ensureFileService(); |
| 589 Completer<String> completer = new Completer<String>(); | |
| 590 List request = new List(2); | 566 List request = new List(2); |
| 591 request[0] = _FileUtils.kFullPathRequest; | 567 request[0] = _FileUtils.kFullPathRequest; |
| 592 request[1] = _name; | 568 request[1] = _name; |
| 593 _fileService.call(request).then((response) { | 569 return _fileService.call(request).transform((response) { |
| 594 if (_isErrorResponse(response)) { | 570 if (_isErrorResponse(response)) { |
| 595 var e = _exceptionFromResponse(response, | 571 throw _exceptionFromResponse(response, |
| 596 "Cannot retrieve full path" | 572 "Cannot retrieve full path" |
| 597 " for '$_name'"); | 573 " for '$_name'"); |
| 598 completer.completeException(e); | |
| 599 } else { | |
| 600 completer.complete(response); | |
| 601 } | 574 } |
| 575 return response; |
| 602 }); | 576 }); |
| 603 return completer.future; | |
| 604 } | 577 } |
| 605 | 578 |
| 606 String fullPathSync() { | 579 String fullPathSync() { |
| 607 return _FileUtils.checkedFullPath(_name); | 580 return _FileUtils.checkedFullPath(_name); |
| 608 } | 581 } |
| 609 | 582 |
| 610 InputStream openInputStream() { | 583 InputStream openInputStream() { |
| 611 return new _FileInputStream(_name); | 584 return new _FileInputStream(_name); |
| 612 } | 585 } |
| 613 | 586 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 717 Future<RandomAccessFile> close() { | 690 Future<RandomAccessFile> close() { |
| 718 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 691 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 719 if (_isClosed) return _completeWithClosedException(completer); | 692 if (_isClosed) return _completeWithClosedException(completer); |
| 720 _ensureFileService(); | 693 _ensureFileService(); |
| 721 List request = new List(2); | 694 List request = new List(2); |
| 722 request[0] = _FileUtils.kCloseRequest; | 695 request[0] = _FileUtils.kCloseRequest; |
| 723 request[1] = _id; | 696 request[1] = _id; |
| 724 // Set the id_ to 0 (NULL) to ensure the no more async requests | 697 // Set the id_ to 0 (NULL) to ensure the no more async requests |
| 725 // can be issues for this file. | 698 // can be issues for this file. |
| 726 _id = 0; | 699 _id = 0; |
| 727 _fileService.call(request).then((result) { | 700 return _fileService.call(request).transform((result) { |
| 728 if (result != -1) { | 701 if (result != -1) { |
| 729 _id = result; | 702 _id = result; |
| 730 completer.complete(this); | 703 return this; |
| 731 } else { | 704 } else { |
| 732 completer.completeException( | 705 throw new FileIOException("Cannot close file '$_name'"); |
| 733 new FileIOException("Cannot close file '$_name'")); | |
| 734 } | 706 } |
| 735 }); | 707 }); |
| 736 return completer.future; | |
| 737 } | 708 } |
| 738 | 709 |
| 739 void closeSync() { | 710 void closeSync() { |
| 740 var id = _FileUtils.close(_id); | 711 var id = _FileUtils.close(_id); |
| 741 if (id == -1) { | 712 if (id == -1) { |
| 742 throw new FileIOException("Cannot close file '$_name'"); | 713 throw new FileIOException("Cannot close file '$_name'"); |
| 743 } | 714 } |
| 744 _id = id; | 715 _id = id; |
| 745 } | 716 } |
| 746 | 717 |
| 747 Future<int> readByte() { | 718 Future<int> readByte() { |
| 748 _ensureFileService(); | 719 _ensureFileService(); |
| 749 Completer<int> completer = new Completer<int>(); | 720 Completer<int> completer = new Completer<int>(); |
| 750 if (_isClosed) return _completeWithClosedException(completer); | 721 if (_isClosed) return _completeWithClosedException(completer); |
| 751 List request = new List(2); | 722 List request = new List(2); |
| 752 request[0] = _FileUtils.kReadByteRequest; | 723 request[0] = _FileUtils.kReadByteRequest; |
| 753 request[1] = _id; | 724 request[1] = _id; |
| 754 _fileService.call(request).then((response) { | 725 return _fileService.call(request).transform((response) { |
| 755 if (_isErrorResponse(response)) { | 726 if (_isErrorResponse(response)) { |
| 756 var e = _exceptionFromResponse(response, | 727 throw _exceptionFromResponse(response, |
| 757 "readByte failed for file '$_name'"); | 728 "readByte failed for file '$_name'"); |
| 758 completer.completeException(e); | |
| 759 } else { | |
| 760 completer.complete(response); | |
| 761 } | 729 } |
| 730 return response; |
| 762 }); | 731 }); |
| 763 return completer.future; | |
| 764 } | 732 } |
| 765 | 733 |
| 766 int readByteSync() { | 734 int readByteSync() { |
| 767 _checkNotClosed(); | 735 _checkNotClosed(); |
| 768 var result = _FileUtils.readByte(_id); | 736 var result = _FileUtils.readByte(_id); |
| 769 if (result is OSError) { | 737 if (result is OSError) { |
| 770 throw new FileIOException("readByte failed for file '$_name'", result); | 738 throw new FileIOException("readByte failed for file '$_name'", result); |
| 771 } | 739 } |
| 772 return result; | 740 return result; |
| 773 } | 741 } |
| 774 | 742 |
| 775 Future<int> readList(List<int> buffer, int offset, int bytes) { | 743 Future<int> readList(List<int> buffer, int offset, int bytes) { |
| 776 _ensureFileService(); | 744 _ensureFileService(); |
| 777 Completer<int> completer = new Completer<int>(); | 745 Completer<int> completer = new Completer<int>(); |
| 778 if (buffer is !List || offset is !int || bytes is !int) { | 746 if (buffer is !List || offset is !int || bytes is !int) { |
| 779 // Complete asynchronously so the user has a chance to setup | 747 // Complete asynchronously so the user has a chance to setup |
| 780 // handlers without getting exceptions when registering the | 748 // handlers without getting exceptions when registering the |
| 781 // then handler. | 749 // then handler. |
| 782 new Timer(0, (t) { | 750 new Timer(0, (t) { |
| 783 completer.completeException(new FileIOException( | 751 completer.completeException(new FileIOException( |
| 784 "Invalid arguments to readList for file '$_name'")); | 752 "Invalid arguments to readList for file '$_name'")); |
| 785 }); | 753 }); |
| 786 return completer.future; | 754 return completer.future; |
| 787 }; | 755 }; |
| 788 if (_isClosed) return _completeWithClosedException(completer); | 756 if (_isClosed) return _completeWithClosedException(completer); |
| 789 List request = new List(3); | 757 List request = new List(3); |
| 790 request[0] = _FileUtils.kReadListRequest; | 758 request[0] = _FileUtils.kReadListRequest; |
| 791 request[1] = _id; | 759 request[1] = _id; |
| 792 request[2] = bytes; | 760 request[2] = bytes; |
| 793 _fileService.call(request).then((response) { | 761 return _fileService.call(request).transform((response) { |
| 794 if (_isErrorResponse(response)) { | 762 if (_isErrorResponse(response)) { |
| 795 var e = _exceptionFromResponse(response, | 763 throw _exceptionFromResponse(response, |
| 796 "readList failed for file '$_name'"); | 764 "readList failed for file '$_name'"); |
| 797 completer.completeException(e); | |
| 798 } else { | |
| 799 var read = response[1]; | |
| 800 var data = response[2]; | |
| 801 buffer.setRange(offset, read, data); | |
| 802 completer.complete(read); | |
| 803 } | 765 } |
| 766 var read = response[1]; |
| 767 var data = response[2]; |
| 768 buffer.setRange(offset, read, data); |
| 769 return read; |
| 804 }); | 770 }); |
| 805 return completer.future; | |
| 806 } | 771 } |
| 807 | 772 |
| 808 int readListSync(List<int> buffer, int offset, int bytes) { | 773 int readListSync(List<int> buffer, int offset, int bytes) { |
| 809 _checkNotClosed(); | 774 _checkNotClosed(); |
| 810 if (buffer is !List || offset is !int || bytes is !int) { | 775 if (buffer is !List || offset is !int || bytes is !int) { |
| 811 throw new FileIOException( | 776 throw new FileIOException( |
| 812 "Invalid arguments to readList for file '$_name'"); | 777 "Invalid arguments to readList for file '$_name'"); |
| 813 } | 778 } |
| 814 if (bytes == 0) return 0; | 779 if (bytes == 0) return 0; |
| 815 int index = | 780 int index = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 836 completer.completeException(new FileIOException( | 801 completer.completeException(new FileIOException( |
| 837 "Invalid argument to writeByte for file '$_name'")); | 802 "Invalid argument to writeByte for file '$_name'")); |
| 838 }); | 803 }); |
| 839 return completer.future; | 804 return completer.future; |
| 840 } | 805 } |
| 841 if (_isClosed) return _completeWithClosedException(completer); | 806 if (_isClosed) return _completeWithClosedException(completer); |
| 842 List request = new List(3); | 807 List request = new List(3); |
| 843 request[0] = _FileUtils.kWriteByteRequest; | 808 request[0] = _FileUtils.kWriteByteRequest; |
| 844 request[1] = _id; | 809 request[1] = _id; |
| 845 request[2] = value; | 810 request[2] = value; |
| 846 _fileService.call(request).then((response) { | 811 return _fileService.call(request).transform((response) { |
| 847 if (_isErrorResponse(response)) { | 812 if (_isErrorResponse(response)) { |
| 848 var e = _exceptionFromResponse(response, | 813 throw _exceptionFromResponse(response, |
| 849 "writeByte failed for file '$_name'"); | 814 "writeByte failed for file '$_name'"); |
| 850 completer.completeException(e); | |
| 851 } else { | |
| 852 completer.complete(this); | |
| 853 } | 815 } |
| 816 return this; |
| 854 }); | 817 }); |
| 855 return completer.future; | |
| 856 } | 818 } |
| 857 | 819 |
| 858 int writeByteSync(int value) { | 820 int writeByteSync(int value) { |
| 859 _checkNotClosed(); | 821 _checkNotClosed(); |
| 860 if (value is !int) { | 822 if (value is !int) { |
| 861 throw new FileIOException( | 823 throw new FileIOException( |
| 862 "Invalid argument to writeByte for file '$_name'"); | 824 "Invalid argument to writeByte for file '$_name'"); |
| 863 } | 825 } |
| 864 var result = _FileUtils.writeByte(_id, value); | 826 var result = _FileUtils.writeByte(_id, value); |
| 865 if (result is OSError) { | 827 if (result is OSError) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 877 // handlers without getting exceptions when registering the | 839 // handlers without getting exceptions when registering the |
| 878 // then handler. | 840 // then handler. |
| 879 new Timer(0, (t) { | 841 new Timer(0, (t) { |
| 880 completer.completeException(new FileIOException( | 842 completer.completeException(new FileIOException( |
| 881 "Invalid arguments to writeList for file '$_name'")); | 843 "Invalid arguments to writeList for file '$_name'")); |
| 882 }); | 844 }); |
| 883 return completer.future; | 845 return completer.future; |
| 884 } | 846 } |
| 885 if (_isClosed) return _completeWithClosedException(completer); | 847 if (_isClosed) return _completeWithClosedException(completer); |
| 886 | 848 |
| 887 List result = | 849 List result; |
| 888 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); | 850 try { |
| 851 result = |
| 852 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); |
| 853 } catch (var e) { |
| 854 // Complete asynchronously so the user has a chance to setup |
| 855 // handlers without getting exceptions when registering the |
| 856 // then handler. |
| 857 new Timer(0, (t) => completer.completeException(e)); |
| 858 return completer.future; |
| 859 } |
| 889 List outBuffer = result[0]; | 860 List outBuffer = result[0]; |
| 890 int outOffset = result[1]; | 861 int outOffset = result[1]; |
| 891 | 862 |
| 892 List request = new List(5); | 863 List request = new List(5); |
| 893 request[0] = _FileUtils.kWriteListRequest; | 864 request[0] = _FileUtils.kWriteListRequest; |
| 894 request[1] = _id; | 865 request[1] = _id; |
| 895 request[2] = outBuffer; | 866 request[2] = outBuffer; |
| 896 request[3] = outOffset; | 867 request[3] = outOffset; |
| 897 request[4] = bytes; | 868 request[4] = bytes; |
| 898 _fileService.call(request).then((response) { | 869 return _fileService.call(request).transform((response) { |
| 899 if (_isErrorResponse(response)) { | 870 if (_isErrorResponse(response)) { |
| 900 var e = _exceptionFromResponse(response, | 871 throw _exceptionFromResponse(response, |
| 901 "writeList failed for file '$_name'"); | 872 "writeList failed for file '$_name'"); |
| 902 completer.completeException(e); | |
| 903 } else { | |
| 904 completer.complete(this); | |
| 905 } | 873 } |
| 874 return this; |
| 906 }); | 875 }); |
| 907 return completer.future; | |
| 908 } | 876 } |
| 909 | 877 |
| 910 int writeListSync(List<int> buffer, int offset, int bytes) { | 878 int writeListSync(List<int> buffer, int offset, int bytes) { |
| 911 _checkNotClosed(); | 879 _checkNotClosed(); |
| 912 if (buffer is !List || offset is !int || bytes is !int) { | 880 if (buffer is !List || offset is !int || bytes is !int) { |
| 913 throw new FileIOException( | 881 throw new FileIOException( |
| 914 "Invalid arguments to writeList for file '$_name'"); | 882 "Invalid arguments to writeList for file '$_name'"); |
| 915 } | 883 } |
| 916 if (bytes == 0) return 0; | 884 if (bytes == 0) return 0; |
| 917 int index = | 885 int index = |
| (...skipping 10 matching lines...) Expand all Loading... |
| 928 | 896 |
| 929 Future<RandomAccessFile> writeString(String string, | 897 Future<RandomAccessFile> writeString(String string, |
| 930 [Encoding encoding = Encoding.UTF_8]) { | 898 [Encoding encoding = Encoding.UTF_8]) { |
| 931 _ensureFileService(); | 899 _ensureFileService(); |
| 932 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 900 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 933 if (_isClosed) return _completeWithClosedException(completer); | 901 if (_isClosed) return _completeWithClosedException(completer); |
| 934 List request = new List(3); | 902 List request = new List(3); |
| 935 request[0] = _FileUtils.kWriteStringRequest; | 903 request[0] = _FileUtils.kWriteStringRequest; |
| 936 request[1] = _id; | 904 request[1] = _id; |
| 937 request[2] = string; | 905 request[2] = string; |
| 938 _fileService.call(request).then((response) { | 906 return _fileService.call(request).transform((response) { |
| 939 if (_isErrorResponse(response)) { | 907 if (_isErrorResponse(response)) { |
| 940 var e = _exceptionFromResponse(response, | 908 throw _exceptionFromResponse(response, |
| 941 "writeString failed for file '$_name'"); | 909 "writeString failed for file '$_name'"); |
| 942 completer.completeException(e); | |
| 943 } else { | |
| 944 completer.complete(this); | |
| 945 } | 910 } |
| 911 return this; |
| 946 }); | 912 }); |
| 947 return completer.future; | |
| 948 } | 913 } |
| 949 | 914 |
| 950 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { | 915 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { |
| 951 _checkNotClosed(); | 916 _checkNotClosed(); |
| 952 var result = _FileUtils.checkedWriteString(_id, string); | 917 var result = _FileUtils.checkedWriteString(_id, string); |
| 953 if (result is OSError) { | 918 if (result is OSError) { |
| 954 throw new FileIOException("writeString failed for file '$_name'", result); | 919 throw new FileIOException("writeString failed for file '$_name'", result); |
| 955 } | 920 } |
| 956 return result; | 921 return result; |
| 957 } | 922 } |
| 958 | 923 |
| 959 Future<int> position() { | 924 Future<int> position() { |
| 960 _ensureFileService(); | 925 _ensureFileService(); |
| 961 Completer<int> completer = new Completer<int>(); | 926 Completer<int> completer = new Completer<int>(); |
| 962 if (_isClosed) return _completeWithClosedException(completer); | 927 if (_isClosed) return _completeWithClosedException(completer); |
| 963 List request = new List(2); | 928 List request = new List(2); |
| 964 request[0] = _FileUtils.kPositionRequest; | 929 request[0] = _FileUtils.kPositionRequest; |
| 965 request[1] = _id; | 930 request[1] = _id; |
| 966 _fileService.call(request).then((response) { | 931 return _fileService.call(request).transform((response) { |
| 967 if (_isErrorResponse(response)) { | 932 if (_isErrorResponse(response)) { |
| 968 var e = _exceptionFromResponse(response, | 933 throw _exceptionFromResponse(response, |
| 969 "position failed for file '$_name'"); | 934 "position failed for file '$_name'"); |
| 970 completer.completeException(e); | |
| 971 } else { | |
| 972 completer.complete(response); | |
| 973 } | 935 } |
| 936 return response; |
| 974 }); | 937 }); |
| 975 return completer.future; | |
| 976 } | 938 } |
| 977 | 939 |
| 978 int positionSync() { | 940 int positionSync() { |
| 979 _checkNotClosed(); | 941 _checkNotClosed(); |
| 980 var result = _FileUtils.position(_id); | 942 var result = _FileUtils.position(_id); |
| 981 if (result is OSError) { | 943 if (result is OSError) { |
| 982 throw new FileIOException("position failed for file '$_name'", result); | 944 throw new FileIOException("position failed for file '$_name'", result); |
| 983 } | 945 } |
| 984 return result; | 946 return result; |
| 985 } | 947 } |
| 986 | 948 |
| 987 Future<RandomAccessFile> setPosition(int position) { | 949 Future<RandomAccessFile> setPosition(int position) { |
| 988 _ensureFileService(); | 950 _ensureFileService(); |
| 989 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 951 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 990 if (_isClosed) return _completeWithClosedException(completer); | 952 if (_isClosed) return _completeWithClosedException(completer); |
| 991 List request = new List(3); | 953 List request = new List(3); |
| 992 request[0] = _FileUtils.kSetPositionRequest; | 954 request[0] = _FileUtils.kSetPositionRequest; |
| 993 request[1] = _id; | 955 request[1] = _id; |
| 994 request[2] = position; | 956 request[2] = position; |
| 995 _fileService.call(request).then((response) { | 957 return _fileService.call(request).transform((response) { |
| 996 if (_isErrorResponse(response)) { | 958 if (_isErrorResponse(response)) { |
| 997 var e = _exceptionFromResponse(response, | 959 throw _exceptionFromResponse(response, |
| 998 "setPosition failed for file '$_name'"); | 960 "setPosition failed for file '$_name'"); |
| 999 completer.completeException(e); | |
| 1000 } else { | |
| 1001 completer.complete(this); | |
| 1002 } | 961 } |
| 962 return this; |
| 1003 }); | 963 }); |
| 1004 return completer.future; | |
| 1005 } | 964 } |
| 1006 | 965 |
| 1007 void setPositionSync(int position) { | 966 void setPositionSync(int position) { |
| 1008 _checkNotClosed(); | 967 _checkNotClosed(); |
| 1009 var result = _FileUtils.setPosition(_id, position); | 968 var result = _FileUtils.setPosition(_id, position); |
| 1010 if (result is OSError) { | 969 if (result is OSError) { |
| 1011 throw new FileIOException("setPosition failed for file '$_name'", result); | 970 throw new FileIOException("setPosition failed for file '$_name'", result); |
| 1012 } | 971 } |
| 1013 } | 972 } |
| 1014 | 973 |
| 1015 Future<RandomAccessFile> truncate(int length) { | 974 Future<RandomAccessFile> truncate(int length) { |
| 1016 _ensureFileService(); | 975 _ensureFileService(); |
| 1017 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 976 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 1018 if (_isClosed) return _completeWithClosedException(completer); | 977 if (_isClosed) return _completeWithClosedException(completer); |
| 1019 List request = new List(3); | 978 List request = new List(3); |
| 1020 request[0] = _FileUtils.kTruncateRequest; | 979 request[0] = _FileUtils.kTruncateRequest; |
| 1021 request[1] = _id; | 980 request[1] = _id; |
| 1022 request[2] = length; | 981 request[2] = length; |
| 1023 _fileService.call(request).then((response) { | 982 return _fileService.call(request).transform((response) { |
| 1024 if (_isErrorResponse(response)) { | 983 if (_isErrorResponse(response)) { |
| 1025 var e = _exceptionFromResponse(response, | 984 throw _exceptionFromResponse(response, |
| 1026 "truncate failed for file '$_name'"); | 985 "truncate failed for file '$_name'"); |
| 1027 completer.completeException(e); | |
| 1028 } else { | |
| 1029 completer.complete(this); | |
| 1030 } | 986 } |
| 987 return this; |
| 1031 }); | 988 }); |
| 1032 return completer.future; | |
| 1033 } | 989 } |
| 1034 | 990 |
| 1035 void truncateSync(int length) { | 991 void truncateSync(int length) { |
| 1036 _checkNotClosed(); | 992 _checkNotClosed(); |
| 1037 var result = _FileUtils.truncate(_id, length); | 993 var result = _FileUtils.truncate(_id, length); |
| 1038 if (result is OSError) { | 994 if (result is OSError) { |
| 1039 throw new FileIOException("truncate failed for file '$_name'", result); | 995 throw new FileIOException("truncate failed for file '$_name'", result); |
| 1040 } | 996 } |
| 1041 } | 997 } |
| 1042 | 998 |
| 1043 Future<int> length() { | 999 Future<int> length() { |
| 1044 _ensureFileService(); | 1000 _ensureFileService(); |
| 1045 Completer<int> completer = new Completer<int>(); | 1001 Completer<int> completer = new Completer<int>(); |
| 1046 if (_isClosed) return _completeWithClosedException(completer); | 1002 if (_isClosed) return _completeWithClosedException(completer); |
| 1047 List request = new List(2); | 1003 List request = new List(2); |
| 1048 request[0] = _FileUtils.kLengthRequest; | 1004 request[0] = _FileUtils.kLengthRequest; |
| 1049 request[1] = _id; | 1005 request[1] = _id; |
| 1050 _fileService.call(request).then((response) { | 1006 return _fileService.call(request).transform((response) { |
| 1051 if (_isErrorResponse(response)) { | 1007 if (_isErrorResponse(response)) { |
| 1052 var e = _exceptionFromResponse(response, | 1008 throw _exceptionFromResponse(response, |
| 1053 "length failed for file '$_name'"); | 1009 "length failed for file '$_name'"); |
| 1054 completer.completeException(e); | |
| 1055 } else { | |
| 1056 completer.complete(response); | |
| 1057 } | 1010 } |
| 1011 return response; |
| 1058 }); | 1012 }); |
| 1059 return completer.future; | |
| 1060 } | 1013 } |
| 1061 | 1014 |
| 1062 int lengthSync() { | 1015 int lengthSync() { |
| 1063 _checkNotClosed(); | 1016 _checkNotClosed(); |
| 1064 var result = _FileUtils.length(_id); | 1017 var result = _FileUtils.length(_id); |
| 1065 if (result is OSError) { | 1018 if (result is OSError) { |
| 1066 throw new FileIOException("length failed for file '$_name'", result); | 1019 throw new FileIOException("length failed for file '$_name'", result); |
| 1067 } | 1020 } |
| 1068 return result; | 1021 return result; |
| 1069 } | 1022 } |
| 1070 | 1023 |
| 1071 Future<RandomAccessFile> flush() { | 1024 Future<RandomAccessFile> flush() { |
| 1072 _ensureFileService(); | 1025 _ensureFileService(); |
| 1073 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 1026 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 1074 if (_isClosed) return _completeWithClosedException(completer); | 1027 if (_isClosed) return _completeWithClosedException(completer); |
| 1075 List request = new List(2); | 1028 List request = new List(2); |
| 1076 request[0] = _FileUtils.kFlushRequest; | 1029 request[0] = _FileUtils.kFlushRequest; |
| 1077 request[1] = _id; | 1030 request[1] = _id; |
| 1078 _fileService.call(request).then((response) { | 1031 return _fileService.call(request).transform((response) { |
| 1079 if (_isErrorResponse(response)) { | 1032 if (_isErrorResponse(response)) { |
| 1080 var e = _exceptionFromResponse(response, | 1033 throw _exceptionFromResponse(response, |
| 1081 "flush failed for file '$_name'"); | 1034 "flush failed for file '$_name'"); |
| 1082 completer.completeException(e); | |
| 1083 } else { | |
| 1084 completer.complete(this); | |
| 1085 } | 1035 } |
| 1036 return this; |
| 1086 }); | 1037 }); |
| 1087 return completer.future; | |
| 1088 } | 1038 } |
| 1089 | 1039 |
| 1090 void flushSync() { | 1040 void flushSync() { |
| 1091 _checkNotClosed(); | 1041 _checkNotClosed(); |
| 1092 var result = _FileUtils.flush(_id); | 1042 var result = _FileUtils.flush(_id); |
| 1093 if (result is OSError) { | 1043 if (result is OSError) { |
| 1094 throw new FileIOException("flush failed for file '$_name'", result); | 1044 throw new FileIOException("flush failed for file '$_name'", result); |
| 1095 } | 1045 } |
| 1096 } | 1046 } |
| 1097 | 1047 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1117 new FileIOException("File closed '$_name'")); | 1067 new FileIOException("File closed '$_name'")); |
| 1118 }); | 1068 }); |
| 1119 return completer.future; | 1069 return completer.future; |
| 1120 } | 1070 } |
| 1121 | 1071 |
| 1122 final String _name; | 1072 final String _name; |
| 1123 int _id; | 1073 int _id; |
| 1124 | 1074 |
| 1125 SendPort _fileService; | 1075 SendPort _fileService; |
| 1126 } | 1076 } |
| OLD | NEW |