Chromium Code Reviews| 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 632 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 643 var read = opened.readListSync(result, 0, length); | 643 var read = opened.readListSync(result, 0, length); |
| 644 if (read != length) { | 644 if (read != length) { |
| 645 throw new FileIOException("Failed to read file"); | 645 throw new FileIOException("Failed to read file"); |
| 646 } | 646 } |
| 647 opened.closeSync(); | 647 opened.closeSync(); |
| 648 return result; | 648 return result; |
| 649 } | 649 } |
| 650 | 650 |
| 651 Future<String> readAsText([Encoding encoding = Encoding.UTF_8]) { | 651 Future<String> readAsText([Encoding encoding = Encoding.UTF_8]) { |
| 652 _ensureFileService(); | 652 _ensureFileService(); |
| 653 var decoder = _StringDecoders.decoder(encoding); | |
| 654 return readAsBytes().transform((bytes) { | 653 return readAsBytes().transform((bytes) { |
| 654 var decoder = _StringDecoders.decoder(encoding); | |
| 655 decoder.write(bytes); | 655 decoder.write(bytes); |
| 656 return decoder.decoded; | 656 return decoder.decoded; |
| 657 }); | 657 }); |
| 658 } | 658 } |
| 659 | 659 |
| 660 String readAsTextSync([Encoding encoding = Encoding.UTF_8]) { | 660 String readAsTextSync([Encoding encoding = Encoding.UTF_8]) { |
| 661 var decoder = _StringDecoders.decoder(encoding); | 661 var decoder = _StringDecoders.decoder(encoding); |
| 662 List<int> bytes = readAsBytesSync(); | 662 List<int> bytes = readAsBytesSync(); |
| 663 decoder.write(bytes); | 663 decoder.write(bytes); |
| 664 return decoder.decoded; | 664 return decoder.decoded; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 676 var data = decoder.decoded; | 676 var data = decoder.decoded; |
| 677 if (data != null) { | 677 if (data != null) { |
| 678 result.add(data); | 678 result.add(data); |
| 679 } | 679 } |
| 680 return result; | 680 return result; |
| 681 } | 681 } |
| 682 | 682 |
| 683 Future<List<String>> readAsLines([Encoding encoding = Encoding.UTF_8]) { | 683 Future<List<String>> readAsLines([Encoding encoding = Encoding.UTF_8]) { |
| 684 _ensureFileService(); | 684 _ensureFileService(); |
| 685 Completer<List<String>> completer = new Completer<List<String>>(); | 685 Completer<List<String>> completer = new Completer<List<String>>(); |
| 686 var decoder = _StringDecoders.decoder(encoding); | |
| 687 return readAsBytes().transform((bytes) { | 686 return readAsBytes().transform((bytes) { |
| 687 var decoder = _StringDecoders.decoder(encoding); | |
| 688 decoder.write(bytes); | 688 decoder.write(bytes); |
| 689 return _getDecodedLines(decoder); | 689 return _getDecodedLines(decoder); |
| 690 }); | 690 }); |
| 691 } | 691 } |
| 692 | 692 |
| 693 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) { | 693 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) { |
| 694 var decoder = _StringDecoders.decoder(encoding); | 694 var decoder = _StringDecoders.decoder(encoding); |
| 695 List<int> bytes = readAsBytesSync(); | 695 List<int> bytes = readAsBytesSync(); |
| 696 decoder.write(bytes); | 696 decoder.write(bytes); |
| 697 return _getDecodedLines(decoder); | 697 return _getDecodedLines(decoder); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 709 | 709 |
| 710 SendPort _fileService; | 710 SendPort _fileService; |
| 711 } | 711 } |
| 712 | 712 |
| 713 | 713 |
| 714 class _RandomAccessFile extends _FileBase implements RandomAccessFile { | 714 class _RandomAccessFile extends _FileBase implements RandomAccessFile { |
| 715 _RandomAccessFile(int this._id, String this._name); | 715 _RandomAccessFile(int this._id, String this._name); |
| 716 | 716 |
| 717 Future<RandomAccessFile> close() { | 717 Future<RandomAccessFile> close() { |
| 718 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 718 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 719 if (_id == 0) { | 719 if (_isClosed) { |
| 720 // Complete asynchronously so the user has a chance to setup | 720 // Complete asynchronously so the user has a chance to setup |
| 721 // handlers without getting exceptions when registering the | 721 // handlers without getting exceptions when registering the |
| 722 // then handler. | 722 // then handler. |
| 723 new Timer(0, (t) { | 723 new Timer(0, (t) { |
| 724 completer.completeException( | 724 completer.completeException( |
| 725 new FileIOException("Cannot close file '$_name'")); | 725 new FileIOException("Cannot close file '$_name'")); |
| 726 }); | 726 }); |
| 727 return completer.future; | 727 return completer.future; |
| 728 } | 728 } |
| 729 _ensureFileService(); | 729 _ensureFileService(); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 749 var id = _FileUtils.close(_id); | 749 var id = _FileUtils.close(_id); |
| 750 if (id == -1) { | 750 if (id == -1) { |
| 751 throw new FileIOException("Cannot close file '$_name'"); | 751 throw new FileIOException("Cannot close file '$_name'"); |
| 752 } | 752 } |
| 753 _id = id; | 753 _id = id; |
| 754 } | 754 } |
| 755 | 755 |
| 756 Future<int> readByte() { | 756 Future<int> readByte() { |
| 757 _ensureFileService(); | 757 _ensureFileService(); |
| 758 Completer<int> completer = new Completer<int>(); | 758 Completer<int> completer = new Completer<int>(); |
| 759 if (_isClosed) { | |
|
Søren Gjesse
2012/05/16 11:54:11
Add a function for closed exception as 4 lines of
Mads Ager (google)
2012/05/16 12:45:19
Done. Each of these replaced by:
if (_isClosed) r
| |
| 760 new Timer(0, (t) { | |
| 761 completer.completeException( | |
| 762 new FileIOException("File closed '$_name'")); | |
| 763 }); | |
| 764 return completer.future; | |
| 765 } | |
| 759 List request = new List(2); | 766 List request = new List(2); |
| 760 request[0] = _FileUtils.kReadByteRequest; | 767 request[0] = _FileUtils.kReadByteRequest; |
| 761 request[1] = _id; | 768 request[1] = _id; |
| 762 _fileService.call(request).then((response) { | 769 _fileService.call(request).then((response) { |
| 763 if (_isErrorResponse(response)) { | 770 if (_isErrorResponse(response)) { |
| 764 var e = _exceptionFromResponse(response, | 771 var e = _exceptionFromResponse(response, |
| 765 "readByte failed for file '$_name'"); | 772 "readByte failed for file '$_name'"); |
| 766 completer.completeException(e); | 773 completer.completeException(e); |
| 767 } else { | 774 } else { |
| 768 completer.complete(response); | 775 completer.complete(response); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 786 if (buffer is !List || offset is !int || bytes is !int) { | 793 if (buffer is !List || offset is !int || bytes is !int) { |
| 787 // Complete asynchronously so the user has a chance to setup | 794 // Complete asynchronously so the user has a chance to setup |
| 788 // handlers without getting exceptions when registering the | 795 // handlers without getting exceptions when registering the |
| 789 // then handler. | 796 // then handler. |
| 790 new Timer(0, (t) { | 797 new Timer(0, (t) { |
| 791 completer.completeException(new FileIOException( | 798 completer.completeException(new FileIOException( |
| 792 "Invalid arguments to readList for file '$_name'")); | 799 "Invalid arguments to readList for file '$_name'")); |
| 793 }); | 800 }); |
| 794 return completer.future; | 801 return completer.future; |
| 795 }; | 802 }; |
| 803 if (_isClosed) { | |
| 804 new Timer(0, (t) { | |
| 805 completer.completeException( | |
| 806 new FileIOException("File closed '$_name'")); | |
| 807 }); | |
| 808 return completer.future; | |
| 809 } | |
| 796 List request = new List(3); | 810 List request = new List(3); |
| 797 request[0] = _FileUtils.kReadListRequest; | 811 request[0] = _FileUtils.kReadListRequest; |
| 798 request[1] = _id; | 812 request[1] = _id; |
| 799 request[2] = bytes; | 813 request[2] = bytes; |
| 800 _fileService.call(request).then((response) { | 814 _fileService.call(request).then((response) { |
| 801 if (_isErrorResponse(response)) { | 815 if (_isErrorResponse(response)) { |
| 802 var e = _exceptionFromResponse(response, | 816 var e = _exceptionFromResponse(response, |
| 803 "readList failed for file '$_name'"); | 817 "readList failed for file '$_name'"); |
| 804 completer.completeException(e); | 818 completer.completeException(e); |
| 805 } else { | 819 } else { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 838 if (value is !int) { | 852 if (value is !int) { |
| 839 // Complete asynchronously so the user has a chance to setup | 853 // Complete asynchronously so the user has a chance to setup |
| 840 // handlers without getting exceptions when registering the | 854 // handlers without getting exceptions when registering the |
| 841 // then handler. | 855 // then handler. |
| 842 new Timer(0, (t) { | 856 new Timer(0, (t) { |
| 843 completer.completeException(new FileIOException( | 857 completer.completeException(new FileIOException( |
| 844 "Invalid argument to writeByte for file '$_name'")); | 858 "Invalid argument to writeByte for file '$_name'")); |
| 845 }); | 859 }); |
| 846 return completer.future; | 860 return completer.future; |
| 847 } | 861 } |
| 862 if (_isClosed) { | |
| 863 new Timer(0, (t) { | |
| 864 completer.completeException( | |
| 865 new FileIOException("File closed '$_name'")); | |
| 866 }); | |
| 867 return completer.future; | |
| 868 } | |
| 848 List request = new List(3); | 869 List request = new List(3); |
| 849 request[0] = _FileUtils.kWriteByteRequest; | 870 request[0] = _FileUtils.kWriteByteRequest; |
| 850 request[1] = _id; | 871 request[1] = _id; |
| 851 request[2] = value; | 872 request[2] = value; |
| 852 _fileService.call(request).then((response) { | 873 _fileService.call(request).then((response) { |
| 853 if (_isErrorResponse(response)) { | 874 if (_isErrorResponse(response)) { |
| 854 var e = _exceptionFromResponse(response, | 875 var e = _exceptionFromResponse(response, |
| 855 "writeByte failed for file '$_name'"); | 876 "writeByte failed for file '$_name'"); |
| 856 completer.completeException(e); | 877 completer.completeException(e); |
| 857 } else { | 878 } else { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 881 if (buffer is !List || offset is !int || bytes is !int) { | 902 if (buffer is !List || offset is !int || bytes is !int) { |
| 882 // Complete asynchronously so the user has a chance to setup | 903 // Complete asynchronously so the user has a chance to setup |
| 883 // handlers without getting exceptions when registering the | 904 // handlers without getting exceptions when registering the |
| 884 // then handler. | 905 // then handler. |
| 885 new Timer(0, (t) { | 906 new Timer(0, (t) { |
| 886 completer.completeException(new FileIOException( | 907 completer.completeException(new FileIOException( |
| 887 "Invalid arguments to writeList for file '$_name'")); | 908 "Invalid arguments to writeList for file '$_name'")); |
| 888 }); | 909 }); |
| 889 return completer.future; | 910 return completer.future; |
| 890 } | 911 } |
| 912 if (_isClosed) { | |
| 913 new Timer(0, (t) { | |
| 914 completer.completeException( | |
| 915 new FileIOException("File closed '$_name'")); | |
| 916 }); | |
| 917 return completer.future; | |
| 918 } | |
| 891 | 919 |
| 892 List result = | 920 List result = |
| 893 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); | 921 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); |
| 894 List outBuffer = result[0]; | 922 List outBuffer = result[0]; |
| 895 int outOffset = result[1]; | 923 int outOffset = result[1]; |
| 896 | 924 |
| 897 List request = new List(5); | 925 List request = new List(5); |
| 898 request[0] = _FileUtils.kWriteListRequest; | 926 request[0] = _FileUtils.kWriteListRequest; |
| 899 request[1] = _id; | 927 request[1] = _id; |
| 900 request[2] = outBuffer; | 928 request[2] = outBuffer; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 928 if (result is OSError) { | 956 if (result is OSError) { |
| 929 throw new FileIOException("writeList failed for file '$_name'", result); | 957 throw new FileIOException("writeList failed for file '$_name'", result); |
| 930 } | 958 } |
| 931 return result; | 959 return result; |
| 932 } | 960 } |
| 933 | 961 |
| 934 Future<RandomAccessFile> writeString(String string, | 962 Future<RandomAccessFile> writeString(String string, |
| 935 [Encoding encoding = Encoding.UTF_8]) { | 963 [Encoding encoding = Encoding.UTF_8]) { |
| 936 _ensureFileService(); | 964 _ensureFileService(); |
| 937 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 965 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 966 if (_isClosed) { | |
| 967 new Timer(0, (t) { | |
| 968 completer.completeException( | |
| 969 new FileIOException("File closed '$_name'")); | |
| 970 }); | |
| 971 return completer.future; | |
| 972 } | |
| 938 List request = new List(3); | 973 List request = new List(3); |
| 939 request[0] = _FileUtils.kWriteStringRequest; | 974 request[0] = _FileUtils.kWriteStringRequest; |
| 940 request[1] = _id; | 975 request[1] = _id; |
| 941 request[2] = string; | 976 request[2] = string; |
| 942 _fileService.call(request).then((response) { | 977 _fileService.call(request).then((response) { |
| 943 if (_isErrorResponse(response)) { | 978 if (_isErrorResponse(response)) { |
| 944 var e = _exceptionFromResponse(response, | 979 var e = _exceptionFromResponse(response, |
| 945 "writeString failed for file '$_name'"); | 980 "writeString failed for file '$_name'"); |
| 946 completer.completeException(e); | 981 completer.completeException(e); |
| 947 } else { | 982 } else { |
| 948 completer.complete(this); | 983 completer.complete(this); |
| 949 } | 984 } |
| 950 }); | 985 }); |
| 951 return completer.future; | 986 return completer.future; |
| 952 } | 987 } |
| 953 | 988 |
| 954 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { | 989 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { |
| 955 _checkNotClosed(); | 990 _checkNotClosed(); |
| 956 var result = _FileUtils.checkedWriteString(_id, string); | 991 var result = _FileUtils.checkedWriteString(_id, string); |
| 957 if (result is OSError) { | 992 if (result is OSError) { |
| 958 throw new FileIOException("writeString failed for file '$_name'", result); | 993 throw new FileIOException("writeString failed for file '$_name'", result); |
| 959 } | 994 } |
| 960 return result; | 995 return result; |
| 961 } | 996 } |
| 962 | 997 |
| 963 Future<int> position() { | 998 Future<int> position() { |
| 964 _ensureFileService(); | 999 _ensureFileService(); |
| 965 Completer<int> completer = new Completer<int>(); | 1000 Completer<int> completer = new Completer<int>(); |
| 1001 if (_isClosed) { | |
| 1002 new Timer(0, (t) { | |
| 1003 completer.completeException( | |
| 1004 new FileIOException("File closed '$_name'")); | |
| 1005 }); | |
| 1006 return completer.future; | |
| 1007 } | |
| 966 List request = new List(2); | 1008 List request = new List(2); |
| 967 request[0] = _FileUtils.kPositionRequest; | 1009 request[0] = _FileUtils.kPositionRequest; |
| 968 request[1] = _id; | 1010 request[1] = _id; |
| 969 _fileService.call(request).then((response) { | 1011 _fileService.call(request).then((response) { |
| 970 if (_isErrorResponse(response)) { | 1012 if (_isErrorResponse(response)) { |
| 971 var e = _exceptionFromResponse(response, | 1013 var e = _exceptionFromResponse(response, |
| 972 "position failed for file '$_name'"); | 1014 "position failed for file '$_name'"); |
| 973 completer.completeException(e); | 1015 completer.completeException(e); |
| 974 } else { | 1016 } else { |
| 975 completer.complete(response); | 1017 completer.complete(response); |
| 976 } | 1018 } |
| 977 }); | 1019 }); |
| 978 return completer.future; | 1020 return completer.future; |
| 979 } | 1021 } |
| 980 | 1022 |
| 981 int positionSync() { | 1023 int positionSync() { |
| 982 _checkNotClosed(); | 1024 _checkNotClosed(); |
| 983 var result = _FileUtils.position(_id); | 1025 var result = _FileUtils.position(_id); |
| 984 if (result is OSError) { | 1026 if (result is OSError) { |
| 985 throw new FileIOException("position failed for file '$_name'", result); | 1027 throw new FileIOException("position failed for file '$_name'", result); |
| 986 } | 1028 } |
| 987 return result; | 1029 return result; |
| 988 } | 1030 } |
| 989 | 1031 |
| 990 Future<RandomAccessFile> setPosition(int position) { | 1032 Future<RandomAccessFile> setPosition(int position) { |
| 991 _ensureFileService(); | 1033 _ensureFileService(); |
| 992 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 1034 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 1035 if (_isClosed) { | |
| 1036 new Timer(0, (t) { | |
| 1037 completer.completeException( | |
| 1038 new FileIOException("File closed '$_name'")); | |
| 1039 }); | |
| 1040 return completer.future; | |
| 1041 } | |
| 993 List request = new List(3); | 1042 List request = new List(3); |
| 994 request[0] = _FileUtils.kSetPositionRequest; | 1043 request[0] = _FileUtils.kSetPositionRequest; |
| 995 request[1] = _id; | 1044 request[1] = _id; |
| 996 request[2] = position; | 1045 request[2] = position; |
| 997 _fileService.call(request).then((response) { | 1046 _fileService.call(request).then((response) { |
| 998 if (_isErrorResponse(response)) { | 1047 if (_isErrorResponse(response)) { |
| 999 var e = _exceptionFromResponse(response, | 1048 var e = _exceptionFromResponse(response, |
| 1000 "setPosition failed for file '$_name'"); | 1049 "setPosition failed for file '$_name'"); |
| 1001 completer.completeException(e); | 1050 completer.completeException(e); |
| 1002 } else { | 1051 } else { |
| 1003 completer.complete(this); | 1052 completer.complete(this); |
| 1004 } | 1053 } |
| 1005 }); | 1054 }); |
| 1006 return completer.future; | 1055 return completer.future; |
| 1007 } | 1056 } |
| 1008 | 1057 |
| 1009 void setPositionSync(int position) { | 1058 void setPositionSync(int position) { |
| 1010 _checkNotClosed(); | 1059 _checkNotClosed(); |
| 1011 var result = _FileUtils.setPosition(_id, position); | 1060 var result = _FileUtils.setPosition(_id, position); |
| 1012 if (result is OSError) { | 1061 if (result is OSError) { |
| 1013 throw new FileIOException("setPosition failed for file '$_name'", result); | 1062 throw new FileIOException("setPosition failed for file '$_name'", result); |
| 1014 } | 1063 } |
| 1015 } | 1064 } |
| 1016 | 1065 |
| 1017 Future<RandomAccessFile> truncate(int length) { | 1066 Future<RandomAccessFile> truncate(int length) { |
| 1018 _ensureFileService(); | 1067 _ensureFileService(); |
| 1019 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 1068 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 1069 if (_isClosed) { | |
| 1070 new Timer(0, (t) { | |
| 1071 completer.completeException( | |
| 1072 new FileIOException("File closed '$_name'")); | |
| 1073 }); | |
| 1074 return completer.future; | |
| 1075 } | |
| 1020 List request = new List(3); | 1076 List request = new List(3); |
| 1021 request[0] = _FileUtils.kTruncateRequest; | 1077 request[0] = _FileUtils.kTruncateRequest; |
| 1022 request[1] = _id; | 1078 request[1] = _id; |
| 1023 request[2] = length; | 1079 request[2] = length; |
| 1024 _fileService.call(request).then((response) { | 1080 _fileService.call(request).then((response) { |
| 1025 if (_isErrorResponse(response)) { | 1081 if (_isErrorResponse(response)) { |
| 1026 var e = _exceptionFromResponse(response, | 1082 var e = _exceptionFromResponse(response, |
| 1027 "truncate failed for file '$_name'"); | 1083 "truncate failed for file '$_name'"); |
| 1028 completer.completeException(e); | 1084 completer.completeException(e); |
| 1029 } else { | 1085 } else { |
| 1030 completer.complete(this); | 1086 completer.complete(this); |
| 1031 } | 1087 } |
| 1032 }); | 1088 }); |
| 1033 return completer.future; | 1089 return completer.future; |
| 1034 } | 1090 } |
| 1035 | 1091 |
| 1036 void truncateSync(int length) { | 1092 void truncateSync(int length) { |
| 1037 _checkNotClosed(); | 1093 _checkNotClosed(); |
| 1038 var result = _FileUtils.truncate(_id, length); | 1094 var result = _FileUtils.truncate(_id, length); |
| 1039 if (result is OSError) { | 1095 if (result is OSError) { |
| 1040 throw new FileIOException("truncate failed for file '$_name'", result); | 1096 throw new FileIOException("truncate failed for file '$_name'", result); |
| 1041 } | 1097 } |
| 1042 } | 1098 } |
| 1043 | 1099 |
| 1044 Future<int> length() { | 1100 Future<int> length() { |
| 1045 _ensureFileService(); | 1101 _ensureFileService(); |
| 1046 Completer<int> completer = new Completer<int>(); | 1102 Completer<int> completer = new Completer<int>(); |
| 1103 if (_isClosed) { | |
| 1104 new Timer(0, (t) { | |
| 1105 completer.completeException( | |
| 1106 new FileIOException("File closed '$_name'")); | |
| 1107 }); | |
| 1108 return completer.future; | |
| 1109 } | |
| 1047 List request = new List(2); | 1110 List request = new List(2); |
| 1048 request[0] = _FileUtils.kLengthRequest; | 1111 request[0] = _FileUtils.kLengthRequest; |
| 1049 request[1] = _id; | 1112 request[1] = _id; |
| 1050 _fileService.call(request).then((response) { | 1113 _fileService.call(request).then((response) { |
| 1051 if (_isErrorResponse(response)) { | 1114 if (_isErrorResponse(response)) { |
| 1052 var e = _exceptionFromResponse(response, | 1115 var e = _exceptionFromResponse(response, |
| 1053 "length failed for file '$_name'"); | 1116 "length failed for file '$_name'"); |
| 1054 completer.completeException(e); | 1117 completer.completeException(e); |
| 1055 } else { | 1118 } else { |
| 1056 completer.complete(response); | 1119 completer.complete(response); |
| 1057 } | 1120 } |
| 1058 }); | 1121 }); |
| 1059 return completer.future; | 1122 return completer.future; |
| 1060 } | 1123 } |
| 1061 | 1124 |
| 1062 int lengthSync() { | 1125 int lengthSync() { |
| 1063 _checkNotClosed(); | 1126 _checkNotClosed(); |
| 1064 var result = _FileUtils.length(_id); | 1127 var result = _FileUtils.length(_id); |
| 1065 if (result is OSError) { | 1128 if (result is OSError) { |
| 1066 throw new FileIOException("length failed for file '$_name'", result); | 1129 throw new FileIOException("length failed for file '$_name'", result); |
| 1067 } | 1130 } |
| 1068 return result; | 1131 return result; |
| 1069 } | 1132 } |
| 1070 | 1133 |
| 1071 Future<RandomAccessFile> flush() { | 1134 Future<RandomAccessFile> flush() { |
| 1072 _ensureFileService(); | 1135 _ensureFileService(); |
| 1073 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 1136 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 1137 if (_isClosed) { | |
| 1138 new Timer(0, (t) { | |
| 1139 completer.completeException( | |
| 1140 new FileIOException("File closed '$_name'")); | |
| 1141 }); | |
| 1142 return completer.future; | |
| 1143 } | |
| 1074 List request = new List(2); | 1144 List request = new List(2); |
| 1075 request[0] = _FileUtils.kFlushRequest; | 1145 request[0] = _FileUtils.kFlushRequest; |
| 1076 request[1] = _id; | 1146 request[1] = _id; |
| 1077 _fileService.call(request).then((response) { | 1147 _fileService.call(request).then((response) { |
| 1078 if (_isErrorResponse(response)) { | 1148 if (_isErrorResponse(response)) { |
| 1079 var e = _exceptionFromResponse(response, | 1149 var e = _exceptionFromResponse(response, |
| 1080 "flush failed for file '$_name'"); | 1150 "flush failed for file '$_name'"); |
| 1081 completer.completeException(e); | 1151 completer.completeException(e); |
| 1082 } else { | 1152 } else { |
| 1083 completer.complete(this); | 1153 completer.complete(this); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 1095 } | 1165 } |
| 1096 | 1166 |
| 1097 String get name() => _name; | 1167 String get name() => _name; |
| 1098 | 1168 |
| 1099 void _ensureFileService() { | 1169 void _ensureFileService() { |
| 1100 if (_fileService == null) { | 1170 if (_fileService == null) { |
| 1101 _fileService = _FileUtils.newServicePort(); | 1171 _fileService = _FileUtils.newServicePort(); |
| 1102 } | 1172 } |
| 1103 } | 1173 } |
| 1104 | 1174 |
| 1175 bool get _isClosed() => _id == 0; | |
| 1176 | |
| 1105 void _checkNotClosed() { | 1177 void _checkNotClosed() { |
| 1106 if (_id == 0) { | 1178 if (_isClosed) { |
| 1107 throw new FileIOException("File closed '$_name'"); | 1179 throw new FileIOException("File closed '$_name'"); |
| 1108 } | 1180 } |
| 1109 } | 1181 } |
| 1110 | 1182 |
| 1111 final String _name; | 1183 final String _name; |
| 1112 int _id; | 1184 int _id; |
| 1113 | 1185 |
| 1114 SendPort _fileService; | 1186 SendPort _fileService; |
| 1115 } | 1187 } |
| OLD | NEW |