Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: runtime/bin/file_impl.dart

Issue 9452014: Implement File.{readAsBytes, readAsText, readAsLines}. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 = [];
Søren Gjesse 2012/02/24 07:24:51 We have the class _BufferList in runtime/bin. var
Mads Ager (google) 2012/02/24 13:11:17 Done.
835 var resultLength = 0;
836 openInputStream();
837 inputStreamHandler = (inputStream) {
838 inputStream.closeHandler = () {
839 if (_readAsBytesHandler != null) {
840 var result = new ByteArray(resultLength);
841 var resultIndex = 0;
842 for (var i = 0; i < chunks.length; i++) {
843 var chunk = chunks[i];
844 result.setRange(resultIndex, chunk.length, chunk);
845 resultIndex += chunk.length;
846 }
847 _readAsBytesHandler(result);
848 }
849 };
850 inputStream.dataHandler = () {
851 var chunk = inputStream.read();
852 chunks.add(chunk);
853 resultLength += chunk.length;
854 };
855 inputStream.errorHandler = () {
856 if (_errorHandler != null) {
857 _errorHandler("Failed to read file as bytes: $_name");
858 }
859 };
860 };
861 }
862
863 List<int> readAsBytesSync() {
864 if (_asyncUsed) {
865 throw new FileIOException(
866 "Mixed use of synchronous and asynchronous API");
867 }
868 var opened = openSync();
869 var length = opened.lengthSync();
870 var result = new ByteArray(length);
871 var read = opened.readListSync(result, 0, length);
872 if (read != length) {
873 throw new FileIOException("Failed reading file as bytes: $_name");
874 }
875 return result;
876 }
877
878 _StringDecoder _getDecoder(encoding) {
879 if (encoding == "UTF-8") {
880 return new _UTF8Decoder();
881 } else if (_encoding == "ISO-8859-1") {
882 return new _Latin1Decoder();
883 } else if (_encoding == "ASCII") {
884 return new _AsciiDecoder();
885 }
886 throw new FileIOException("Unsupported encoding $_encoding");
887 }
888
889 void readAsText([String encoding = "UTF-8"]) {
890 _asyncUsed = true;
891 var decoder = _getDecoder(encoding);
892 readAsBytes();
893 readAsBytesHandler = (bytes) {
894 if (_readAsTextHandler != null) {
895 decoder.write(bytes);
896 _readAsTextHandler(decoder.decoded);
897 }
898 };
899 }
900
901 String readAsTextSync([String encoding = "UTF-8"]) {
902 if (_asyncUsed) {
903 throw new FileIOException(
904 "Mixed use of synchronous and asynchronous API");
905 }
906 var decoder = _getDecoder(encoding);
907 List<int> bytes = readAsBytesSync();
908 decoder.write(bytes);
909 return decoder.decoded;
910 }
911
912 List<String> _getDecodedLines(_StringDecoder decoder) {
913 List<String> result = [];
914 var line = decoder.decodedLine;
915 while (line != null) {
916 result.add(line);
917 line = decoder.decodedLine;
918 }
919 // If there is more data with no terminating line break we treat
920 // it as the last line.
921 var data = decoder.decoded;
922 if (data != null) {
923 result.add(data);
924 }
925 return result;
926 }
927
928 void readAsLines([String encoding = "UTF-8"]) {
929 _asyncUsed = true;
930 var decoder = _getDecoder(encoding);
931 readAsBytes();
932 readAsBytesHandler = (bytes) {
933 if (_readAsLinesHandler != null) {
934 decoder.write(bytes);
935 _readAsLinesHandler(_getDecodedLines(decoder));
936 }
937 };
938 }
939
940 List<String> readAsLinesSync([String encoding = "UTF-8"]) {
941 if (_asyncUsed) {
942 throw new FileIOException(
943 "Mixed use of synchronous and asynchronous API");
944 }
945 var decoder = _getDecoder(encoding);
946 List<int> bytes = readAsBytesSync();
947 decoder.write(bytes);
948 return _getDecodedLines(decoder);
949 }
950
830 String get name() => _name; 951 String get name() => _name;
831 952
832 void set existsHandler(void handler(bool exists)) { 953 void set existsHandler(void handler(bool exists)) {
833 _existsHandler = handler; 954 _existsHandler = handler;
834 } 955 }
835 956
836 void set createHandler(void handler()) { 957 void set createHandler(void handler()) {
837 _createHandler = handler; 958 _createHandler = handler;
838 } 959 }
839 960
(...skipping 10 matching lines...) Expand all
850 } 971 }
851 972
852 void set inputStreamHandler(void handler(InputStream stream)) { 973 void set inputStreamHandler(void handler(InputStream stream)) {
853 _inputStreamHandler = handler; 974 _inputStreamHandler = handler;
854 } 975 }
855 976
856 void set outputStreamHandler(void handler(OutputStream stream)) { 977 void set outputStreamHandler(void handler(OutputStream stream)) {
857 _outputStreamHandler = handler; 978 _outputStreamHandler = handler;
858 } 979 }
859 980
981 void set readAsBytesHandler(void handler(List<int> bytes)) {
982 _readAsBytesHandler = handler;
983 }
984
985 void set readAsTextHandler(void handler(String text)) {
986 _readAsTextHandler = handler;
987 }
988
989 void set readAsLinesHandler(void handler(List<String> lines)) {
990 _readAsLinesHandler = handler;
991 }
992
860 void set fullPathHandler(void handler(String)) { 993 void set fullPathHandler(void handler(String)) {
861 _fullPathHandler = handler; 994 _fullPathHandler = handler;
862 } 995 }
863 996
864 void set errorHandler(void handler(String error)) { 997 void set errorHandler(void handler(String error)) {
865 _errorHandler = handler; 998 _errorHandler = handler;
866 } 999 }
867 1000
868 String _name; 1001 String _name;
869 bool _asyncUsed; 1002 bool _asyncUsed;
870 1003
871 _FileOperationScheduler _scheduler; 1004 _FileOperationScheduler _scheduler;
872 1005
873 Function _existsHandler; 1006 Function _existsHandler;
874 Function _createHandler; 1007 Function _createHandler;
875 Function _deleteHandler; 1008 Function _deleteHandler;
876 Function _directoryHandler; 1009 Function _directoryHandler;
877 Function _openHandler; 1010 Function _openHandler;
878 Function _inputStreamHandler; 1011 Function _inputStreamHandler;
879 Function _outputStreamHandler; 1012 Function _outputStreamHandler;
1013 Function _readAsBytesHandler;
1014 Function _readAsTextHandler;
1015 Function _readAsLinesHandler;
880 Function _fullPathHandler; 1016 Function _fullPathHandler;
881 Function _errorHandler; 1017 Function _errorHandler;
882 } 1018 }
883 1019
884 1020
885 class _RandomAccessFile implements RandomAccessFile { 1021 class _RandomAccessFile implements RandomAccessFile {
886 _RandomAccessFile(int this._id, String this._name) 1022 _RandomAccessFile(int this._id, String this._name)
887 : _scheduler = new _FileOperationScheduler(), 1023 : _scheduler = new _FileOperationScheduler(),
888 _asyncUsed = false; 1024 _asyncUsed = false;
889 1025
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
1283 Function _readByteHandler; 1419 Function _readByteHandler;
1284 Function _readListHandler; 1420 Function _readListHandler;
1285 Function _noPendingWriteHandler; 1421 Function _noPendingWriteHandler;
1286 Function _positionHandler; 1422 Function _positionHandler;
1287 Function _setPositionHandler; 1423 Function _setPositionHandler;
1288 Function _truncateHandler; 1424 Function _truncateHandler;
1289 Function _lengthHandler; 1425 Function _lengthHandler;
1290 Function _flushHandler; 1426 Function _flushHandler;
1291 Function _errorHandler; 1427 Function _errorHandler;
1292 } 1428 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698