| 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(RandomAccessFile this._file, int this._length) { | 6 _FileInputStream(RandomAccessFile this._file, int this._length) { |
| 7 _streamMarkedClosed = true; | 7 _streamMarkedClosed = true; |
| 8 _checkScheduleCallbacks(); | 8 _checkScheduleCallbacks(); |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 747 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 758 "Mixed use of synchronous and asynchronous API"); | 758 "Mixed use of synchronous and asynchronous API"); |
| 759 } | 759 } |
| 760 String result = _FileUtils.checkedFullPath(_name); | 760 String result = _FileUtils.checkedFullPath(_name); |
| 761 if (result == null) { | 761 if (result == null) { |
| 762 throw new FileIOException("fullPath failed"); | 762 throw new FileIOException("fullPath failed"); |
| 763 } | 763 } |
| 764 return result; | 764 return result; |
| 765 } | 765 } |
| 766 | 766 |
| 767 void openInputStream() { | 767 void openInputStream() { |
| 768 _asyncUsed = true; |
| 768 // Create a new file object to handle the opening of the file for | 769 // Create a new file object to handle the opening of the file for |
| 769 // creating an input stream. Currently the file input stream uses | 770 // creating an input stream. Currently the file input stream uses |
| 770 // synchronous calls on the opened file so we need to open it | 771 // synchronous calls on the opened file so we need to open it |
| 771 // synchronously. | 772 // synchronously. |
| 772 File file = new File(this._name); | 773 File file = new File(this._name); |
| 773 file.errorHandler = (String error) { | 774 file.errorHandler = (String error) { |
| 774 if (_errorHandler != null) _errorHandler(error); | 775 if (_errorHandler != null) _errorHandler(error); |
| 775 }; | 776 }; |
| 776 RandomAccessFile openedFile = file.openSync(); | 777 RandomAccessFile openedFile = file.openSync(); |
| 777 InputStream stream = | 778 InputStream stream = |
| 778 new _FileInputStream(openedFile, openedFile.lengthSync()); | 779 new _FileInputStream(openedFile, openedFile.lengthSync()); |
| 779 new Timer( | 780 new Timer( |
| 780 (Timer ignore) { | 781 (Timer ignore) { |
| 781 if (_inputStreamHandler != null) _inputStreamHandler(stream); | 782 if (_inputStreamHandler != null) _inputStreamHandler(stream); |
| 782 }, 0); | 783 }, 0); |
| 783 } | 784 } |
| 784 | 785 |
| 785 InputStream openInputStreamSync() { | 786 InputStream openInputStreamSync() { |
| 786 if (_asyncUsed) { | 787 if (_asyncUsed) { |
| 787 throw new FileIOException( | 788 throw new FileIOException( |
| 788 "Mixed use of synchronous and asynchronous API"); | 789 "Mixed use of synchronous and asynchronous API"); |
| 789 } | 790 } |
| 790 RandomAccessFile openedFile = openSync(); | 791 RandomAccessFile openedFile = openSync(); |
| 791 return new _FileInputStream(openedFile, openedFile.lengthSync()); | 792 return new _FileInputStream(openedFile, openedFile.lengthSync()); |
| 792 } | 793 } |
| 793 | 794 |
| 794 void openOutputStream([FileMode mode = FileMode.WRITE]) { | 795 void openOutputStream([FileMode mode = FileMode.WRITE]) { |
| 796 _asyncUsed = true; |
| 795 if (mode != FileMode.WRITE && | 797 if (mode != FileMode.WRITE && |
| 796 mode != FileMode.APPEND) { | 798 mode != FileMode.APPEND) { |
| 797 throw new FileIOException( | 799 throw new FileIOException( |
| 798 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); | 800 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); |
| 799 } | 801 } |
| 800 // Create a new file object to handle the opening of the file for | 802 // Create a new file object to handle the opening of the file for |
| 801 // creating an input stream. Currently the file input stream uses | 803 // creating an input stream. Currently the file input stream uses |
| 802 // synchronous calls on the opened file so we need to open it | 804 // synchronous calls on the opened file so we need to open it |
| 803 // synchronously. | 805 // synchronously. |
| 804 File file = new File(this._name); | 806 File file = new File(this._name); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 820 } | 822 } |
| 821 if (mode != FileMode.WRITE && | 823 if (mode != FileMode.WRITE && |
| 822 mode != FileMode.APPEND) { | 824 mode != FileMode.APPEND) { |
| 823 throw new FileIOException( | 825 throw new FileIOException( |
| 824 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); | 826 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); |
| 825 } | 827 } |
| 826 RandomAccessFile openedFile = openSync(mode); | 828 RandomAccessFile openedFile = openSync(mode); |
| 827 return new _FileOutputStream(openedFile); | 829 return new _FileOutputStream(openedFile); |
| 828 } | 830 } |
| 829 | 831 |
| 832 void readAsBytes() { |
| 833 _asyncUsed = true; |
| 834 var chunks = new _BufferList(); |
| 835 openInputStream(); |
| 836 inputStreamHandler = (inputStream) { |
| 837 inputStream.closeHandler = () { |
| 838 if (_readAsBytesHandler != null) { |
| 839 _readAsBytesHandler(chunks.readBytes(chunks.length)); |
| 840 } |
| 841 }; |
| 842 inputStream.dataHandler = () { |
| 843 var chunk = inputStream.read(); |
| 844 chunks.add(chunk); |
| 845 }; |
| 846 inputStream.errorHandler = () { |
| 847 if (_errorHandler != null) { |
| 848 _errorHandler("Failed to read file as bytes: $_name"); |
| 849 } |
| 850 }; |
| 851 }; |
| 852 } |
| 853 |
| 854 List<int> readAsBytesSync() { |
| 855 if (_asyncUsed) { |
| 856 throw new FileIOException( |
| 857 "Mixed use of synchronous and asynchronous API"); |
| 858 } |
| 859 var opened = openSync(); |
| 860 var length = opened.lengthSync(); |
| 861 var result = new ByteArray(length); |
| 862 var read = opened.readListSync(result, 0, length); |
| 863 if (read != length) { |
| 864 throw new FileIOException("Failed reading file as bytes: $_name"); |
| 865 } |
| 866 return result; |
| 867 } |
| 868 |
| 869 _StringDecoder _getDecoder(encoding) { |
| 870 if (encoding == "UTF-8") { |
| 871 return new _UTF8Decoder(); |
| 872 } else if (encoding == "ISO-8859-1") { |
| 873 return new _Latin1Decoder(); |
| 874 } else if (encoding == "ASCII") { |
| 875 return new _AsciiDecoder(); |
| 876 } |
| 877 throw new FileIOException("Unsupported encoding $_encoding"); |
| 878 } |
| 879 |
| 880 void readAsText([String encoding = "UTF-8"]) { |
| 881 _asyncUsed = true; |
| 882 var decoder = _getDecoder(encoding); |
| 883 readAsBytes(); |
| 884 readAsBytesHandler = (bytes) { |
| 885 if (_readAsTextHandler != null) { |
| 886 try { |
| 887 decoder.write(bytes); |
| 888 } catch (var e) { |
| 889 if (_errorHandler != null) { |
| 890 _errorHandler(e.toString()); |
| 891 return; |
| 892 } |
| 893 } |
| 894 _readAsTextHandler(decoder.decoded); |
| 895 } |
| 896 }; |
| 897 } |
| 898 |
| 899 String readAsTextSync([String encoding = "UTF-8"]) { |
| 900 if (_asyncUsed) { |
| 901 throw new FileIOException( |
| 902 "Mixed use of synchronous and asynchronous API"); |
| 903 } |
| 904 var decoder = _getDecoder(encoding); |
| 905 List<int> bytes = readAsBytesSync(); |
| 906 decoder.write(bytes); |
| 907 return decoder.decoded; |
| 908 } |
| 909 |
| 910 List<String> _getDecodedLines(_StringDecoder decoder) { |
| 911 List<String> result = []; |
| 912 var line = decoder.decodedLine; |
| 913 while (line != null) { |
| 914 result.add(line); |
| 915 line = decoder.decodedLine; |
| 916 } |
| 917 // If there is more data with no terminating line break we treat |
| 918 // it as the last line. |
| 919 var data = decoder.decoded; |
| 920 if (data != null) { |
| 921 result.add(data); |
| 922 } |
| 923 return result; |
| 924 } |
| 925 |
| 926 void readAsLines([String encoding = "UTF-8"]) { |
| 927 _asyncUsed = true; |
| 928 var decoder = _getDecoder(encoding); |
| 929 readAsBytes(); |
| 930 readAsBytesHandler = (bytes) { |
| 931 if (_readAsLinesHandler != null) { |
| 932 try { |
| 933 decoder.write(bytes); |
| 934 } catch (var e) { |
| 935 if (_errorHandler != null) { |
| 936 _errorHandler(e.toString()); |
| 937 return; |
| 938 } |
| 939 } |
| 940 _readAsLinesHandler(_getDecodedLines(decoder)); |
| 941 } |
| 942 }; |
| 943 } |
| 944 |
| 945 List<String> readAsLinesSync([String encoding = "UTF-8"]) { |
| 946 if (_asyncUsed) { |
| 947 throw new FileIOException( |
| 948 "Mixed use of synchronous and asynchronous API"); |
| 949 } |
| 950 var decoder = _getDecoder(encoding); |
| 951 List<int> bytes = readAsBytesSync(); |
| 952 decoder.write(bytes); |
| 953 return _getDecodedLines(decoder); |
| 954 } |
| 955 |
| 830 String get name() => _name; | 956 String get name() => _name; |
| 831 | 957 |
| 832 void set existsHandler(void handler(bool exists)) { | 958 void set existsHandler(void handler(bool exists)) { |
| 833 _existsHandler = handler; | 959 _existsHandler = handler; |
| 834 } | 960 } |
| 835 | 961 |
| 836 void set createHandler(void handler()) { | 962 void set createHandler(void handler()) { |
| 837 _createHandler = handler; | 963 _createHandler = handler; |
| 838 } | 964 } |
| 839 | 965 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 850 } | 976 } |
| 851 | 977 |
| 852 void set inputStreamHandler(void handler(InputStream stream)) { | 978 void set inputStreamHandler(void handler(InputStream stream)) { |
| 853 _inputStreamHandler = handler; | 979 _inputStreamHandler = handler; |
| 854 } | 980 } |
| 855 | 981 |
| 856 void set outputStreamHandler(void handler(OutputStream stream)) { | 982 void set outputStreamHandler(void handler(OutputStream stream)) { |
| 857 _outputStreamHandler = handler; | 983 _outputStreamHandler = handler; |
| 858 } | 984 } |
| 859 | 985 |
| 986 void set readAsBytesHandler(void handler(List<int> bytes)) { |
| 987 _readAsBytesHandler = handler; |
| 988 } |
| 989 |
| 990 void set readAsTextHandler(void handler(String text)) { |
| 991 _readAsTextHandler = handler; |
| 992 } |
| 993 |
| 994 void set readAsLinesHandler(void handler(List<String> lines)) { |
| 995 _readAsLinesHandler = handler; |
| 996 } |
| 997 |
| 860 void set fullPathHandler(void handler(String)) { | 998 void set fullPathHandler(void handler(String)) { |
| 861 _fullPathHandler = handler; | 999 _fullPathHandler = handler; |
| 862 } | 1000 } |
| 863 | 1001 |
| 864 void set errorHandler(void handler(String error)) { | 1002 void set errorHandler(void handler(String error)) { |
| 865 _errorHandler = handler; | 1003 _errorHandler = handler; |
| 866 } | 1004 } |
| 867 | 1005 |
| 868 String _name; | 1006 String _name; |
| 869 bool _asyncUsed; | 1007 bool _asyncUsed; |
| 870 | 1008 |
| 871 _FileOperationScheduler _scheduler; | 1009 _FileOperationScheduler _scheduler; |
| 872 | 1010 |
| 873 Function _existsHandler; | 1011 Function _existsHandler; |
| 874 Function _createHandler; | 1012 Function _createHandler; |
| 875 Function _deleteHandler; | 1013 Function _deleteHandler; |
| 876 Function _directoryHandler; | 1014 Function _directoryHandler; |
| 877 Function _openHandler; | 1015 Function _openHandler; |
| 878 Function _inputStreamHandler; | 1016 Function _inputStreamHandler; |
| 879 Function _outputStreamHandler; | 1017 Function _outputStreamHandler; |
| 1018 Function _readAsBytesHandler; |
| 1019 Function _readAsTextHandler; |
| 1020 Function _readAsLinesHandler; |
| 880 Function _fullPathHandler; | 1021 Function _fullPathHandler; |
| 881 Function _errorHandler; | 1022 Function _errorHandler; |
| 882 } | 1023 } |
| 883 | 1024 |
| 884 | 1025 |
| 885 class _RandomAccessFile implements RandomAccessFile { | 1026 class _RandomAccessFile implements RandomAccessFile { |
| 886 _RandomAccessFile(int this._id, String this._name) | 1027 _RandomAccessFile(int this._id, String this._name) |
| 887 : _scheduler = new _FileOperationScheduler(), | 1028 : _scheduler = new _FileOperationScheduler(), |
| 888 _asyncUsed = false; | 1029 _asyncUsed = false; |
| 889 | 1030 |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1283 Function _readByteHandler; | 1424 Function _readByteHandler; |
| 1284 Function _readListHandler; | 1425 Function _readListHandler; |
| 1285 Function _noPendingWriteHandler; | 1426 Function _noPendingWriteHandler; |
| 1286 Function _positionHandler; | 1427 Function _positionHandler; |
| 1287 Function _setPositionHandler; | 1428 Function _setPositionHandler; |
| 1288 Function _truncateHandler; | 1429 Function _truncateHandler; |
| 1289 Function _lengthHandler; | 1430 Function _lengthHandler; |
| 1290 Function _flushHandler; | 1431 Function _flushHandler; |
| 1291 Function _errorHandler; | 1432 Function _errorHandler; |
| 1292 } | 1433 } |
| OLD | NEW |