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 : _data = [], | |
| 8 _position = 0, | |
| 9 _filePosition = 0 { | |
| 7 var file = new File(name); | 10 var file = new File(name); |
| 8 _data = []; | 11 var future = file.open(FileMode.READ); |
| 9 _position = 0; | 12 future.handleException((e) { |
| 10 var chained = file.open(FileMode.READ).chain((openedFile) { | 13 _reportError(e); |
| 11 return _readDataFromFile(openedFile); | 14 return true; |
| 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(); | |
| 12 }); | 32 }); |
| 13 chained.handleException((e) { | 33 chained.handleException((e) { |
| 14 _reportError(e); | 34 _reportError(e); |
| 15 return true; | 35 return true; |
| 16 }); | 36 }); |
| 37 chained.then((ignored) => _checkScheduleCallbacks()); | |
| 17 } | 38 } |
| 18 | 39 |
| 19 _FileInputStream.fromStdio(int fd) { | 40 Future<int> _fillBuffer() { |
| 20 assert(fd == 0); | 41 Expect.equals(_position, _data.length); |
| 21 var file = _File._openStdioSync(fd); | 42 Expect.isTrue(_filePosition < _fileLength); |
| 22 _data = []; | 43 |
| 23 _position = 0; | 44 int size = Math.min(_bufferLength, _fileLength - _filePosition); |
| 24 _readDataFromFile(file).handleException((e) { | 45 if (_data.length != size) { |
| 25 _reportError(e); | 46 _data = new Uint8List(size); |
| 26 return true; | 47 } |
| 48 var future = _openedFile.readList(_data, 0, _data.length); | |
| 49 future = future.transform((read) { | |
| 50 _filePosition += read; | |
| 51 if (read != _data.length) { | |
| 52 _data.removeRange(read, _data.length - read); | |
| 53 } | |
| 54 _position = 0; | |
| 55 | |
| 56 if (_fileLength == _filePosition) { | |
| 57 _streamMarkedClosed = true; | |
| 58 _openedFile.close(); | |
| 59 } | |
| 60 return read; | |
| 27 }); | 61 }); |
| 28 } | 62 return future; |
| 29 | |
| 30 Future<RandomAccessFile> _closeAfterRead(RandomAccessFile openedFile) { | |
| 31 return openedFile.close().transform((ignore) { | |
| 32 _streamMarkedClosed = true; | |
| 33 _checkScheduleCallbacks(); | |
| 34 return openedFile; | |
| 35 }); | |
| 36 } | |
| 37 | |
| 38 Future<RandomAccessFile> _readDataFromFile(RandomAccessFile openedFile) { | |
| 39 return openedFile.length().chain((length) { | |
| 40 var contents = new Uint8List(length); | |
| 41 if (length != 0) { | |
| 42 return openedFile.readList(contents, 0, length).chain((read) { | |
| 43 if (read != length) { | |
| 44 throw new FileIOException( | |
| 45 'Failed reading file contents in FileInputStream'); | |
| 46 } else { | |
| 47 _data = contents; | |
| 48 } | |
| 49 return _closeAfterRead(openedFile); | |
| 50 }); | |
| 51 } else { | |
| 52 return _closeAfterRead(openedFile); | |
| 53 } | |
| 54 }); | |
| 55 } | 63 } |
| 56 | 64 |
| 57 int available() { | 65 int available() { |
| 58 return _closed ? 0 : _data.length - _position; | 66 return closed ? 0 : _data.length - _position; |
| 59 } | 67 } |
| 60 | 68 |
| 61 void pipe(OutputStream output, [bool close = true]) { | 69 void pipe(OutputStream output, [bool close = true]) { |
| 62 _pipe(this, output, close: close); | 70 _pipe(this, output, close: close); |
| 63 } | 71 } |
| 64 | 72 |
| 73 void _finishRead() { | |
| 74 if (_position == _data.length && !_streamMarkedClosed) { | |
| 75 _fillBuffer().then((ignored) { | |
| 76 _checkScheduleCallbacks(); | |
| 77 }); | |
| 78 } else { | |
| 79 _checkScheduleCallbacks(); | |
| 80 } | |
| 81 } | |
| 82 | |
| 65 List<int> _read(int bytesToRead) { | 83 List<int> _read(int bytesToRead) { |
| 66 List<int> result = new Uint8List(bytesToRead); | 84 List<int> result; |
| 67 result.setRange(0, bytesToRead, _data, _position); | 85 if (_position == 0 && bytesToRead == _data.length) { |
| 68 _position += bytesToRead; | 86 result = _data; |
| 69 _checkScheduleCallbacks(); | 87 _data = []; |
| 88 } else { | |
| 89 result = new Uint8List(bytesToRead); | |
| 90 result.setRange(0, bytesToRead, _data, _position); | |
| 91 _position += bytesToRead; | |
| 92 } | |
| 93 _finishRead(); | |
| 70 return result; | 94 return result; |
| 71 } | 95 } |
| 72 | 96 |
| 73 int _readInto(List<int> buffer, int offset, int len) { | 97 int _readInto(List<int> buffer, int offset, int len) { |
| 74 buffer.setRange(offset, len, _data, _position); | 98 buffer.setRange(offset, len, _data, _position); |
| 75 _position += len; | 99 _position += len; |
| 76 _checkScheduleCallbacks(); | 100 _finishRead(); |
| 77 return len; | 101 return len; |
| 78 } | 102 } |
| 79 | 103 |
| 80 void _close() { | 104 void _close() { |
| 81 if (_closed) return; | 105 _streamMarkedClosed = true; |
| 82 _closed = true; | 106 _data = []; |
| 107 _position = 0; | |
| 108 if (!_openedFile.closed) { | |
| 109 _openedFile.close(); | |
| 110 } | |
| 83 } | 111 } |
| 84 | 112 |
| 113 static final int _bufferLength = 64 * 1024; | |
| 114 | |
| 115 RandomAccessFile _openedFile; | |
| 85 List<int> _data; | 116 List<int> _data; |
| 86 int _position; | 117 int _position; |
| 87 bool _closed = false; | 118 int _filePosition; |
| 119 int _fileLength; | |
| 88 } | 120 } |
| 89 | 121 |
| 90 | 122 |
| 91 class _FileOutputStream extends _BaseOutputStream implements OutputStream { | 123 class _FileOutputStream extends _BaseOutputStream implements OutputStream { |
| 92 _FileOutputStream(String name, FileMode mode) { | 124 _FileOutputStream(String name, FileMode mode) { |
| 93 _pendingOperations = new List<List<int>>(); | 125 _pendingOperations = new List<List<int>>(); |
| 94 var f = new File(name); | 126 var f = new File(name); |
| 95 var openFuture = f.open(mode); | 127 var openFuture = f.open(mode); |
| 96 openFuture.then((openedFile) { | 128 openFuture.then((openedFile) { |
| 97 _file = openedFile; | 129 _file = openedFile; |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 678 | 710 |
| 679 SendPort _fileService; | 711 SendPort _fileService; |
| 680 } | 712 } |
| 681 | 713 |
| 682 | 714 |
| 683 class _RandomAccessFile extends _FileBase implements RandomAccessFile { | 715 class _RandomAccessFile extends _FileBase implements RandomAccessFile { |
| 684 _RandomAccessFile(int this._id, String this._name); | 716 _RandomAccessFile(int this._id, String this._name); |
| 685 | 717 |
| 686 Future<RandomAccessFile> close() { | 718 Future<RandomAccessFile> close() { |
| 687 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 719 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 688 if (_isClosed) return _completeWithClosedException(completer); | 720 if (closed) return _completeWithClosedException(completer); |
| 689 _ensureFileService(); | 721 _ensureFileService(); |
| 690 List request = new List(2); | 722 List request = new List(2); |
| 691 request[0] = _FileUtils.CLOSE_REQUEST; | 723 request[0] = _FileUtils.CLOSE_REQUEST; |
| 692 request[1] = _id; | 724 request[1] = _id; |
| 693 // Set the id_ to 0 (NULL) to ensure the no more async requests | 725 // Set the id_ to 0 (NULL) to ensure the no more async requests |
| 694 // can be issues for this file. | 726 // can be issued for this file. |
| 695 _id = 0; | 727 _id = 0; |
| 696 return _fileService.call(request).transform((result) { | 728 return _fileService.call(request).transform((result) { |
| 697 if (result != -1) { | 729 if (result != -1) { |
| 698 _id = result; | 730 _id = result; |
| 699 return this; | 731 return this; |
| 700 } else { | 732 } else { |
| 701 throw new FileIOException("Cannot close file '$_name'"); | 733 throw new FileIOException("Cannot close file '$_name'"); |
| 702 } | 734 } |
| 703 }); | 735 }); |
| 704 } | 736 } |
| 705 | 737 |
| 706 void closeSync() { | 738 void closeSync() { |
| 707 var id = _FileUtils.close(_id); | 739 var id = _FileUtils.close(_id); |
| 708 if (id == -1) { | 740 if (id == -1) { |
| 709 throw new FileIOException("Cannot close file '$_name'"); | 741 throw new FileIOException("Cannot close file '$_name'"); |
| 710 } | 742 } |
| 711 _id = id; | 743 _id = id; |
| 712 } | 744 } |
| 713 | 745 |
| 714 Future<int> readByte() { | 746 Future<int> readByte() { |
| 715 _ensureFileService(); | 747 _ensureFileService(); |
| 716 Completer<int> completer = new Completer<int>(); | 748 Completer<int> completer = new Completer<int>(); |
| 717 if (_isClosed) return _completeWithClosedException(completer); | 749 if (closed) return _completeWithClosedException(completer); |
| 718 List request = new List(2); | 750 List request = new List(2); |
| 719 request[0] = _FileUtils.READ_BYTE_REQUEST; | 751 request[0] = _FileUtils.READ_BYTE_REQUEST; |
| 720 request[1] = _id; | 752 request[1] = _id; |
| 721 return _fileService.call(request).transform((response) { | 753 return _fileService.call(request).transform((response) { |
| 722 if (_isErrorResponse(response)) { | 754 if (_isErrorResponse(response)) { |
| 723 throw _exceptionFromResponse(response, | 755 throw _exceptionFromResponse(response, |
| 724 "readByte failed for file '$_name'"); | 756 "readByte failed for file '$_name'"); |
| 725 } | 757 } |
| 726 return response; | 758 return response; |
| 727 }); | 759 }); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 742 if (buffer is !List || offset is !int || bytes is !int) { | 774 if (buffer is !List || offset is !int || bytes is !int) { |
| 743 // Complete asynchronously so the user has a chance to setup | 775 // Complete asynchronously so the user has a chance to setup |
| 744 // handlers without getting exceptions when registering the | 776 // handlers without getting exceptions when registering the |
| 745 // then handler. | 777 // then handler. |
| 746 new Timer(0, (t) { | 778 new Timer(0, (t) { |
| 747 completer.completeException(new FileIOException( | 779 completer.completeException(new FileIOException( |
| 748 "Invalid arguments to readList for file '$_name'")); | 780 "Invalid arguments to readList for file '$_name'")); |
| 749 }); | 781 }); |
| 750 return completer.future; | 782 return completer.future; |
| 751 }; | 783 }; |
| 752 if (_isClosed) return _completeWithClosedException(completer); | 784 if (closed) return _completeWithClosedException(completer); |
| 753 List request = new List(3); | 785 List request = new List(3); |
| 754 request[0] = _FileUtils.READ_LIST_REQUEST; | 786 request[0] = _FileUtils.READ_LIST_REQUEST; |
| 755 request[1] = _id; | 787 request[1] = _id; |
| 756 request[2] = bytes; | 788 request[2] = bytes; |
| 757 return _fileService.call(request).transform((response) { | 789 return _fileService.call(request).transform((response) { |
| 758 if (_isErrorResponse(response)) { | 790 if (_isErrorResponse(response)) { |
| 759 throw _exceptionFromResponse(response, | 791 throw _exceptionFromResponse(response, |
| 760 "readList failed for file '$_name'"); | 792 "readList failed for file '$_name'"); |
| 761 } | 793 } |
| 762 var read = response[1]; | 794 var read = response[1]; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 792 if (value is !int) { | 824 if (value is !int) { |
| 793 // Complete asynchronously so the user has a chance to setup | 825 // Complete asynchronously so the user has a chance to setup |
| 794 // handlers without getting exceptions when registering the | 826 // handlers without getting exceptions when registering the |
| 795 // then handler. | 827 // then handler. |
| 796 new Timer(0, (t) { | 828 new Timer(0, (t) { |
| 797 completer.completeException(new FileIOException( | 829 completer.completeException(new FileIOException( |
| 798 "Invalid argument to writeByte for file '$_name'")); | 830 "Invalid argument to writeByte for file '$_name'")); |
| 799 }); | 831 }); |
| 800 return completer.future; | 832 return completer.future; |
| 801 } | 833 } |
| 802 if (_isClosed) return _completeWithClosedException(completer); | 834 if (closed) return _completeWithClosedException(completer); |
| 803 List request = new List(3); | 835 List request = new List(3); |
| 804 request[0] = _FileUtils.WRITE_BYTE_REQUEST; | 836 request[0] = _FileUtils.WRITE_BYTE_REQUEST; |
| 805 request[1] = _id; | 837 request[1] = _id; |
| 806 request[2] = value; | 838 request[2] = value; |
| 807 return _fileService.call(request).transform((response) { | 839 return _fileService.call(request).transform((response) { |
| 808 if (_isErrorResponse(response)) { | 840 if (_isErrorResponse(response)) { |
| 809 throw _exceptionFromResponse(response, | 841 throw _exceptionFromResponse(response, |
| 810 "writeByte failed for file '$_name'"); | 842 "writeByte failed for file '$_name'"); |
| 811 } | 843 } |
| 812 return this; | 844 return this; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 833 if (buffer is !List || offset is !int || bytes is !int) { | 865 if (buffer is !List || offset is !int || bytes is !int) { |
| 834 // Complete asynchronously so the user has a chance to setup | 866 // Complete asynchronously so the user has a chance to setup |
| 835 // handlers without getting exceptions when registering the | 867 // handlers without getting exceptions when registering the |
| 836 // then handler. | 868 // then handler. |
| 837 new Timer(0, (t) { | 869 new Timer(0, (t) { |
| 838 completer.completeException(new FileIOException( | 870 completer.completeException(new FileIOException( |
| 839 "Invalid arguments to writeList for file '$_name'")); | 871 "Invalid arguments to writeList for file '$_name'")); |
| 840 }); | 872 }); |
| 841 return completer.future; | 873 return completer.future; |
| 842 } | 874 } |
| 843 if (_isClosed) return _completeWithClosedException(completer); | 875 if (closed) return _completeWithClosedException(completer); |
| 844 | 876 |
| 845 List result; | 877 List result; |
| 846 try { | 878 try { |
| 847 result = | 879 result = |
| 848 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); | 880 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); |
| 849 } catch (var e) { | 881 } catch (var e) { |
| 850 // Complete asynchronously so the user has a chance to setup | 882 // Complete asynchronously so the user has a chance to setup |
| 851 // handlers without getting exceptions when registering the | 883 // handlers without getting exceptions when registering the |
| 852 // then handler. | 884 // then handler. |
| 853 new Timer(0, (t) => completer.completeException(e)); | 885 new Timer(0, (t) => completer.completeException(e)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 887 if (result is OSError) { | 919 if (result is OSError) { |
| 888 throw new FileIOException("writeList failed for file '$_name'", result); | 920 throw new FileIOException("writeList failed for file '$_name'", result); |
| 889 } | 921 } |
| 890 return result; | 922 return result; |
| 891 } | 923 } |
| 892 | 924 |
| 893 Future<RandomAccessFile> writeString(String string, | 925 Future<RandomAccessFile> writeString(String string, |
| 894 [Encoding encoding = Encoding.UTF_8]) { | 926 [Encoding encoding = Encoding.UTF_8]) { |
| 895 _ensureFileService(); | 927 _ensureFileService(); |
| 896 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 928 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 897 if (_isClosed) return _completeWithClosedException(completer); | 929 if (closed) return _completeWithClosedException(completer); |
| 898 List request = new List(3); | 930 List request = new List(3); |
| 899 request[0] = _FileUtils.WRITE_STRING_REQUEST; | 931 request[0] = _FileUtils.WRITE_STRING_REQUEST; |
| 900 request[1] = _id; | 932 request[1] = _id; |
| 901 request[2] = string; | 933 request[2] = string; |
| 902 return _fileService.call(request).transform((response) { | 934 return _fileService.call(request).transform((response) { |
| 903 if (_isErrorResponse(response)) { | 935 if (_isErrorResponse(response)) { |
| 904 throw _exceptionFromResponse(response, | 936 throw _exceptionFromResponse(response, |
| 905 "writeString failed for file '$_name'"); | 937 "writeString failed for file '$_name'"); |
| 906 } | 938 } |
| 907 return this; | 939 return this; |
| 908 }); | 940 }); |
| 909 } | 941 } |
| 910 | 942 |
| 911 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { | 943 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { |
| 912 _checkNotClosed(); | 944 _checkNotClosed(); |
| 913 var result = _FileUtils.checkedWriteString(_id, string); | 945 var result = _FileUtils.checkedWriteString(_id, string); |
| 914 if (result is OSError) { | 946 if (result is OSError) { |
| 915 throw new FileIOException("writeString failed for file '$_name'"); | 947 throw new FileIOException("writeString failed for file '$_name'"); |
| 916 } | 948 } |
| 917 return result; | 949 return result; |
| 918 } | 950 } |
| 919 | 951 |
| 920 Future<int> position() { | 952 Future<int> position() { |
| 921 _ensureFileService(); | 953 _ensureFileService(); |
| 922 Completer<int> completer = new Completer<int>(); | 954 Completer<int> completer = new Completer<int>(); |
| 923 if (_isClosed) return _completeWithClosedException(completer); | 955 if (closed) return _completeWithClosedException(completer); |
| 924 List request = new List(2); | 956 List request = new List(2); |
| 925 request[0] = _FileUtils.POSITION_REQUEST; | 957 request[0] = _FileUtils.POSITION_REQUEST; |
| 926 request[1] = _id; | 958 request[1] = _id; |
| 927 return _fileService.call(request).transform((response) { | 959 return _fileService.call(request).transform((response) { |
| 928 if (_isErrorResponse(response)) { | 960 if (_isErrorResponse(response)) { |
| 929 throw _exceptionFromResponse(response, | 961 throw _exceptionFromResponse(response, |
| 930 "position failed for file '$_name'"); | 962 "position failed for file '$_name'"); |
| 931 } | 963 } |
| 932 return response; | 964 return response; |
| 933 }); | 965 }); |
| 934 } | 966 } |
| 935 | 967 |
| 936 int positionSync() { | 968 int positionSync() { |
| 937 _checkNotClosed(); | 969 _checkNotClosed(); |
| 938 var result = _FileUtils.position(_id); | 970 var result = _FileUtils.position(_id); |
| 939 if (result is OSError) { | 971 if (result is OSError) { |
| 940 throw new FileIOException("position failed for file '$_name'", result); | 972 throw new FileIOException("position failed for file '$_name'", result); |
| 941 } | 973 } |
| 942 return result; | 974 return result; |
| 943 } | 975 } |
| 944 | 976 |
| 945 Future<RandomAccessFile> setPosition(int position) { | 977 Future<RandomAccessFile> setPosition(int position) { |
| 946 _ensureFileService(); | 978 _ensureFileService(); |
| 947 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 979 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 948 if (_isClosed) return _completeWithClosedException(completer); | 980 if (closed) return _completeWithClosedException(completer); |
| 949 List request = new List(3); | 981 List request = new List(3); |
| 950 request[0] = _FileUtils.SET_POSITION_REQUEST; | 982 request[0] = _FileUtils.SET_POSITION_REQUEST; |
| 951 request[1] = _id; | 983 request[1] = _id; |
| 952 request[2] = position; | 984 request[2] = position; |
| 953 return _fileService.call(request).transform((response) { | 985 return _fileService.call(request).transform((response) { |
| 954 if (_isErrorResponse(response)) { | 986 if (_isErrorResponse(response)) { |
| 955 throw _exceptionFromResponse(response, | 987 throw _exceptionFromResponse(response, |
| 956 "setPosition failed for file '$_name'"); | 988 "setPosition failed for file '$_name'"); |
| 957 } | 989 } |
| 958 return this; | 990 return this; |
| 959 }); | 991 }); |
| 960 } | 992 } |
| 961 | 993 |
| 962 void setPositionSync(int position) { | 994 void setPositionSync(int position) { |
| 963 _checkNotClosed(); | 995 _checkNotClosed(); |
| 964 var result = _FileUtils.setPosition(_id, position); | 996 var result = _FileUtils.setPosition(_id, position); |
| 965 if (result is OSError) { | 997 if (result is OSError) { |
| 966 throw new FileIOException("setPosition failed for file '$_name'", result); | 998 throw new FileIOException("setPosition failed for file '$_name'", result); |
| 967 } | 999 } |
| 968 } | 1000 } |
| 969 | 1001 |
| 970 Future<RandomAccessFile> truncate(int length) { | 1002 Future<RandomAccessFile> truncate(int length) { |
| 971 _ensureFileService(); | 1003 _ensureFileService(); |
| 972 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 1004 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 973 if (_isClosed) return _completeWithClosedException(completer); | 1005 if (closed) return _completeWithClosedException(completer); |
| 974 List request = new List(3); | 1006 List request = new List(3); |
| 975 request[0] = _FileUtils.TRUNCATE_REQUEST; | 1007 request[0] = _FileUtils.TRUNCATE_REQUEST; |
| 976 request[1] = _id; | 1008 request[1] = _id; |
| 977 request[2] = length; | 1009 request[2] = length; |
| 978 return _fileService.call(request).transform((response) { | 1010 return _fileService.call(request).transform((response) { |
| 979 if (_isErrorResponse(response)) { | 1011 if (_isErrorResponse(response)) { |
| 980 throw _exceptionFromResponse(response, | 1012 throw _exceptionFromResponse(response, |
| 981 "truncate failed for file '$_name'"); | 1013 "truncate failed for file '$_name'"); |
| 982 } | 1014 } |
| 983 return this; | 1015 return this; |
| 984 }); | 1016 }); |
| 985 } | 1017 } |
| 986 | 1018 |
| 987 void truncateSync(int length) { | 1019 void truncateSync(int length) { |
| 988 _checkNotClosed(); | 1020 _checkNotClosed(); |
| 989 var result = _FileUtils.truncate(_id, length); | 1021 var result = _FileUtils.truncate(_id, length); |
| 990 if (result is OSError) { | 1022 if (result is OSError) { |
| 991 throw new FileIOException("truncate failed for file '$_name'", result); | 1023 throw new FileIOException("truncate failed for file '$_name'", result); |
| 992 } | 1024 } |
| 993 } | 1025 } |
| 994 | 1026 |
| 995 Future<int> length() { | 1027 Future<int> length() { |
| 996 _ensureFileService(); | 1028 _ensureFileService(); |
| 997 Completer<int> completer = new Completer<int>(); | 1029 Completer<int> completer = new Completer<int>(); |
| 998 if (_isClosed) return _completeWithClosedException(completer); | 1030 if (closed) return _completeWithClosedException(completer); |
| 999 List request = new List(2); | 1031 List request = new List(2); |
| 1000 request[0] = _FileUtils.LENGTH_REQUEST; | 1032 request[0] = _FileUtils.LENGTH_REQUEST; |
| 1001 request[1] = _id; | 1033 request[1] = _id; |
| 1002 return _fileService.call(request).transform((response) { | 1034 return _fileService.call(request).transform((response) { |
| 1003 if (_isErrorResponse(response)) { | 1035 if (_isErrorResponse(response)) { |
| 1004 throw _exceptionFromResponse(response, | 1036 throw _exceptionFromResponse(response, |
| 1005 "length failed for file '$_name'"); | 1037 "length failed for file '$_name'"); |
| 1006 } | 1038 } |
| 1007 return response; | 1039 return response; |
| 1008 }); | 1040 }); |
| 1009 } | 1041 } |
| 1010 | 1042 |
| 1011 int lengthSync() { | 1043 int lengthSync() { |
| 1012 _checkNotClosed(); | 1044 _checkNotClosed(); |
| 1013 var result = _FileUtils.length(_id); | 1045 var result = _FileUtils.length(_id); |
| 1014 if (result is OSError) { | 1046 if (result is OSError) { |
| 1015 throw new FileIOException("length failed for file '$_name'", result); | 1047 throw new FileIOException("length failed for file '$_name'", result); |
| 1016 } | 1048 } |
| 1017 return result; | 1049 return result; |
| 1018 } | 1050 } |
| 1019 | 1051 |
| 1020 Future<RandomAccessFile> flush() { | 1052 Future<RandomAccessFile> flush() { |
| 1021 _ensureFileService(); | 1053 _ensureFileService(); |
| 1022 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 1054 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 1023 if (_isClosed) return _completeWithClosedException(completer); | 1055 if (closed) return _completeWithClosedException(completer); |
| 1024 List request = new List(2); | 1056 List request = new List(2); |
| 1025 request[0] = _FileUtils.FLUSH_REQUEST; | 1057 request[0] = _FileUtils.FLUSH_REQUEST; |
| 1026 request[1] = _id; | 1058 request[1] = _id; |
| 1027 return _fileService.call(request).transform((response) { | 1059 return _fileService.call(request).transform((response) { |
| 1028 if (_isErrorResponse(response)) { | 1060 if (_isErrorResponse(response)) { |
| 1029 throw _exceptionFromResponse(response, | 1061 throw _exceptionFromResponse(response, |
| 1030 "flush failed for file '$_name'"); | 1062 "flush failed for file '$_name'"); |
| 1031 } | 1063 } |
| 1032 return this; | 1064 return this; |
| 1033 }); | 1065 }); |
| 1034 } | 1066 } |
| 1035 | 1067 |
| 1036 void flushSync() { | 1068 void flushSync() { |
| 1037 _checkNotClosed(); | 1069 _checkNotClosed(); |
| 1038 var result = _FileUtils.flush(_id); | 1070 var result = _FileUtils.flush(_id); |
| 1039 if (result is OSError) { | 1071 if (result is OSError) { |
| 1040 throw new FileIOException("flush failed for file '$_name'", result); | 1072 throw new FileIOException("flush failed for file '$_name'", result); |
| 1041 } | 1073 } |
| 1042 } | 1074 } |
| 1043 | 1075 |
| 1044 String get name() => _name; | 1076 String get name() => _name; |
| 1045 | 1077 |
| 1046 void _ensureFileService() { | 1078 void _ensureFileService() { |
| 1047 if (_fileService == null) { | 1079 if (_fileService == null) { |
| 1048 _fileService = _FileUtils.newServicePort(); | 1080 _fileService = _FileUtils.newServicePort(); |
| 1049 } | 1081 } |
| 1050 } | 1082 } |
| 1051 | 1083 |
| 1052 bool get _isClosed() => _id == 0; | 1084 bool get closed() => _id == 0; |
|
Mads Ager (google)
2012/06/08 05:55:58
You are exposing this now. Add it to the interface
| |
| 1053 | 1085 |
| 1054 void _checkNotClosed() { | 1086 void _checkNotClosed() { |
| 1055 if (_isClosed) { | 1087 if (closed) { |
| 1056 throw new FileIOException("File closed '$_name'"); | 1088 throw new FileIOException("File closed '$_name'"); |
| 1057 } | 1089 } |
| 1058 } | 1090 } |
| 1059 | 1091 |
| 1060 Future _completeWithClosedException(Completer completer) { | 1092 Future _completeWithClosedException(Completer completer) { |
| 1061 new Timer(0, (t) { | 1093 new Timer(0, (t) { |
| 1062 completer.completeException( | 1094 completer.completeException( |
| 1063 new FileIOException("File closed '$_name'")); | 1095 new FileIOException("File closed '$_name'")); |
| 1064 }); | 1096 }); |
| 1065 return completer.future; | 1097 return completer.future; |
| 1066 } | 1098 } |
| 1067 | 1099 |
| 1068 final String _name; | 1100 final String _name; |
| 1069 int _id; | 1101 int _id; |
| 1070 | 1102 |
| 1071 SendPort _fileService; | 1103 SendPort _fileService; |
| 1072 } | 1104 } |
| OLD | NEW |