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 614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 } | 625 } |
626 _id = id; | 626 _id = id; |
627 _maybePerformCleanup(); | 627 _maybePerformCleanup(); |
628 } | 628 } |
629 | 629 |
630 Future<int> readByte() { | 630 Future<int> readByte() { |
631 return _dispatch(_FILE_READ_BYTE, [_id]).then((response) { | 631 return _dispatch(_FILE_READ_BYTE, [_id]).then((response) { |
632 if (_isErrorResponse(response)) { | 632 if (_isErrorResponse(response)) { |
633 throw _exceptionFromResponse(response, "readByte failed", path); | 633 throw _exceptionFromResponse(response, "readByte failed", path); |
634 } | 634 } |
635 _resourceInfo.readCount++; | 635 _resourceInfo.addRead(1); |
636 _resourceInfo.totalRead++; | |
637 return response; | 636 return response; |
638 }); | 637 }); |
639 } | 638 } |
640 | 639 |
641 external static _readByte(int id); | 640 external static _readByte(int id); |
642 | 641 |
643 int readByteSync() { | 642 int readByteSync() { |
644 _checkAvailable(); | 643 _checkAvailable(); |
645 var result = _readByte(_id); | 644 var result = _readByte(_id); |
646 if (result is OSError) { | 645 if (result is OSError) { |
647 throw new FileSystemException("readByte failed", path, result); | 646 throw new FileSystemException("readByte failed", path, result); |
648 } | 647 } |
649 _resourceInfo.readCount++; | 648 _resourceInfo.addRead(1); |
650 _resourceInfo.totalRead++; | |
651 return result; | 649 return result; |
652 } | 650 } |
653 | 651 |
654 Future<List<int>> read(int bytes) { | 652 Future<List<int>> read(int bytes) { |
655 if (bytes is !int) { | 653 if (bytes is !int) { |
656 throw new ArgumentError(bytes); | 654 throw new ArgumentError(bytes); |
657 } | 655 } |
658 return _dispatch(_FILE_READ, [_id, bytes]).then((response) { | 656 return _dispatch(_FILE_READ, [_id, bytes]).then((response) { |
659 if (_isErrorResponse(response)) { | 657 if (_isErrorResponse(response)) { |
660 throw _exceptionFromResponse(response, "read failed", path); | 658 throw _exceptionFromResponse(response, "read failed", path); |
661 } | 659 } |
662 _resourceInfo.readCount++; | 660 _resourceInfo.addRead(response[1].length); |
663 _resourceInfo.totalRead += response[1].length; | |
664 return response[1]; | 661 return response[1]; |
665 }); | 662 }); |
666 } | 663 } |
667 | 664 |
668 external static _read(int id, int bytes); | 665 external static _read(int id, int bytes); |
669 | 666 |
670 List<int> readSync(int bytes) { | 667 List<int> readSync(int bytes) { |
671 _checkAvailable(); | 668 _checkAvailable(); |
672 if (bytes is !int) { | 669 if (bytes is !int) { |
673 throw new ArgumentError(bytes); | 670 throw new ArgumentError(bytes); |
674 } | 671 } |
675 var result = _read(_id, bytes); | 672 var result = _read(_id, bytes); |
676 if (result is OSError) { | 673 if (result is OSError) { |
677 throw new FileSystemException("readSync failed", path, result); | 674 throw new FileSystemException("readSync failed", path, result); |
678 } | 675 } |
679 _resourceInfo.readCount++; | 676 _resourceInfo.addRead(result.length); |
680 _resourceInfo.totalRead += result.length; | |
681 return result; | 677 return result; |
682 } | 678 } |
683 | 679 |
684 Future<int> readInto(List<int> buffer, [int start = 0, int end]) { | 680 Future<int> readInto(List<int> buffer, [int start = 0, int end]) { |
685 if (buffer is !List || | 681 if (buffer is !List || |
686 (start != null && start is !int) || | 682 (start != null && start is !int) || |
687 (end != null && end is !int)) { | 683 (end != null && end is !int)) { |
688 throw new ArgumentError(); | 684 throw new ArgumentError(); |
689 } | 685 } |
690 end = RangeError.checkValidRange(start, end, buffer.length); | 686 end = RangeError.checkValidRange(start, end, buffer.length); |
691 if (end == start) return new Future.value(0); | 687 if (end == start) return new Future.value(0); |
692 int length = end - start; | 688 int length = end - start; |
693 return _dispatch(_FILE_READ_INTO, [_id, length]).then((response) { | 689 return _dispatch(_FILE_READ_INTO, [_id, length]).then((response) { |
694 if (_isErrorResponse(response)) { | 690 if (_isErrorResponse(response)) { |
695 throw _exceptionFromResponse(response, "readInto failed", path); | 691 throw _exceptionFromResponse(response, "readInto failed", path); |
696 } | 692 } |
697 var read = response[1]; | 693 var read = response[1]; |
698 var data = response[2]; | 694 var data = response[2]; |
699 buffer.setRange(start, start + read, data); | 695 buffer.setRange(start, start + read, data); |
700 _resourceInfo.readCount++; | 696 _resourceInfo.addRead(read); |
701 _resourceInfo.totalRead += read; | |
702 return read; | 697 return read; |
703 }); | 698 }); |
704 } | 699 } |
705 | 700 |
706 external static _readInto(int id, List<int> buffer, int start, int end); | 701 external static _readInto(int id, List<int> buffer, int start, int end); |
707 | 702 |
708 int readIntoSync(List<int> buffer, [int start = 0, int end]) { | 703 int readIntoSync(List<int> buffer, [int start = 0, int end]) { |
709 _checkAvailable(); | 704 _checkAvailable(); |
710 if (buffer is !List || | 705 if (buffer is !List || |
711 (start != null && start is !int) || | 706 (start != null && start is !int) || |
712 (end != null && end is !int)) { | 707 (end != null && end is !int)) { |
713 throw new ArgumentError(); | 708 throw new ArgumentError(); |
714 } | 709 } |
715 end = RangeError.checkValidRange(start, end, buffer.length); | 710 end = RangeError.checkValidRange(start, end, buffer.length); |
716 if (end == start) return 0; | 711 if (end == start) return 0; |
717 var result = _readInto(_id, buffer, start, end); | 712 var result = _readInto(_id, buffer, start, end); |
718 if (result is OSError) { | 713 if (result is OSError) { |
719 throw new FileSystemException("readInto failed", path, result); | 714 throw new FileSystemException("readInto failed", path, result); |
720 } | 715 } |
721 _resourceInfo.readCount++; | 716 _resourceInfo.addRead(result); |
722 _resourceInfo.totalRead += result; | |
723 return result; | 717 return result; |
724 } | 718 } |
725 | 719 |
726 Future<RandomAccessFile> writeByte(int value) { | 720 Future<RandomAccessFile> writeByte(int value) { |
727 if (value is !int) { | 721 if (value is !int) { |
728 throw new ArgumentError(value); | 722 throw new ArgumentError(value); |
729 } | 723 } |
730 return _dispatch(_FILE_WRITE_BYTE, [_id, value]).then((response) { | 724 return _dispatch(_FILE_WRITE_BYTE, [_id, value]).then((response) { |
731 if (_isErrorResponse(response)) { | 725 if (_isErrorResponse(response)) { |
732 throw _exceptionFromResponse(response, "writeByte failed", path); | 726 throw _exceptionFromResponse(response, "writeByte failed", path); |
733 } | 727 } |
734 _resourceInfo.writeCount++; | 728 _resourceInfo.addWrite(1); |
735 _resourceInfo.totalWritten++; | |
736 return this; | 729 return this; |
737 }); | 730 }); |
738 } | 731 } |
739 | 732 |
740 external static _writeByte(int id, int value); | 733 external static _writeByte(int id, int value); |
741 | 734 |
742 int writeByteSync(int value) { | 735 int writeByteSync(int value) { |
743 _checkAvailable(); | 736 _checkAvailable(); |
744 if (value is !int) { | 737 if (value is !int) { |
745 throw new ArgumentError(value); | 738 throw new ArgumentError(value); |
746 } | 739 } |
747 var result = _writeByte(_id, value); | 740 var result = _writeByte(_id, value); |
748 if (result is OSError) { | 741 if (result is OSError) { |
749 throw new FileSystemException("writeByte failed", path, result); | 742 throw new FileSystemException("writeByte failed", path, result); |
750 } | 743 } |
751 _resourceInfo.writeCount++; | 744 _resourceInfo.addWrite(1); |
752 _resourceInfo.totalWritten++; | |
753 return result; | 745 return result; |
754 } | 746 } |
755 | 747 |
756 Future<RandomAccessFile> writeFrom( | 748 Future<RandomAccessFile> writeFrom( |
757 List<int> buffer, [int start = 0, int end]) { | 749 List<int> buffer, [int start = 0, int end]) { |
758 if ((buffer is !List) || | 750 if ((buffer is !List) || |
759 (start != null && start is !int) || | 751 (start != null && start is !int) || |
760 (end != null && end is !int)) { | 752 (end != null && end is !int)) { |
761 throw new ArgumentError("Invalid arguments to writeFrom"); | 753 throw new ArgumentError("Invalid arguments to writeFrom"); |
762 } | 754 } |
763 end = RangeError.checkValidRange(start, end, buffer.length); | 755 end = RangeError.checkValidRange(start, end, buffer.length); |
764 if (end == start) return new Future.value(this); | 756 if (end == start) return new Future.value(this); |
765 _BufferAndStart result; | 757 _BufferAndStart result; |
766 try { | 758 try { |
767 result = _ensureFastAndSerializableByteData(buffer, start, end); | 759 result = _ensureFastAndSerializableByteData(buffer, start, end); |
768 } catch (e) { | 760 } catch (e) { |
769 return new Future.error(e); | 761 return new Future.error(e); |
770 } | 762 } |
771 | 763 |
772 List request = new List(4); | 764 List request = new List(4); |
773 request[0] = _id; | 765 request[0] = _id; |
774 request[1] = result.buffer; | 766 request[1] = result.buffer; |
775 request[2] = result.start; | 767 request[2] = result.start; |
776 request[3] = end - (start - result.start); | 768 request[3] = end - (start - result.start); |
777 return _dispatch(_FILE_WRITE_FROM, request).then((response) { | 769 return _dispatch(_FILE_WRITE_FROM, request).then((response) { |
778 if (_isErrorResponse(response)) { | 770 if (_isErrorResponse(response)) { |
779 throw _exceptionFromResponse(response, "writeFrom failed", path); | 771 throw _exceptionFromResponse(response, "writeFrom failed", path); |
780 } | 772 } |
781 _resourceInfo.writeCount++; | 773 _resourceInfo.addWrite(end - (start - result.start)); |
782 _resourceInfo.totalWritten += end - (start - result.start); | |
783 return this; | 774 return this; |
784 }); | 775 }); |
785 } | 776 } |
786 | 777 |
787 external static _writeFrom(int id, List<int> buffer, int start, int end); | 778 external static _writeFrom(int id, List<int> buffer, int start, int end); |
788 | 779 |
789 void writeFromSync(List<int> buffer, [int start = 0, int end]) { | 780 void writeFromSync(List<int> buffer, [int start = 0, int end]) { |
790 _checkAvailable(); | 781 _checkAvailable(); |
791 if (buffer is !List || | 782 if (buffer is !List || |
792 (start != null && start is !int) || | 783 (start != null && start is !int) || |
793 (end != null && end is !int)) { | 784 (end != null && end is !int)) { |
794 throw new ArgumentError("Invalid arguments to writeFromSync"); | 785 throw new ArgumentError("Invalid arguments to writeFromSync"); |
795 } | 786 } |
796 end = RangeError.checkValidRange(start, end, buffer.length); | 787 end = RangeError.checkValidRange(start, end, buffer.length); |
797 if (end == start) return; | 788 if (end == start) return; |
798 _BufferAndStart bufferAndStart = | 789 _BufferAndStart bufferAndStart = |
799 _ensureFastAndSerializableByteData(buffer, start, end); | 790 _ensureFastAndSerializableByteData(buffer, start, end); |
800 var result = _writeFrom(_id, | 791 var result = _writeFrom(_id, |
801 bufferAndStart.buffer, | 792 bufferAndStart.buffer, |
802 bufferAndStart.start, | 793 bufferAndStart.start, |
803 end - (start - bufferAndStart.start)); | 794 end - (start - bufferAndStart.start)); |
804 if (result is OSError) { | 795 if (result is OSError) { |
805 throw new FileSystemException("writeFrom failed", path, result); | 796 throw new FileSystemException("writeFrom failed", path, result); |
806 } | 797 } |
807 _resourceInfo.writeCount++; | 798 _resourceInfo.addWrite(end - (start - bufferAndStart.start)); |
808 _resourceInfo.totalWritten += end - (start - bufferAndStart.start); | |
809 } | 799 } |
810 | 800 |
811 Future<RandomAccessFile> writeString(String string, | 801 Future<RandomAccessFile> writeString(String string, |
812 {Encoding encoding: UTF8}) { | 802 {Encoding encoding: UTF8}) { |
813 if (encoding is! Encoding) { | 803 if (encoding is! Encoding) { |
814 throw new ArgumentError(encoding); | 804 throw new ArgumentError(encoding); |
815 } | 805 } |
816 var data = encoding.encode(string); | 806 var data = encoding.encode(string); |
817 return writeFrom(data, 0, data.length); | 807 return writeFrom(data, 0, data.length); |
818 } | 808 } |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1029 void _checkAvailable() { | 1019 void _checkAvailable() { |
1030 if (_asyncDispatched) { | 1020 if (_asyncDispatched) { |
1031 throw new FileSystemException("An async operation is currently pending", | 1021 throw new FileSystemException("An async operation is currently pending", |
1032 path); | 1022 path); |
1033 } | 1023 } |
1034 if (closed) { | 1024 if (closed) { |
1035 throw new FileSystemException("File closed", path); | 1025 throw new FileSystemException("File closed", path); |
1036 } | 1026 } |
1037 } | 1027 } |
1038 } | 1028 } |
OLD | NEW |