| 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 : _data = [], | |
| 8 _position = 0, | |
| 9 _filePosition = 0 { | |
| 10 var file = new File(name); | 7 var file = new File(name); |
| 11 var future = file.open(FileMode.READ); | 8 _data = []; |
| 12 future.handleException((e) { | 9 _position = 0; |
| 13 _reportError(e); | 10 var chained = file.open(FileMode.READ).chain((openedFile) { |
| 14 return true; | 11 return _readDataFromFile(openedFile); |
| 15 }); | |
| 16 future.then(_setupOpenedFile); | |
| 17 } | |
| 18 | |
| 19 _FileInputStream.fromStdio(int fd) | |
| 20 : _data = [], | |
| 21 _position = 0, | |
| 22 _filePosition = 0 { | |
| 23 assert(fd == 0); | |
| 24 _setupOpenedFile(_File._openStdioSync(fd)); | |
| 25 } | |
| 26 | |
| 27 void _setupOpenedFile(RandomAccessFile openedFile) { | |
| 28 _openedFile = openedFile; | |
| 29 var chained = _openedFile.length().chain((len) { | |
| 30 _fileLength = len; | |
| 31 return _fillBuffer(); | |
| 32 }); | 12 }); |
| 33 chained.handleException((e) { | 13 chained.handleException((e) { |
| 34 _reportError(e); | 14 _reportError(e); |
| 35 return true; | 15 return true; |
| 36 }); | 16 }); |
| 37 chained.then((ignored) => _checkScheduleCallbacks()); | |
| 38 } | 17 } |
| 39 | 18 |
| 40 void _closeFile() { | 19 _FileInputStream.fromStdio(int fd) { |
| 41 if (available() == 0) _cancelScheduledDataCallback(); | 20 assert(fd == 0); |
| 42 if (!_openedFile.closed) { | 21 var file = _File._openStdioSync(fd); |
| 43 _openedFile.close().then((ignore) { | 22 _data = []; |
| 44 _streamMarkedClosed = true; | 23 _position = 0; |
| 45 _checkScheduleCallbacks(); | 24 _readDataFromFile(file).handleException((e) { |
| 46 }); | 25 _reportError(e); |
| 47 } | 26 return true; |
| 27 }); |
| 48 } | 28 } |
| 49 | 29 |
| 50 Future<int> _fillBuffer() { | 30 Future<RandomAccessFile> _closeAfterRead(RandomAccessFile openedFile) { |
| 51 Expect.equals(_position, _data.length); | 31 return openedFile.close().transform((ignore) { |
| 32 _streamMarkedClosed = true; |
| 33 _checkScheduleCallbacks(); |
| 34 return openedFile; |
| 35 }); |
| 36 } |
| 52 | 37 |
| 53 int size = Math.min(_bufferLength, _fileLength - _filePosition); | 38 Future<RandomAccessFile> _readDataFromFile(RandomAccessFile openedFile) { |
| 54 if (size == 0) { | 39 return openedFile.length().chain((length) { |
| 55 _closeFile(); | 40 var contents = new Uint8List(length); |
| 56 return new Future.immediate(0); | 41 if (length != 0) { |
| 57 } | 42 return openedFile.readList(contents, 0, length).chain((read) { |
| 58 if (_data.length != size) { | 43 if (read != length) { |
| 59 _data = new Uint8List(size); | 44 throw new FileIOException( |
| 60 } | 45 'Failed reading file contents in FileInputStream'); |
| 61 var future = _openedFile.readList(_data, 0, _data.length); | 46 } else { |
| 62 future = future.transform((read) { | 47 _data = contents; |
| 63 _filePosition += read; | 48 } |
| 64 if (read != _data.length) { | 49 return _closeAfterRead(openedFile); |
| 65 _data.removeRange(read, _data.length - read); | 50 }); |
| 51 } else { |
| 52 return _closeAfterRead(openedFile); |
| 66 } | 53 } |
| 67 _position = 0; | |
| 68 | |
| 69 if (_fileLength == _filePosition) { | |
| 70 _closeFile(); | |
| 71 } | |
| 72 return read; | |
| 73 }); | 54 }); |
| 74 return future; | |
| 75 } | 55 } |
| 76 | 56 |
| 77 int available() { | 57 int available() { |
| 78 return closed ? 0 : _data.length - _position; | 58 return _closed ? 0 : _data.length - _position; |
| 79 } | 59 } |
| 80 | 60 |
| 81 void pipe(OutputStream output, [bool close = true]) { | 61 void pipe(OutputStream output, [bool close = true]) { |
| 82 _pipe(this, output, close: close); | 62 _pipe(this, output, close: close); |
| 83 } | 63 } |
| 84 | 64 |
| 85 void _finishRead() { | |
| 86 if (_position == _data.length && !_streamMarkedClosed) { | |
| 87 _fillBuffer().then((ignored) { | |
| 88 _checkScheduleCallbacks(); | |
| 89 }); | |
| 90 } else { | |
| 91 _checkScheduleCallbacks(); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 List<int> _read(int bytesToRead) { | 65 List<int> _read(int bytesToRead) { |
| 96 List<int> result; | 66 List<int> result = new Uint8List(bytesToRead); |
| 97 if (_position == 0 && bytesToRead == _data.length) { | 67 result.setRange(0, bytesToRead, _data, _position); |
| 98 result = _data; | 68 _position += bytesToRead; |
| 99 _data = []; | 69 _checkScheduleCallbacks(); |
| 100 } else { | |
| 101 result = new Uint8List(bytesToRead); | |
| 102 result.setRange(0, bytesToRead, _data, _position); | |
| 103 _position += bytesToRead; | |
| 104 } | |
| 105 _finishRead(); | |
| 106 return result; | 70 return result; |
| 107 } | 71 } |
| 108 | 72 |
| 109 int _readInto(List<int> buffer, int offset, int len) { | 73 int _readInto(List<int> buffer, int offset, int len) { |
| 110 buffer.setRange(offset, len, _data, _position); | 74 buffer.setRange(offset, len, _data, _position); |
| 111 _position += len; | 75 _position += len; |
| 112 _finishRead(); | 76 _checkScheduleCallbacks(); |
| 113 return len; | 77 return len; |
| 114 } | 78 } |
| 115 | 79 |
| 116 void _close() { | 80 void _close() { |
| 117 _data = []; | 81 if (_closed) return; |
| 118 _position = 0; | 82 _closed = true; |
| 119 _filePosition = _fileLength; | |
| 120 _closeFile(); | |
| 121 } | 83 } |
| 122 | 84 |
| 123 static final int _bufferLength = 64 * 1024; | |
| 124 | |
| 125 RandomAccessFile _openedFile; | |
| 126 List<int> _data; | 85 List<int> _data; |
| 127 int _position; | 86 int _position; |
| 128 int _filePosition; | 87 bool _closed = false; |
| 129 int _fileLength; | |
| 130 } | 88 } |
| 131 | 89 |
| 132 | 90 |
| 133 class _PendingOperation { | 91 class _PendingOperation { |
| 134 const _PendingOperation(this._id); | 92 const _PendingOperation(this._id); |
| 135 static final _PendingOperation CLOSE = const _PendingOperation(0); | 93 static final _PendingOperation CLOSE = const _PendingOperation(0); |
| 136 static final _PendingOperation FLUSH = const _PendingOperation(1); | 94 static final _PendingOperation FLUSH = const _PendingOperation(1); |
| 137 final int _id; | 95 final int _id; |
| 138 } | 96 } |
| 139 | 97 |
| (...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 747 | 705 |
| 748 SendPort _fileService; | 706 SendPort _fileService; |
| 749 } | 707 } |
| 750 | 708 |
| 751 | 709 |
| 752 class _RandomAccessFile extends _FileBase implements RandomAccessFile { | 710 class _RandomAccessFile extends _FileBase implements RandomAccessFile { |
| 753 _RandomAccessFile(int this._id, String this._name); | 711 _RandomAccessFile(int this._id, String this._name); |
| 754 | 712 |
| 755 Future<RandomAccessFile> close() { | 713 Future<RandomAccessFile> close() { |
| 756 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 714 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 757 if (closed) return _completeWithClosedException(completer); | 715 if (_isClosed) return _completeWithClosedException(completer); |
| 758 _ensureFileService(); | 716 _ensureFileService(); |
| 759 List request = new List(2); | 717 List request = new List(2); |
| 760 request[0] = _FileUtils.CLOSE_REQUEST; | 718 request[0] = _FileUtils.CLOSE_REQUEST; |
| 761 request[1] = _id; | 719 request[1] = _id; |
| 762 // Set the id_ to 0 (NULL) to ensure the no more async requests | 720 // Set the id_ to 0 (NULL) to ensure the no more async requests |
| 763 // can be issued for this file. | 721 // can be issues for this file. |
| 764 _id = 0; | 722 _id = 0; |
| 765 return _fileService.call(request).transform((result) { | 723 return _fileService.call(request).transform((result) { |
| 766 if (result != -1) { | 724 if (result != -1) { |
| 767 _id = result; | 725 _id = result; |
| 768 return this; | 726 return this; |
| 769 } else { | 727 } else { |
| 770 throw new FileIOException("Cannot close file '$_name'"); | 728 throw new FileIOException("Cannot close file '$_name'"); |
| 771 } | 729 } |
| 772 }); | 730 }); |
| 773 } | 731 } |
| 774 | 732 |
| 775 void closeSync() { | 733 void closeSync() { |
| 776 var id = _FileUtils.close(_id); | 734 var id = _FileUtils.close(_id); |
| 777 if (id == -1) { | 735 if (id == -1) { |
| 778 throw new FileIOException("Cannot close file '$_name'"); | 736 throw new FileIOException("Cannot close file '$_name'"); |
| 779 } | 737 } |
| 780 _id = id; | 738 _id = id; |
| 781 } | 739 } |
| 782 | 740 |
| 783 Future<int> readByte() { | 741 Future<int> readByte() { |
| 784 _ensureFileService(); | 742 _ensureFileService(); |
| 785 Completer<int> completer = new Completer<int>(); | 743 Completer<int> completer = new Completer<int>(); |
| 786 if (closed) return _completeWithClosedException(completer); | 744 if (_isClosed) return _completeWithClosedException(completer); |
| 787 List request = new List(2); | 745 List request = new List(2); |
| 788 request[0] = _FileUtils.READ_BYTE_REQUEST; | 746 request[0] = _FileUtils.READ_BYTE_REQUEST; |
| 789 request[1] = _id; | 747 request[1] = _id; |
| 790 return _fileService.call(request).transform((response) { | 748 return _fileService.call(request).transform((response) { |
| 791 if (_isErrorResponse(response)) { | 749 if (_isErrorResponse(response)) { |
| 792 throw _exceptionFromResponse(response, | 750 throw _exceptionFromResponse(response, |
| 793 "readByte failed for file '$_name'"); | 751 "readByte failed for file '$_name'"); |
| 794 } | 752 } |
| 795 return response; | 753 return response; |
| 796 }); | 754 }); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 811 if (buffer is !List || offset is !int || bytes is !int) { | 769 if (buffer is !List || offset is !int || bytes is !int) { |
| 812 // Complete asynchronously so the user has a chance to setup | 770 // Complete asynchronously so the user has a chance to setup |
| 813 // handlers without getting exceptions when registering the | 771 // handlers without getting exceptions when registering the |
| 814 // then handler. | 772 // then handler. |
| 815 new Timer(0, (t) { | 773 new Timer(0, (t) { |
| 816 completer.completeException(new FileIOException( | 774 completer.completeException(new FileIOException( |
| 817 "Invalid arguments to readList for file '$_name'")); | 775 "Invalid arguments to readList for file '$_name'")); |
| 818 }); | 776 }); |
| 819 return completer.future; | 777 return completer.future; |
| 820 }; | 778 }; |
| 821 if (closed) return _completeWithClosedException(completer); | 779 if (_isClosed) return _completeWithClosedException(completer); |
| 822 List request = new List(3); | 780 List request = new List(3); |
| 823 request[0] = _FileUtils.READ_LIST_REQUEST; | 781 request[0] = _FileUtils.READ_LIST_REQUEST; |
| 824 request[1] = _id; | 782 request[1] = _id; |
| 825 request[2] = bytes; | 783 request[2] = bytes; |
| 826 return _fileService.call(request).transform((response) { | 784 return _fileService.call(request).transform((response) { |
| 827 if (_isErrorResponse(response)) { | 785 if (_isErrorResponse(response)) { |
| 828 throw _exceptionFromResponse(response, | 786 throw _exceptionFromResponse(response, |
| 829 "readList failed for file '$_name'"); | 787 "readList failed for file '$_name'"); |
| 830 } | 788 } |
| 831 var read = response[1]; | 789 var read = response[1]; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 861 if (value is !int) { | 819 if (value is !int) { |
| 862 // Complete asynchronously so the user has a chance to setup | 820 // Complete asynchronously so the user has a chance to setup |
| 863 // handlers without getting exceptions when registering the | 821 // handlers without getting exceptions when registering the |
| 864 // then handler. | 822 // then handler. |
| 865 new Timer(0, (t) { | 823 new Timer(0, (t) { |
| 866 completer.completeException(new FileIOException( | 824 completer.completeException(new FileIOException( |
| 867 "Invalid argument to writeByte for file '$_name'")); | 825 "Invalid argument to writeByte for file '$_name'")); |
| 868 }); | 826 }); |
| 869 return completer.future; | 827 return completer.future; |
| 870 } | 828 } |
| 871 if (closed) return _completeWithClosedException(completer); | 829 if (_isClosed) return _completeWithClosedException(completer); |
| 872 List request = new List(3); | 830 List request = new List(3); |
| 873 request[0] = _FileUtils.WRITE_BYTE_REQUEST; | 831 request[0] = _FileUtils.WRITE_BYTE_REQUEST; |
| 874 request[1] = _id; | 832 request[1] = _id; |
| 875 request[2] = value; | 833 request[2] = value; |
| 876 return _fileService.call(request).transform((response) { | 834 return _fileService.call(request).transform((response) { |
| 877 if (_isErrorResponse(response)) { | 835 if (_isErrorResponse(response)) { |
| 878 throw _exceptionFromResponse(response, | 836 throw _exceptionFromResponse(response, |
| 879 "writeByte failed for file '$_name'"); | 837 "writeByte failed for file '$_name'"); |
| 880 } | 838 } |
| 881 return this; | 839 return this; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 902 if (buffer is !List || offset is !int || bytes is !int) { | 860 if (buffer is !List || offset is !int || bytes is !int) { |
| 903 // Complete asynchronously so the user has a chance to setup | 861 // Complete asynchronously so the user has a chance to setup |
| 904 // handlers without getting exceptions when registering the | 862 // handlers without getting exceptions when registering the |
| 905 // then handler. | 863 // then handler. |
| 906 new Timer(0, (t) { | 864 new Timer(0, (t) { |
| 907 completer.completeException(new FileIOException( | 865 completer.completeException(new FileIOException( |
| 908 "Invalid arguments to writeList for file '$_name'")); | 866 "Invalid arguments to writeList for file '$_name'")); |
| 909 }); | 867 }); |
| 910 return completer.future; | 868 return completer.future; |
| 911 } | 869 } |
| 912 if (closed) return _completeWithClosedException(completer); | 870 if (_isClosed) return _completeWithClosedException(completer); |
| 913 | 871 |
| 914 List result; | 872 List result; |
| 915 try { | 873 try { |
| 916 result = | 874 result = |
| 917 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); | 875 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); |
| 918 } catch (var e) { | 876 } catch (var e) { |
| 919 // Complete asynchronously so the user has a chance to setup | 877 // Complete asynchronously so the user has a chance to setup |
| 920 // handlers without getting exceptions when registering the | 878 // handlers without getting exceptions when registering the |
| 921 // then handler. | 879 // then handler. |
| 922 new Timer(0, (t) => completer.completeException(e)); | 880 new Timer(0, (t) => completer.completeException(e)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 if (result is OSError) { | 914 if (result is OSError) { |
| 957 throw new FileIOException("writeList failed for file '$_name'", result); | 915 throw new FileIOException("writeList failed for file '$_name'", result); |
| 958 } | 916 } |
| 959 return result; | 917 return result; |
| 960 } | 918 } |
| 961 | 919 |
| 962 Future<RandomAccessFile> writeString(String string, | 920 Future<RandomAccessFile> writeString(String string, |
| 963 [Encoding encoding = Encoding.UTF_8]) { | 921 [Encoding encoding = Encoding.UTF_8]) { |
| 964 _ensureFileService(); | 922 _ensureFileService(); |
| 965 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 923 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 966 if (closed) return _completeWithClosedException(completer); | 924 if (_isClosed) return _completeWithClosedException(completer); |
| 967 List request = new List(3); | 925 List request = new List(3); |
| 968 request[0] = _FileUtils.WRITE_STRING_REQUEST; | 926 request[0] = _FileUtils.WRITE_STRING_REQUEST; |
| 969 request[1] = _id; | 927 request[1] = _id; |
| 970 request[2] = string; | 928 request[2] = string; |
| 971 return _fileService.call(request).transform((response) { | 929 return _fileService.call(request).transform((response) { |
| 972 if (_isErrorResponse(response)) { | 930 if (_isErrorResponse(response)) { |
| 973 throw _exceptionFromResponse(response, | 931 throw _exceptionFromResponse(response, |
| 974 "writeString failed for file '$_name'"); | 932 "writeString failed for file '$_name'"); |
| 975 } | 933 } |
| 976 return this; | 934 return this; |
| 977 }); | 935 }); |
| 978 } | 936 } |
| 979 | 937 |
| 980 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { | 938 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { |
| 981 _checkNotClosed(); | 939 _checkNotClosed(); |
| 982 var result = _FileUtils.checkedWriteString(_id, string); | 940 var result = _FileUtils.checkedWriteString(_id, string); |
| 983 if (result is OSError) { | 941 if (result is OSError) { |
| 984 throw new FileIOException("writeString failed for file '$_name'"); | 942 throw new FileIOException("writeString failed for file '$_name'"); |
| 985 } | 943 } |
| 986 return result; | 944 return result; |
| 987 } | 945 } |
| 988 | 946 |
| 989 Future<int> position() { | 947 Future<int> position() { |
| 990 _ensureFileService(); | 948 _ensureFileService(); |
| 991 Completer<int> completer = new Completer<int>(); | 949 Completer<int> completer = new Completer<int>(); |
| 992 if (closed) return _completeWithClosedException(completer); | 950 if (_isClosed) return _completeWithClosedException(completer); |
| 993 List request = new List(2); | 951 List request = new List(2); |
| 994 request[0] = _FileUtils.POSITION_REQUEST; | 952 request[0] = _FileUtils.POSITION_REQUEST; |
| 995 request[1] = _id; | 953 request[1] = _id; |
| 996 return _fileService.call(request).transform((response) { | 954 return _fileService.call(request).transform((response) { |
| 997 if (_isErrorResponse(response)) { | 955 if (_isErrorResponse(response)) { |
| 998 throw _exceptionFromResponse(response, | 956 throw _exceptionFromResponse(response, |
| 999 "position failed for file '$_name'"); | 957 "position failed for file '$_name'"); |
| 1000 } | 958 } |
| 1001 return response; | 959 return response; |
| 1002 }); | 960 }); |
| 1003 } | 961 } |
| 1004 | 962 |
| 1005 int positionSync() { | 963 int positionSync() { |
| 1006 _checkNotClosed(); | 964 _checkNotClosed(); |
| 1007 var result = _FileUtils.position(_id); | 965 var result = _FileUtils.position(_id); |
| 1008 if (result is OSError) { | 966 if (result is OSError) { |
| 1009 throw new FileIOException("position failed for file '$_name'", result); | 967 throw new FileIOException("position failed for file '$_name'", result); |
| 1010 } | 968 } |
| 1011 return result; | 969 return result; |
| 1012 } | 970 } |
| 1013 | 971 |
| 1014 Future<RandomAccessFile> setPosition(int position) { | 972 Future<RandomAccessFile> setPosition(int position) { |
| 1015 _ensureFileService(); | 973 _ensureFileService(); |
| 1016 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 974 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 1017 if (closed) return _completeWithClosedException(completer); | 975 if (_isClosed) return _completeWithClosedException(completer); |
| 1018 List request = new List(3); | 976 List request = new List(3); |
| 1019 request[0] = _FileUtils.SET_POSITION_REQUEST; | 977 request[0] = _FileUtils.SET_POSITION_REQUEST; |
| 1020 request[1] = _id; | 978 request[1] = _id; |
| 1021 request[2] = position; | 979 request[2] = position; |
| 1022 return _fileService.call(request).transform((response) { | 980 return _fileService.call(request).transform((response) { |
| 1023 if (_isErrorResponse(response)) { | 981 if (_isErrorResponse(response)) { |
| 1024 throw _exceptionFromResponse(response, | 982 throw _exceptionFromResponse(response, |
| 1025 "setPosition failed for file '$_name'"); | 983 "setPosition failed for file '$_name'"); |
| 1026 } | 984 } |
| 1027 return this; | 985 return this; |
| 1028 }); | 986 }); |
| 1029 } | 987 } |
| 1030 | 988 |
| 1031 void setPositionSync(int position) { | 989 void setPositionSync(int position) { |
| 1032 _checkNotClosed(); | 990 _checkNotClosed(); |
| 1033 var result = _FileUtils.setPosition(_id, position); | 991 var result = _FileUtils.setPosition(_id, position); |
| 1034 if (result is OSError) { | 992 if (result is OSError) { |
| 1035 throw new FileIOException("setPosition failed for file '$_name'", result); | 993 throw new FileIOException("setPosition failed for file '$_name'", result); |
| 1036 } | 994 } |
| 1037 } | 995 } |
| 1038 | 996 |
| 1039 Future<RandomAccessFile> truncate(int length) { | 997 Future<RandomAccessFile> truncate(int length) { |
| 1040 _ensureFileService(); | 998 _ensureFileService(); |
| 1041 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 999 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 1042 if (closed) return _completeWithClosedException(completer); | 1000 if (_isClosed) return _completeWithClosedException(completer); |
| 1043 List request = new List(3); | 1001 List request = new List(3); |
| 1044 request[0] = _FileUtils.TRUNCATE_REQUEST; | 1002 request[0] = _FileUtils.TRUNCATE_REQUEST; |
| 1045 request[1] = _id; | 1003 request[1] = _id; |
| 1046 request[2] = length; | 1004 request[2] = length; |
| 1047 return _fileService.call(request).transform((response) { | 1005 return _fileService.call(request).transform((response) { |
| 1048 if (_isErrorResponse(response)) { | 1006 if (_isErrorResponse(response)) { |
| 1049 throw _exceptionFromResponse(response, | 1007 throw _exceptionFromResponse(response, |
| 1050 "truncate failed for file '$_name'"); | 1008 "truncate failed for file '$_name'"); |
| 1051 } | 1009 } |
| 1052 return this; | 1010 return this; |
| 1053 }); | 1011 }); |
| 1054 } | 1012 } |
| 1055 | 1013 |
| 1056 void truncateSync(int length) { | 1014 void truncateSync(int length) { |
| 1057 _checkNotClosed(); | 1015 _checkNotClosed(); |
| 1058 var result = _FileUtils.truncate(_id, length); | 1016 var result = _FileUtils.truncate(_id, length); |
| 1059 if (result is OSError) { | 1017 if (result is OSError) { |
| 1060 throw new FileIOException("truncate failed for file '$_name'", result); | 1018 throw new FileIOException("truncate failed for file '$_name'", result); |
| 1061 } | 1019 } |
| 1062 } | 1020 } |
| 1063 | 1021 |
| 1064 Future<int> length() { | 1022 Future<int> length() { |
| 1065 _ensureFileService(); | 1023 _ensureFileService(); |
| 1066 Completer<int> completer = new Completer<int>(); | 1024 Completer<int> completer = new Completer<int>(); |
| 1067 if (closed) return _completeWithClosedException(completer); | 1025 if (_isClosed) return _completeWithClosedException(completer); |
| 1068 List request = new List(2); | 1026 List request = new List(2); |
| 1069 request[0] = _FileUtils.LENGTH_REQUEST; | 1027 request[0] = _FileUtils.LENGTH_REQUEST; |
| 1070 request[1] = _id; | 1028 request[1] = _id; |
| 1071 return _fileService.call(request).transform((response) { | 1029 return _fileService.call(request).transform((response) { |
| 1072 if (_isErrorResponse(response)) { | 1030 if (_isErrorResponse(response)) { |
| 1073 throw _exceptionFromResponse(response, | 1031 throw _exceptionFromResponse(response, |
| 1074 "length failed for file '$_name'"); | 1032 "length failed for file '$_name'"); |
| 1075 } | 1033 } |
| 1076 return response; | 1034 return response; |
| 1077 }); | 1035 }); |
| 1078 } | 1036 } |
| 1079 | 1037 |
| 1080 int lengthSync() { | 1038 int lengthSync() { |
| 1081 _checkNotClosed(); | 1039 _checkNotClosed(); |
| 1082 var result = _FileUtils.length(_id); | 1040 var result = _FileUtils.length(_id); |
| 1083 if (result is OSError) { | 1041 if (result is OSError) { |
| 1084 throw new FileIOException("length failed for file '$_name'", result); | 1042 throw new FileIOException("length failed for file '$_name'", result); |
| 1085 } | 1043 } |
| 1086 return result; | 1044 return result; |
| 1087 } | 1045 } |
| 1088 | 1046 |
| 1089 Future<RandomAccessFile> flush() { | 1047 Future<RandomAccessFile> flush() { |
| 1090 _ensureFileService(); | 1048 _ensureFileService(); |
| 1091 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 1049 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 1092 if (closed) return _completeWithClosedException(completer); | 1050 if (_isClosed) return _completeWithClosedException(completer); |
| 1093 List request = new List(2); | 1051 List request = new List(2); |
| 1094 request[0] = _FileUtils.FLUSH_REQUEST; | 1052 request[0] = _FileUtils.FLUSH_REQUEST; |
| 1095 request[1] = _id; | 1053 request[1] = _id; |
| 1096 return _fileService.call(request).transform((response) { | 1054 return _fileService.call(request).transform((response) { |
| 1097 if (_isErrorResponse(response)) { | 1055 if (_isErrorResponse(response)) { |
| 1098 throw _exceptionFromResponse(response, | 1056 throw _exceptionFromResponse(response, |
| 1099 "flush failed for file '$_name'"); | 1057 "flush failed for file '$_name'"); |
| 1100 } | 1058 } |
| 1101 return this; | 1059 return this; |
| 1102 }); | 1060 }); |
| 1103 } | 1061 } |
| 1104 | 1062 |
| 1105 void flushSync() { | 1063 void flushSync() { |
| 1106 _checkNotClosed(); | 1064 _checkNotClosed(); |
| 1107 var result = _FileUtils.flush(_id); | 1065 var result = _FileUtils.flush(_id); |
| 1108 if (result is OSError) { | 1066 if (result is OSError) { |
| 1109 throw new FileIOException("flush failed for file '$_name'", result); | 1067 throw new FileIOException("flush failed for file '$_name'", result); |
| 1110 } | 1068 } |
| 1111 } | 1069 } |
| 1112 | 1070 |
| 1113 String get name() => _name; | 1071 String get name() => _name; |
| 1114 | 1072 |
| 1115 void _ensureFileService() { | 1073 void _ensureFileService() { |
| 1116 if (_fileService == null) { | 1074 if (_fileService == null) { |
| 1117 _fileService = _FileUtils.newServicePort(); | 1075 _fileService = _FileUtils.newServicePort(); |
| 1118 } | 1076 } |
| 1119 } | 1077 } |
| 1120 | 1078 |
| 1121 bool get closed() => _id == 0; | 1079 bool get _isClosed() => _id == 0; |
| 1122 | 1080 |
| 1123 void _checkNotClosed() { | 1081 void _checkNotClosed() { |
| 1124 if (closed) { | 1082 if (_isClosed) { |
| 1125 throw new FileIOException("File closed '$_name'"); | 1083 throw new FileIOException("File closed '$_name'"); |
| 1126 } | 1084 } |
| 1127 } | 1085 } |
| 1128 | 1086 |
| 1129 Future _completeWithClosedException(Completer completer) { | 1087 Future _completeWithClosedException(Completer completer) { |
| 1130 new Timer(0, (t) { | 1088 new Timer(0, (t) { |
| 1131 completer.completeException( | 1089 completer.completeException( |
| 1132 new FileIOException("File closed '$_name'")); | 1090 new FileIOException("File closed '$_name'")); |
| 1133 }); | 1091 }); |
| 1134 return completer.future; | 1092 return completer.future; |
| 1135 } | 1093 } |
| 1136 | 1094 |
| 1137 final String _name; | 1095 final String _name; |
| 1138 int _id; | 1096 int _id; |
| 1139 | 1097 |
| 1140 SendPort _fileService; | 1098 SendPort _fileService; |
| 1141 } | 1099 } |
| OLD | NEW |