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