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

Side by Side Diff: sdk/lib/io/file_impl.dart

Issue 12213092: Rework Timer interface. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 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
« no previous file with comments | « sdk/lib/io/chunked_stream.dart ('k') | sdk/lib/io/http_impl.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 part of dart.io; 5 part of dart.io;
6 6
7 class _FileInputStream extends _BaseDataInputStream implements InputStream { 7 class _FileInputStream extends _BaseDataInputStream implements InputStream {
8 _FileInputStream(String name) 8 _FileInputStream(String name)
9 : _data = const [], 9 : _data = const [],
10 _position = 0, 10 _position = 0,
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 _closeCallbackScheduled = true; 217 _closeCallbackScheduled = true;
218 } 218 }
219 } 219 }
220 220
221 void set onNoPendingWrites(void callback()) { 221 void set onNoPendingWrites(void callback()) {
222 _onNoPendingWrites = callback; 222 _onNoPendingWrites = callback;
223 if ((_pendingOperations == null || _pendingOperations.length == 0) && 223 if ((_pendingOperations == null || _pendingOperations.length == 0) &&
224 outstandingWrites == 0 && 224 outstandingWrites == 0 &&
225 !_streamMarkedClosed && 225 !_streamMarkedClosed &&
226 _onNoPendingWrites != null) { 226 _onNoPendingWrites != null) {
227 new Timer(0, (t) { 227 Timer.run(() {
228 if (_onNoPendingWrites != null) { 228 if (_onNoPendingWrites != null) {
229 _onNoPendingWrites(); 229 _onNoPendingWrites();
230 } 230 }
231 }); 231 });
232 } 232 }
233 } 233 }
234 234
235 void set onClosed(void callback()) { 235 void set onClosed(void callback()) {
236 _onClosed = callback; 236 _onClosed = callback;
237 } 237 }
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 throwIfError(result, "Cannot retrieve directory for file '$_name'"); 437 throwIfError(result, "Cannot retrieve directory for file '$_name'");
438 return new Directory(result); 438 return new Directory(result);
439 } 439 }
440 440
441 Future<RandomAccessFile> open([FileMode mode = FileMode.READ]) { 441 Future<RandomAccessFile> open([FileMode mode = FileMode.READ]) {
442 _ensureFileService(); 442 _ensureFileService();
443 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); 443 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>();
444 if (mode != FileMode.READ && 444 if (mode != FileMode.READ &&
445 mode != FileMode.WRITE && 445 mode != FileMode.WRITE &&
446 mode != FileMode.APPEND) { 446 mode != FileMode.APPEND) {
447 new Timer(0, (t) { 447 Timer.run(() {
448 completer.completeError(new ArgumentError()); 448 completer.completeError(new ArgumentError());
449 }); 449 });
450 return completer.future; 450 return completer.future;
451 } 451 }
452 List request = new List.fixedLength(3); 452 List request = new List.fixedLength(3);
453 request[0] = _OPEN_REQUEST; 453 request[0] = _OPEN_REQUEST;
454 request[1] = _name; 454 request[1] = _name;
455 request[2] = mode._mode; // Direct int value for serialization. 455 request[2] = mode._mode; // Direct int value for serialization.
456 return _fileService.call(request).then((response) { 456 return _fileService.call(request).then((response) {
457 if (_isErrorResponse(response)) { 457 if (_isErrorResponse(response)) {
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 var stream = openOutputStream(mode); 658 var stream = openOutputStream(mode);
659 stream.write(bytes); 659 stream.write(bytes);
660 stream.close(); 660 stream.close();
661 stream.onClosed = () { 661 stream.onClosed = () {
662 completer.complete(this); 662 completer.complete(this);
663 }; 663 };
664 stream.onError = (e) { 664 stream.onError = (e) {
665 completer.completeError(e); 665 completer.completeError(e);
666 }; 666 };
667 } catch (e) { 667 } catch (e) {
668 new Timer(0, (t) => completer.completeError(e)); 668 Timer.run(() => completer.completeError(e));
669 return completer.future; 669 return completer.future;
670 } 670 }
671 return completer.future; 671 return completer.future;
672 } 672 }
673 673
674 void writeAsBytesSync(List<int> bytes, [FileMode mode = FileMode.WRITE]) { 674 void writeAsBytesSync(List<int> bytes, [FileMode mode = FileMode.WRITE]) {
675 RandomAccessFile opened = openSync(mode); 675 RandomAccessFile opened = openSync(mode);
676 opened.writeListSync(bytes, 0, bytes.length); 676 opened.writeListSync(bytes, 0, bytes.length);
677 opened.closeSync(); 677 opened.closeSync();
678 } 678 }
679 679
680 Future<File> writeAsString(String contents, 680 Future<File> writeAsString(String contents,
681 {FileMode mode: FileMode.WRITE, 681 {FileMode mode: FileMode.WRITE,
682 Encoding encoding: Encoding.UTF_8}) { 682 Encoding encoding: Encoding.UTF_8}) {
683 try { 683 try {
684 var data = _StringEncoders.encoder(encoding).encodeString(contents); 684 var data = _StringEncoders.encoder(encoding).encodeString(contents);
685 return writeAsBytes(data, mode); 685 return writeAsBytes(data, mode);
686 } catch (e) { 686 } catch (e) {
687 var completer = new Completer(); 687 var completer = new Completer();
688 new Timer(0, (t) => completer.completeError(e)); 688 Timer.run(() => completer.completeError(e));
689 return completer.future; 689 return completer.future;
690 } 690 }
691 } 691 }
692 692
693 void writeAsStringSync(String contents, 693 void writeAsStringSync(String contents,
694 {FileMode mode: FileMode.WRITE, 694 {FileMode mode: FileMode.WRITE,
695 Encoding encoding: Encoding.UTF_8}) { 695 Encoding encoding: Encoding.UTF_8}) {
696 var data = _StringEncoders.encoder(encoding).encodeString(contents); 696 var data = _StringEncoders.encoder(encoding).encodeString(contents);
697 writeAsBytesSync(data, mode); 697 writeAsBytesSync(data, mode);
698 } 698 }
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
780 return result; 780 return result;
781 } 781 }
782 782
783 Future<List<int>> read(int bytes) { 783 Future<List<int>> read(int bytes) {
784 _ensureFileService(); 784 _ensureFileService();
785 Completer<List<int>> completer = new Completer<List<int>>(); 785 Completer<List<int>> completer = new Completer<List<int>>();
786 if (bytes is !int) { 786 if (bytes is !int) {
787 // Complete asynchronously so the user has a chance to setup 787 // Complete asynchronously so the user has a chance to setup
788 // handlers without getting exceptions when registering the 788 // handlers without getting exceptions when registering the
789 // then handler. 789 // then handler.
790 new Timer(0, (t) { 790 Timer.run(() {
791 completer.completeError(new FileIOException( 791 completer.completeError(new FileIOException(
792 "Invalid arguments to read for file '$_name'")); 792 "Invalid arguments to read for file '$_name'"));
793 }); 793 });
794 return completer.future; 794 return completer.future;
795 }; 795 };
796 if (closed) return _completeWithClosedException(completer); 796 if (closed) return _completeWithClosedException(completer);
797 List request = new List(3); 797 List request = new List(3);
798 request[0] = _READ_REQUEST; 798 request[0] = _READ_REQUEST;
799 request[1] = _id; 799 request[1] = _id;
800 request[2] = bytes; 800 request[2] = bytes;
(...skipping 16 matching lines...) Expand all
817 return _read(_id, bytes); 817 return _read(_id, bytes);
818 } 818 }
819 819
820 Future<int> readList(List<int> buffer, int offset, int bytes) { 820 Future<int> readList(List<int> buffer, int offset, int bytes) {
821 _ensureFileService(); 821 _ensureFileService();
822 Completer<int> completer = new Completer<int>(); 822 Completer<int> completer = new Completer<int>();
823 if (buffer is !List || offset is !int || bytes is !int) { 823 if (buffer is !List || offset is !int || bytes is !int) {
824 // Complete asynchronously so the user has a chance to setup 824 // Complete asynchronously so the user has a chance to setup
825 // handlers without getting exceptions when registering the 825 // handlers without getting exceptions when registering the
826 // then handler. 826 // then handler.
827 new Timer(0, (t) { 827 Timer.run(() {
828 completer.completeError(new FileIOException( 828 completer.completeError(new FileIOException(
829 "Invalid arguments to readList for file '$_name'")); 829 "Invalid arguments to readList for file '$_name'"));
830 }); 830 });
831 return completer.future; 831 return completer.future;
832 }; 832 };
833 if (closed) return _completeWithClosedException(completer); 833 if (closed) return _completeWithClosedException(completer);
834 List request = new List.fixedLength(3); 834 List request = new List.fixedLength(3);
835 request[0] = _READ_LIST_REQUEST; 835 request[0] = _READ_LIST_REQUEST;
836 request[1] = _id; 836 request[1] = _id;
837 request[2] = bytes; 837 request[2] = bytes;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
873 return result; 873 return result;
874 } 874 }
875 875
876 Future<RandomAccessFile> writeByte(int value) { 876 Future<RandomAccessFile> writeByte(int value) {
877 _ensureFileService(); 877 _ensureFileService();
878 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); 878 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>();
879 if (value is !int) { 879 if (value is !int) {
880 // Complete asynchronously so the user has a chance to setup 880 // Complete asynchronously so the user has a chance to setup
881 // handlers without getting exceptions when registering the 881 // handlers without getting exceptions when registering the
882 // then handler. 882 // then handler.
883 new Timer(0, (t) { 883 Timer.run(() {
884 completer.completeError(new FileIOException( 884 completer.completeError(new FileIOException(
885 "Invalid argument to writeByte for file '$_name'")); 885 "Invalid argument to writeByte for file '$_name'"));
886 }); 886 });
887 return completer.future; 887 return completer.future;
888 } 888 }
889 if (closed) return _completeWithClosedException(completer); 889 if (closed) return _completeWithClosedException(completer);
890 List request = new List.fixedLength(3); 890 List request = new List.fixedLength(3);
891 request[0] = _WRITE_BYTE_REQUEST; 891 request[0] = _WRITE_BYTE_REQUEST;
892 request[1] = _id; 892 request[1] = _id;
893 request[2] = value; 893 request[2] = value;
(...skipping 22 matching lines...) Expand all
916 return result; 916 return result;
917 } 917 }
918 918
919 Future<RandomAccessFile> writeList(List<int> buffer, int offset, int bytes) { 919 Future<RandomAccessFile> writeList(List<int> buffer, int offset, int bytes) {
920 _ensureFileService(); 920 _ensureFileService();
921 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); 921 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>();
922 if (buffer is !List || offset is !int || bytes is !int) { 922 if (buffer is !List || offset is !int || bytes is !int) {
923 // Complete asynchronously so the user has a chance to setup 923 // Complete asynchronously so the user has a chance to setup
924 // handlers without getting exceptions when registering the 924 // handlers without getting exceptions when registering the
925 // then handler. 925 // then handler.
926 new Timer(0, (t) { 926 Timer.run(() {
927 completer.completeError(new FileIOException( 927 completer.completeError(new FileIOException(
928 "Invalid arguments to writeList for file '$_name'")); 928 "Invalid arguments to writeList for file '$_name'"));
929 }); 929 });
930 return completer.future; 930 return completer.future;
931 } 931 }
932 if (closed) return _completeWithClosedException(completer); 932 if (closed) return _completeWithClosedException(completer);
933 933
934 _BufferAndOffset result; 934 _BufferAndOffset result;
935 try { 935 try {
936 result = _ensureFastAndSerializableBuffer(buffer, offset, bytes); 936 result = _ensureFastAndSerializableBuffer(buffer, offset, bytes);
937 } catch (e) { 937 } catch (e) {
938 // Complete asynchronously so the user has a chance to setup 938 // Complete asynchronously so the user has a chance to setup
939 // handlers without getting exceptions when registering the 939 // handlers without getting exceptions when registering the
940 // then handler. 940 // then handler.
941 new Timer(0, (t) => completer.completeError(e)); 941 Timer.run(() => completer.completeError(e));
942 return completer.future; 942 return completer.future;
943 } 943 }
944 944
945 List request = new List.fixedLength(5); 945 List request = new List.fixedLength(5);
946 request[0] = _WRITE_LIST_REQUEST; 946 request[0] = _WRITE_LIST_REQUEST;
947 request[1] = _id; 947 request[1] = _id;
948 request[2] = result.buffer; 948 request[2] = result.buffer;
949 request[3] = result.offset; 949 request[3] = result.offset;
950 request[4] = bytes; 950 request[4] = bytes;
951 return _fileService.call(request).then((response) { 951 return _fileService.call(request).then((response) {
(...skipping 22 matching lines...) Expand all
974 if (result is OSError) { 974 if (result is OSError) {
975 throw new FileIOException("writeList failed for file '$_name'", result); 975 throw new FileIOException("writeList failed for file '$_name'", result);
976 } 976 }
977 return result; 977 return result;
978 } 978 }
979 979
980 Future<RandomAccessFile> writeString(String string, 980 Future<RandomAccessFile> writeString(String string,
981 [Encoding encoding = Encoding.UTF_8]) { 981 [Encoding encoding = Encoding.UTF_8]) {
982 if (encoding is! Encoding) { 982 if (encoding is! Encoding) {
983 var completer = new Completer(); 983 var completer = new Completer();
984 new Timer(0, (t) { 984 Timer.run(() {
985 completer.completeError(new FileIOException( 985 completer.completeError(new FileIOException(
986 "Invalid encoding in writeString: $encoding")); 986 "Invalid encoding in writeString: $encoding"));
987 }); 987 });
988 return completer.future; 988 return completer.future;
989 } 989 }
990 var data = _StringEncoders.encoder(encoding).encodeString(string); 990 var data = _StringEncoders.encoder(encoding).encodeString(string);
991 return writeList(data, 0, data.length); 991 return writeList(data, 0, data.length);
992 } 992 }
993 993
994 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { 994 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) {
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
1144 1144
1145 bool get closed => _id == 0; 1145 bool get closed => _id == 0;
1146 1146
1147 void _checkNotClosed() { 1147 void _checkNotClosed() {
1148 if (closed) { 1148 if (closed) {
1149 throw new FileIOException("File closed '$_name'"); 1149 throw new FileIOException("File closed '$_name'");
1150 } 1150 }
1151 } 1151 }
1152 1152
1153 Future _completeWithClosedException(Completer completer) { 1153 Future _completeWithClosedException(Completer completer) {
1154 new Timer(0, (t) { 1154 Timer.run(() {
1155 completer.completeError( 1155 completer.completeError(
1156 new FileIOException("File closed '$_name'")); 1156 new FileIOException("File closed '$_name'"));
1157 }); 1157 });
1158 return completer.future; 1158 return completer.future;
1159 } 1159 }
1160 1160
1161 final String _name; 1161 final String _name;
1162 int _id; 1162 int _id;
1163 1163
1164 SendPort _fileService; 1164 SendPort _fileService;
1165 } 1165 }
OLDNEW
« no previous file with comments | « sdk/lib/io/chunked_stream.dart ('k') | sdk/lib/io/http_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698