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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 throwIfError(ms, "Cannot retrieve modification time for file '$_path'"); | 386 throwIfError(ms, "Cannot retrieve modification time for file '$_path'"); |
387 return new DateTime.fromMillisecondsSinceEpoch(ms); | 387 return new DateTime.fromMillisecondsSinceEpoch(ms); |
388 } | 388 } |
389 | 389 |
390 external static _open(String path, int mode); | 390 external static _open(String path, int mode); |
391 | 391 |
392 RandomAccessFile openSync({FileMode mode: FileMode.READ}) { | 392 RandomAccessFile openSync({FileMode mode: FileMode.READ}) { |
393 if (mode != FileMode.READ && | 393 if (mode != FileMode.READ && |
394 mode != FileMode.WRITE && | 394 mode != FileMode.WRITE && |
395 mode != FileMode.APPEND) { | 395 mode != FileMode.APPEND) { |
396 throw new FileIOException("Unknown file mode. Use FileMode.READ, " | 396 throw new FileException("Unknown file mode. Use FileMode.READ, " |
397 "FileMode.WRITE or FileMode.APPEND."); | 397 "FileMode.WRITE or FileMode.APPEND."); |
398 } | 398 } |
399 var id = _open(_path, mode._mode); | 399 var id = _open(_path, mode._mode); |
400 throwIfError(id, "Cannot open file '$_path'"); | 400 throwIfError(id, "Cannot open file '$_path'"); |
401 return new _RandomAccessFile(id, _path); | 401 return new _RandomAccessFile(id, _path); |
402 } | 402 } |
403 | 403 |
404 external static int _openStdio(int fd); | 404 external static int _openStdio(int fd); |
405 | 405 |
406 static RandomAccessFile _openStdioSync(int fd) { | 406 static RandomAccessFile _openStdioSync(int fd) { |
407 var id = _openStdio(fd); | 407 var id = _openStdio(fd); |
408 if (id == 0) { | 408 if (id == 0) { |
409 throw new FileIOException("Cannot open stdio file for: $fd"); | 409 throw new FileException("Cannot open stdio file for: $fd"); |
410 } | 410 } |
411 return new _RandomAccessFile(id, ""); | 411 return new _RandomAccessFile(id, ""); |
412 } | 412 } |
413 | 413 |
414 Future<String> fullPath() { | 414 Future<String> fullPath() { |
415 _ensureFileService(); | 415 _ensureFileService(); |
416 List request = new List(2); | 416 List request = new List(2); |
417 request[0] = _FULL_PATH_REQUEST; | 417 request[0] = _FULL_PATH_REQUEST; |
418 request[1] = _path; | 418 request[1] = _path; |
419 return _fileService.call(request).then((response) { | 419 return _fileService.call(request).then((response) { |
(...skipping 15 matching lines...) Expand all Loading... |
435 } | 435 } |
436 | 436 |
437 Stream<List<int>> openRead([int start, int end]) { | 437 Stream<List<int>> openRead([int start, int end]) { |
438 return new _FileStream(_path, start, end); | 438 return new _FileStream(_path, start, end); |
439 } | 439 } |
440 | 440 |
441 IOSink openWrite({FileMode mode: FileMode.WRITE, | 441 IOSink openWrite({FileMode mode: FileMode.WRITE, |
442 Encoding encoding: Encoding.UTF_8}) { | 442 Encoding encoding: Encoding.UTF_8}) { |
443 if (mode != FileMode.WRITE && | 443 if (mode != FileMode.WRITE && |
444 mode != FileMode.APPEND) { | 444 mode != FileMode.APPEND) { |
445 throw new FileIOException( | 445 throw new FileException( |
446 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); | 446 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); |
447 } | 447 } |
448 var consumer = new _FileStreamConsumer(this, mode); | 448 var consumer = new _FileStreamConsumer(this, mode); |
449 return new IOSink(consumer, encoding: encoding); | 449 return new IOSink(consumer, encoding: encoding); |
450 } | 450 } |
451 | 451 |
452 Future<List<int>> readAsBytes() { | 452 Future<List<int>> readAsBytes() { |
453 _ensureFileService(); | 453 _ensureFileService(); |
454 Completer<List<int>> completer = new Completer<List<int>>(); | 454 Completer<List<int>> completer = new Completer<List<int>>(); |
455 var chunks = new _BufferList(); | 455 var chunks = new _BufferList(); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 String toString() => "File: '$path'"; | 552 String toString() => "File: '$path'"; |
553 | 553 |
554 void _ensureFileService() { | 554 void _ensureFileService() { |
555 if (_fileService == null) { | 555 if (_fileService == null) { |
556 _fileService = _FileUtils._newServicePort(); | 556 _fileService = _FileUtils._newServicePort(); |
557 } | 557 } |
558 } | 558 } |
559 | 559 |
560 static throwIfError(Object result, String msg) { | 560 static throwIfError(Object result, String msg) { |
561 if (result is OSError) { | 561 if (result is OSError) { |
562 throw new FileIOException(msg, result); | 562 throw new FileException(msg, result); |
563 } | 563 } |
564 } | 564 } |
565 | 565 |
566 final String _path; | 566 final String _path; |
567 | 567 |
568 SendPort _fileService; | 568 SendPort _fileService; |
569 } | 569 } |
570 | 570 |
571 | 571 |
572 class _RandomAccessFile implements RandomAccessFile { | 572 class _RandomAccessFile implements RandomAccessFile { |
573 _RandomAccessFile(int this._id, String this._path); | 573 _RandomAccessFile(int this._id, String this._path); |
574 | 574 |
575 Future<RandomAccessFile> close() { | 575 Future<RandomAccessFile> close() { |
576 if (closed) return _closedException(); | 576 if (closed) return _closedException(); |
577 _ensureFileService(); | 577 _ensureFileService(); |
578 List request = new List(2); | 578 List request = new List(2); |
579 request[0] = _CLOSE_REQUEST; | 579 request[0] = _CLOSE_REQUEST; |
580 request[1] = _id; | 580 request[1] = _id; |
581 // Set the id_ to 0 (NULL) to ensure the no more async requests | 581 // Set the id_ to 0 (NULL) to ensure the no more async requests |
582 // can be issued for this file. | 582 // can be issued for this file. |
583 _id = 0; | 583 _id = 0; |
584 return _fileService.call(request).then((result) { | 584 return _fileService.call(request).then((result) { |
585 if (result != -1) { | 585 if (result != -1) { |
586 _id = result; | 586 _id = result; |
587 return this; | 587 return this; |
588 } else { | 588 } else { |
589 throw new FileIOException("Cannot close file '$_path'"); | 589 throw new FileException("Cannot close file '$_path'"); |
590 } | 590 } |
591 }); | 591 }); |
592 } | 592 } |
593 | 593 |
594 external static int _close(int id); | 594 external static int _close(int id); |
595 | 595 |
596 void closeSync() { | 596 void closeSync() { |
597 _checkNotClosed(); | 597 _checkNotClosed(); |
598 var id = _close(_id); | 598 var id = _close(_id); |
599 if (id == -1) { | 599 if (id == -1) { |
600 throw new FileIOException("Cannot close file '$_path'"); | 600 throw new FileException("Cannot close file '$_path'"); |
601 } | 601 } |
602 _id = id; | 602 _id = id; |
603 } | 603 } |
604 | 604 |
605 Future<int> readByte() { | 605 Future<int> readByte() { |
606 _ensureFileService(); | 606 _ensureFileService(); |
607 if (closed) return _closedException(); | 607 if (closed) return _closedException(); |
608 List request = new List(2); | 608 List request = new List(2); |
609 request[0] = _READ_BYTE_REQUEST; | 609 request[0] = _READ_BYTE_REQUEST; |
610 request[1] = _id; | 610 request[1] = _id; |
611 return _fileService.call(request).then((response) { | 611 return _fileService.call(request).then((response) { |
612 if (_isErrorResponse(response)) { | 612 if (_isErrorResponse(response)) { |
613 throw _exceptionFromResponse(response, | 613 throw _exceptionFromResponse(response, |
614 "readByte failed for file '$_path'"); | 614 "readByte failed for file '$_path'"); |
615 } | 615 } |
616 return response; | 616 return response; |
617 }); | 617 }); |
618 } | 618 } |
619 | 619 |
620 external static _readByte(int id); | 620 external static _readByte(int id); |
621 | 621 |
622 int readByteSync() { | 622 int readByteSync() { |
623 _checkNotClosed(); | 623 _checkNotClosed(); |
624 var result = _readByte(_id); | 624 var result = _readByte(_id); |
625 if (result is OSError) { | 625 if (result is OSError) { |
626 throw new FileIOException("readByte failed for file '$_path'", result); | 626 throw new FileException("readByte failed for file '$_path'", result); |
627 } | 627 } |
628 return result; | 628 return result; |
629 } | 629 } |
630 | 630 |
631 Future<List<int>> read(int bytes) { | 631 Future<List<int>> read(int bytes) { |
632 _ensureFileService(); | 632 _ensureFileService(); |
633 if (bytes is !int) { | 633 if (bytes is !int) { |
634 return new Future.error(new FileIOException( | 634 return new Future.error(new FileException( |
635 "Invalid arguments to read for file '$_path'")); | 635 "Invalid arguments to read for file '$_path'")); |
636 } | 636 } |
637 if (closed) return _closedException(); | 637 if (closed) return _closedException(); |
638 List request = new List(3); | 638 List request = new List(3); |
639 request[0] = _READ_REQUEST; | 639 request[0] = _READ_REQUEST; |
640 request[1] = _id; | 640 request[1] = _id; |
641 request[2] = bytes; | 641 request[2] = bytes; |
642 return _fileService.call(request).then((response) { | 642 return _fileService.call(request).then((response) { |
643 if (_isErrorResponse(response)) { | 643 if (_isErrorResponse(response)) { |
644 throw _exceptionFromResponse(response, | 644 throw _exceptionFromResponse(response, |
645 "read failed for file '$_path'"); | 645 "read failed for file '$_path'"); |
646 } | 646 } |
647 return response[1]; | 647 return response[1]; |
648 }); | 648 }); |
649 } | 649 } |
650 | 650 |
651 external static _read(int id, int bytes); | 651 external static _read(int id, int bytes); |
652 | 652 |
653 List<int> readSync(int bytes) { | 653 List<int> readSync(int bytes) { |
654 _checkNotClosed(); | 654 _checkNotClosed(); |
655 if (bytes is !int) { | 655 if (bytes is !int) { |
656 throw new FileIOException( | 656 throw new FileException( |
657 "Invalid arguments to readSync for file '$_path'"); | 657 "Invalid arguments to readSync for file '$_path'"); |
658 } | 658 } |
659 var result = _read(_id, bytes); | 659 var result = _read(_id, bytes); |
660 if (result is OSError) { | 660 if (result is OSError) { |
661 throw new FileIOException("readSync failed for file '$_path'", | 661 throw new FileException("readSync failed for file '$_path'", |
662 result); | 662 result); |
663 } | 663 } |
664 return result; | 664 return result; |
665 } | 665 } |
666 | 666 |
667 Future<int> readInto(List<int> buffer, [int start, int end]) { | 667 Future<int> readInto(List<int> buffer, [int start, int end]) { |
668 _ensureFileService(); | 668 _ensureFileService(); |
669 if (buffer is !List || | 669 if (buffer is !List || |
670 (start != null && start is !int) || | 670 (start != null && start is !int) || |
671 (end != null && end is !int)) { | 671 (end != null && end is !int)) { |
672 return new Future.error(new FileIOException( | 672 return new Future.error(new FileException( |
673 "Invalid arguments to readInto for file '$_path'")); | 673 "Invalid arguments to readInto for file '$_path'")); |
674 }; | 674 }; |
675 if (closed) return _closedException(); | 675 if (closed) return _closedException(); |
676 List request = new List(3); | 676 List request = new List(3); |
677 if (start == null) start = 0; | 677 if (start == null) start = 0; |
678 if (end == null) end = buffer.length; | 678 if (end == null) end = buffer.length; |
679 request[0] = _READ_LIST_REQUEST; | 679 request[0] = _READ_LIST_REQUEST; |
680 request[1] = _id; | 680 request[1] = _id; |
681 request[2] = end - start; | 681 request[2] = end - start; |
682 return _fileService.call(request).then((response) { | 682 return _fileService.call(request).then((response) { |
(...skipping 16 matching lines...) Expand all Loading... |
699 } | 699 } |
700 } | 700 } |
701 | 701 |
702 external static _readInto(int id, List<int> buffer, int start, int end); | 702 external static _readInto(int id, List<int> buffer, int start, int end); |
703 | 703 |
704 int readIntoSync(List<int> buffer, [int start, int end]) { | 704 int readIntoSync(List<int> buffer, [int start, int end]) { |
705 _checkNotClosed(); | 705 _checkNotClosed(); |
706 if (buffer is !List || | 706 if (buffer is !List || |
707 (start != null && start is !int) || | 707 (start != null && start is !int) || |
708 (end != null && end is !int)) { | 708 (end != null && end is !int)) { |
709 throw new FileIOException( | 709 throw new FileException( |
710 "Invalid arguments to readInto for file '$_path'"); | 710 "Invalid arguments to readInto for file '$_path'"); |
711 } | 711 } |
712 if (start == null) start = 0; | 712 if (start == null) start = 0; |
713 if (end == null) end = buffer.length; | 713 if (end == null) end = buffer.length; |
714 if (end == start) return 0; | 714 if (end == start) return 0; |
715 _checkReadWriteListArguments(buffer.length, start, end); | 715 _checkReadWriteListArguments(buffer.length, start, end); |
716 var result = _readInto(_id, buffer, start, end); | 716 var result = _readInto(_id, buffer, start, end); |
717 if (result is OSError) { | 717 if (result is OSError) { |
718 throw new FileIOException("readInto failed for file '$_path'", | 718 throw new FileException("readInto failed for file '$_path'", |
719 result); | 719 result); |
720 } | 720 } |
721 return result; | 721 return result; |
722 } | 722 } |
723 | 723 |
724 Future<RandomAccessFile> writeByte(int value) { | 724 Future<RandomAccessFile> writeByte(int value) { |
725 _ensureFileService(); | 725 _ensureFileService(); |
726 if (value is !int) { | 726 if (value is !int) { |
727 return new Future.error(new FileIOException( | 727 return new Future.error(new FileException( |
728 "Invalid argument to writeByte for file '$_path'")); | 728 "Invalid argument to writeByte for file '$_path'")); |
729 } | 729 } |
730 if (closed) return _closedException(); | 730 if (closed) return _closedException(); |
731 List request = new List(3); | 731 List request = new List(3); |
732 request[0] = _WRITE_BYTE_REQUEST; | 732 request[0] = _WRITE_BYTE_REQUEST; |
733 request[1] = _id; | 733 request[1] = _id; |
734 request[2] = value; | 734 request[2] = value; |
735 return _fileService.call(request).then((response) { | 735 return _fileService.call(request).then((response) { |
736 if (_isErrorResponse(response)) { | 736 if (_isErrorResponse(response)) { |
737 throw _exceptionFromResponse(response, | 737 throw _exceptionFromResponse(response, |
738 "writeByte failed for file '$_path'"); | 738 "writeByte failed for file '$_path'"); |
739 } | 739 } |
740 return this; | 740 return this; |
741 }); | 741 }); |
742 } | 742 } |
743 | 743 |
744 external static _writeByte(int id, int value); | 744 external static _writeByte(int id, int value); |
745 | 745 |
746 int writeByteSync(int value) { | 746 int writeByteSync(int value) { |
747 _checkNotClosed(); | 747 _checkNotClosed(); |
748 if (value is !int) { | 748 if (value is !int) { |
749 throw new FileIOException( | 749 throw new FileException( |
750 "Invalid argument to writeByte for file '$_path'"); | 750 "Invalid argument to writeByte for file '$_path'"); |
751 } | 751 } |
752 var result = _writeByte(_id, value); | 752 var result = _writeByte(_id, value); |
753 if (result is OSError) { | 753 if (result is OSError) { |
754 throw new FileIOException("writeByte failed for file '$_path'", | 754 throw new FileException("writeByte failed for file '$_path'", |
755 result); | 755 result); |
756 } | 756 } |
757 return result; | 757 return result; |
758 } | 758 } |
759 | 759 |
760 Future<RandomAccessFile> writeFrom(List<int> buffer, [int start, int end]) { | 760 Future<RandomAccessFile> writeFrom(List<int> buffer, [int start, int end]) { |
761 _ensureFileService(); | 761 _ensureFileService(); |
762 if ((buffer is !List && buffer is !ByteData) || | 762 if ((buffer is !List && buffer is !ByteData) || |
763 (start != null && start is !int) || | 763 (start != null && start is !int) || |
764 (end != null && end is !int)) { | 764 (end != null && end is !int)) { |
765 return new Future.error(new FileIOException( | 765 return new Future.error(new FileException( |
766 "Invalid arguments to writeFrom for file '$_path'")); | 766 "Invalid arguments to writeFrom for file '$_path'")); |
767 } | 767 } |
768 | 768 |
769 if (closed) return _closedException(); | 769 if (closed) return _closedException(); |
770 | 770 |
771 _BufferAndStart result; | 771 _BufferAndStart result; |
772 try { | 772 try { |
773 result = _ensureFastAndSerializableBuffer(buffer, start, end); | 773 result = _ensureFastAndSerializableBuffer(buffer, start, end); |
774 } catch (e) { | 774 } catch (e) { |
775 return new Future.error(e); | 775 return new Future.error(e); |
(...skipping 14 matching lines...) Expand all Loading... |
790 }); | 790 }); |
791 } | 791 } |
792 | 792 |
793 external static _writeFrom(int id, List<int> buffer, int start, int end); | 793 external static _writeFrom(int id, List<int> buffer, int start, int end); |
794 | 794 |
795 void writeFromSync(List<int> buffer, [int start, int end]) { | 795 void writeFromSync(List<int> buffer, [int start, int end]) { |
796 _checkNotClosed(); | 796 _checkNotClosed(); |
797 if (buffer is !List || | 797 if (buffer is !List || |
798 (start != null && start is !int) || | 798 (start != null && start is !int) || |
799 (end != null && end is !int)) { | 799 (end != null && end is !int)) { |
800 throw new FileIOException( | 800 throw new FileException( |
801 "Invalid arguments to writeFrom for file '$_path'"); | 801 "Invalid arguments to writeFrom for file '$_path'"); |
802 } | 802 } |
803 if (start == null) start = 0; | 803 if (start == null) start = 0; |
804 if (end == null) end = buffer.length; | 804 if (end == null) end = buffer.length; |
805 if (end == start) return; | 805 if (end == start) return; |
806 _checkReadWriteListArguments(buffer.length, start, end); | 806 _checkReadWriteListArguments(buffer.length, start, end); |
807 _BufferAndStart bufferAndStart = | 807 _BufferAndStart bufferAndStart = |
808 _ensureFastAndSerializableBuffer(buffer, start, end); | 808 _ensureFastAndSerializableBuffer(buffer, start, end); |
809 var result = _writeFrom(_id, | 809 var result = _writeFrom(_id, |
810 bufferAndStart.buffer, | 810 bufferAndStart.buffer, |
811 bufferAndStart.start, | 811 bufferAndStart.start, |
812 end - (start - bufferAndStart.start)); | 812 end - (start - bufferAndStart.start)); |
813 if (result is OSError) { | 813 if (result is OSError) { |
814 throw new FileIOException("writeFrom failed for file '$_path'", result); | 814 throw new FileException("writeFrom failed for file '$_path'", result); |
815 } | 815 } |
816 } | 816 } |
817 | 817 |
818 Future<RandomAccessFile> writeString(String string, | 818 Future<RandomAccessFile> writeString(String string, |
819 {Encoding encoding: Encoding.UTF_8}) { | 819 {Encoding encoding: Encoding.UTF_8}) { |
820 if (encoding is! Encoding) { | 820 if (encoding is! Encoding) { |
821 return new Future.error(new FileIOException( | 821 return new Future.error(new FileException( |
822 "Invalid encoding in writeString: $encoding")); | 822 "Invalid encoding in writeString: $encoding")); |
823 } | 823 } |
824 var data = _encodeString(string, encoding); | 824 var data = _encodeString(string, encoding); |
825 return writeFrom(data, 0, data.length); | 825 return writeFrom(data, 0, data.length); |
826 } | 826 } |
827 | 827 |
828 void writeStringSync(String string, {Encoding encoding: Encoding.UTF_8}) { | 828 void writeStringSync(String string, {Encoding encoding: Encoding.UTF_8}) { |
829 if (encoding is! Encoding) { | 829 if (encoding is! Encoding) { |
830 throw new FileIOException( | 830 throw new FileException( |
831 "Invalid encoding in writeStringSync: $encoding"); | 831 "Invalid encoding in writeStringSync: $encoding"); |
832 } | 832 } |
833 var data = _encodeString(string, encoding); | 833 var data = _encodeString(string, encoding); |
834 writeFromSync(data, 0, data.length); | 834 writeFromSync(data, 0, data.length); |
835 } | 835 } |
836 | 836 |
837 Future<int> position() { | 837 Future<int> position() { |
838 _ensureFileService(); | 838 _ensureFileService(); |
839 if (closed) return _closedException(); | 839 if (closed) return _closedException(); |
840 List request = new List(2); | 840 List request = new List(2); |
841 request[0] = _POSITION_REQUEST; | 841 request[0] = _POSITION_REQUEST; |
842 request[1] = _id; | 842 request[1] = _id; |
843 return _fileService.call(request).then((response) { | 843 return _fileService.call(request).then((response) { |
844 if (_isErrorResponse(response)) { | 844 if (_isErrorResponse(response)) { |
845 throw _exceptionFromResponse(response, | 845 throw _exceptionFromResponse(response, |
846 "position failed for file '$_path'"); | 846 "position failed for file '$_path'"); |
847 } | 847 } |
848 return response; | 848 return response; |
849 }); | 849 }); |
850 } | 850 } |
851 | 851 |
852 external static _position(int id); | 852 external static _position(int id); |
853 | 853 |
854 int positionSync() { | 854 int positionSync() { |
855 _checkNotClosed(); | 855 _checkNotClosed(); |
856 var result = _position(_id); | 856 var result = _position(_id); |
857 if (result is OSError) { | 857 if (result is OSError) { |
858 throw new FileIOException("position failed for file '$_path'", result); | 858 throw new FileException("position failed for file '$_path'", result); |
859 } | 859 } |
860 return result; | 860 return result; |
861 } | 861 } |
862 | 862 |
863 Future<RandomAccessFile> setPosition(int position) { | 863 Future<RandomAccessFile> setPosition(int position) { |
864 _ensureFileService(); | 864 _ensureFileService(); |
865 if (closed) return _closedException(); | 865 if (closed) return _closedException(); |
866 List request = new List(3); | 866 List request = new List(3); |
867 request[0] = _SET_POSITION_REQUEST; | 867 request[0] = _SET_POSITION_REQUEST; |
868 request[1] = _id; | 868 request[1] = _id; |
869 request[2] = position; | 869 request[2] = position; |
870 return _fileService.call(request).then((response) { | 870 return _fileService.call(request).then((response) { |
871 if (_isErrorResponse(response)) { | 871 if (_isErrorResponse(response)) { |
872 throw _exceptionFromResponse(response, | 872 throw _exceptionFromResponse(response, |
873 "setPosition failed for file '$_path'"); | 873 "setPosition failed for file '$_path'"); |
874 } | 874 } |
875 return this; | 875 return this; |
876 }); | 876 }); |
877 } | 877 } |
878 | 878 |
879 external static _setPosition(int id, int position); | 879 external static _setPosition(int id, int position); |
880 | 880 |
881 void setPositionSync(int position) { | 881 void setPositionSync(int position) { |
882 _checkNotClosed(); | 882 _checkNotClosed(); |
883 var result = _setPosition(_id, position); | 883 var result = _setPosition(_id, position); |
884 if (result is OSError) { | 884 if (result is OSError) { |
885 throw new FileIOException("setPosition failed for file '$_path'", result); | 885 throw new FileException("setPosition failed for file '$_path'", result); |
886 } | 886 } |
887 } | 887 } |
888 | 888 |
889 Future<RandomAccessFile> truncate(int length) { | 889 Future<RandomAccessFile> truncate(int length) { |
890 _ensureFileService(); | 890 _ensureFileService(); |
891 if (closed) return _closedException(); | 891 if (closed) return _closedException(); |
892 List request = new List(3); | 892 List request = new List(3); |
893 request[0] = _TRUNCATE_REQUEST; | 893 request[0] = _TRUNCATE_REQUEST; |
894 request[1] = _id; | 894 request[1] = _id; |
895 request[2] = length; | 895 request[2] = length; |
896 return _fileService.call(request).then((response) { | 896 return _fileService.call(request).then((response) { |
897 if (_isErrorResponse(response)) { | 897 if (_isErrorResponse(response)) { |
898 throw _exceptionFromResponse(response, | 898 throw _exceptionFromResponse(response, |
899 "truncate failed for file '$_path'"); | 899 "truncate failed for file '$_path'"); |
900 } | 900 } |
901 return this; | 901 return this; |
902 }); | 902 }); |
903 } | 903 } |
904 | 904 |
905 external static _truncate(int id, int length); | 905 external static _truncate(int id, int length); |
906 | 906 |
907 void truncateSync(int length) { | 907 void truncateSync(int length) { |
908 _checkNotClosed(); | 908 _checkNotClosed(); |
909 var result = _truncate(_id, length); | 909 var result = _truncate(_id, length); |
910 if (result is OSError) { | 910 if (result is OSError) { |
911 throw new FileIOException("truncate failed for file '$_path'", result); | 911 throw new FileException("truncate failed for file '$_path'", result); |
912 } | 912 } |
913 } | 913 } |
914 | 914 |
915 Future<int> length() { | 915 Future<int> length() { |
916 _ensureFileService(); | 916 _ensureFileService(); |
917 if (closed) return _closedException(); | 917 if (closed) return _closedException(); |
918 List request = new List(2); | 918 List request = new List(2); |
919 request[0] = _LENGTH_REQUEST; | 919 request[0] = _LENGTH_REQUEST; |
920 request[1] = _id; | 920 request[1] = _id; |
921 return _fileService.call(request).then((response) { | 921 return _fileService.call(request).then((response) { |
922 if (_isErrorResponse(response)) { | 922 if (_isErrorResponse(response)) { |
923 throw _exceptionFromResponse(response, | 923 throw _exceptionFromResponse(response, |
924 "length failed for file '$_path'"); | 924 "length failed for file '$_path'"); |
925 } | 925 } |
926 return response; | 926 return response; |
927 }); | 927 }); |
928 } | 928 } |
929 | 929 |
930 external static _length(int id); | 930 external static _length(int id); |
931 | 931 |
932 int lengthSync() { | 932 int lengthSync() { |
933 _checkNotClosed(); | 933 _checkNotClosed(); |
934 var result = _length(_id); | 934 var result = _length(_id); |
935 if (result is OSError) { | 935 if (result is OSError) { |
936 throw new FileIOException("length failed for file '$_path'", result); | 936 throw new FileException("length failed for file '$_path'", result); |
937 } | 937 } |
938 return result; | 938 return result; |
939 } | 939 } |
940 | 940 |
941 Future<RandomAccessFile> flush() { | 941 Future<RandomAccessFile> flush() { |
942 _ensureFileService(); | 942 _ensureFileService(); |
943 if (closed) return _closedException(); | 943 if (closed) return _closedException(); |
944 List request = new List(2); | 944 List request = new List(2); |
945 request[0] = _FLUSH_REQUEST; | 945 request[0] = _FLUSH_REQUEST; |
946 request[1] = _id; | 946 request[1] = _id; |
947 return _fileService.call(request).then((response) { | 947 return _fileService.call(request).then((response) { |
948 if (_isErrorResponse(response)) { | 948 if (_isErrorResponse(response)) { |
949 throw _exceptionFromResponse(response, | 949 throw _exceptionFromResponse(response, |
950 "flush failed for file '$_path'"); | 950 "flush failed for file '$_path'"); |
951 } | 951 } |
952 return this; | 952 return this; |
953 }); | 953 }); |
954 } | 954 } |
955 | 955 |
956 external static _flush(int id); | 956 external static _flush(int id); |
957 | 957 |
958 void flushSync() { | 958 void flushSync() { |
959 _checkNotClosed(); | 959 _checkNotClosed(); |
960 var result = _flush(_id); | 960 var result = _flush(_id); |
961 if (result is OSError) { | 961 if (result is OSError) { |
962 throw new FileIOException("flush failed for file '$_path'", result); | 962 throw new FileException("flush failed for file '$_path'", result); |
963 } | 963 } |
964 } | 964 } |
965 | 965 |
966 String get path => _path; | 966 String get path => _path; |
967 | 967 |
968 void _ensureFileService() { | 968 void _ensureFileService() { |
969 if (_fileService == null) { | 969 if (_fileService == null) { |
970 _fileService = _FileUtils._newServicePort(); | 970 _fileService = _FileUtils._newServicePort(); |
971 } | 971 } |
972 } | 972 } |
973 | 973 |
974 bool get closed => _id == 0; | 974 bool get closed => _id == 0; |
975 | 975 |
976 void _checkNotClosed() { | 976 void _checkNotClosed() { |
977 if (closed) { | 977 if (closed) { |
978 throw new FileIOException("File closed '$_path'"); | 978 throw new FileException("File closed '$_path'"); |
979 } | 979 } |
980 } | 980 } |
981 | 981 |
982 Future _closedException() { | 982 Future _closedException() { |
983 return new Future.error(new FileIOException("File closed '$_path'")); | 983 return new Future.error(new FileException("File closed '$_path'")); |
984 } | 984 } |
985 | 985 |
986 final String _path; | 986 final String _path; |
987 int _id; | 987 int _id; |
988 | 988 |
989 SendPort _fileService; | 989 SendPort _fileService; |
990 } | 990 } |
OLD | NEW |