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(File file) { | 6 _FileInputStream(File file) { |
7 _file = file.openSync(); | 7 _file = file.openSync(); |
8 _length = _file.lengthSync(); | 8 _length = _file.lengthSync(); |
9 _streamMarkedClosed = true; | 9 _streamMarkedClosed = true; |
10 _checkScheduleCallbacks(); | 10 _checkScheduleCallbacks(); |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 _asyncUsed = false; | 495 _asyncUsed = false; |
496 | 496 |
497 void exists() { | 497 void exists() { |
498 _asyncUsed = true; | 498 _asyncUsed = true; |
499 if (_name is !String) { | 499 if (_name is !String) { |
500 if (_errorHandler != null) { | 500 if (_errorHandler != null) { |
501 _errorHandler('File name is not a string: $_name'); | 501 _errorHandler('File name is not a string: $_name'); |
502 } | 502 } |
503 return; | 503 return; |
504 } | 504 } |
505 var handler = | |
506 (_existsHandler != null) ? _existsHandler : (result) => null; | |
507 var operation = new _ExistsOperation(_name); | 505 var operation = new _ExistsOperation(_name); |
508 _scheduler.enqueue(operation, (result, ignored) { _existsHandler(result); })
; | 506 _scheduler.enqueue(operation, (result, ignored) { |
| 507 var handler = |
| 508 (_existsHandler != null) ? _existsHandler : (result) => null; |
| 509 handler(result); |
| 510 }); |
509 } | 511 } |
510 | 512 |
511 bool existsSync() { | 513 bool existsSync() { |
512 if (_asyncUsed) { | 514 if (_asyncUsed) { |
513 throw new FileIOException( | 515 throw new FileIOException( |
514 "Mixed use of synchronous and asynchronous API"); | 516 "Mixed use of synchronous and asynchronous API"); |
515 } | 517 } |
516 if (_name is !String) { | 518 if (_name is !String) { |
517 throw new FileIOException('File name is not a string: $_name'); | 519 throw new FileIOException('File name is not a string: $_name'); |
518 } | 520 } |
519 return _FileUtils.exists(_name); | 521 return _FileUtils.exists(_name); |
520 } | 522 } |
521 | 523 |
522 void create() { | 524 void create() { |
523 _asyncUsed = true; | 525 _asyncUsed = true; |
524 var handler = (_createHandler != null) ? _createHandler : () => null; | |
525 var handleCreateResult = (created, ignored) { | 526 var handleCreateResult = (created, ignored) { |
| 527 var handler = (_createHandler != null) ? _createHandler : () => null; |
526 if (created) { | 528 if (created) { |
527 handler(); | 529 handler(); |
528 } else if (_errorHandler != null) { | 530 } else if (_errorHandler != null) { |
529 _errorHandler("Cannot create file: $_name"); | 531 _errorHandler("Cannot create file: $_name"); |
530 } | 532 } |
531 }; | 533 }; |
532 var operation = new _CreateOperation(_name); | 534 var operation = new _CreateOperation(_name); |
533 _scheduler.enqueue(operation, handleCreateResult); | 535 _scheduler.enqueue(operation, handleCreateResult); |
534 } | 536 } |
535 | 537 |
536 void createSync() { | 538 void createSync() { |
537 if (_asyncUsed) { | 539 if (_asyncUsed) { |
538 throw new FileIOException( | 540 throw new FileIOException( |
539 "Mixed use of synchronous and asynchronous API"); | 541 "Mixed use of synchronous and asynchronous API"); |
540 } | 542 } |
541 bool created = _FileUtils.checkedCreate(_name); | 543 bool created = _FileUtils.checkedCreate(_name); |
542 if (!created) { | 544 if (!created) { |
543 throw new FileIOException("Cannot create file: $_name"); | 545 throw new FileIOException("Cannot create file: $_name"); |
544 } | 546 } |
545 } | 547 } |
546 | 548 |
547 void delete() { | 549 void delete() { |
548 _asyncUsed = true; | 550 _asyncUsed = true; |
549 var handler = (_deleteHandler != null) ? _deleteHandler : () => null; | |
550 var handleDeleteResult = (created, ignored) { | 551 var handleDeleteResult = (created, ignored) { |
| 552 var handler = (_deleteHandler != null) ? _deleteHandler : () => null; |
551 if (created) { | 553 if (created) { |
552 handler(); | 554 handler(); |
553 } else if (_errorHandler != null) { | 555 } else if (_errorHandler != null) { |
554 _errorHandler("Cannot delete file: $_name"); | 556 _errorHandler("Cannot delete file: $_name"); |
555 } | 557 } |
556 }; | 558 }; |
557 var operation = new _DeleteOperation(_name); | 559 var operation = new _DeleteOperation(_name); |
558 _scheduler.enqueue(operation, handleDeleteResult); | 560 _scheduler.enqueue(operation, handleDeleteResult); |
559 } | 561 } |
560 | 562 |
(...skipping 12 matching lines...) Expand all Loading... |
573 _asyncUsed = true; | 575 _asyncUsed = true; |
574 if (mode != FileMode.READ && | 576 if (mode != FileMode.READ && |
575 mode != FileMode.WRITE && | 577 mode != FileMode.WRITE && |
576 mode != FileMode.APPEND) { | 578 mode != FileMode.APPEND) { |
577 if (_errorHandler != null) { | 579 if (_errorHandler != null) { |
578 _errorHandler("Unknown file mode. Use FileMode.READ, FileMode.WRITE " + | 580 _errorHandler("Unknown file mode. Use FileMode.READ, FileMode.WRITE " + |
579 "or FileMode.APPEND."); | 581 "or FileMode.APPEND."); |
580 return; | 582 return; |
581 } | 583 } |
582 } | 584 } |
583 // If no open handler is present, close the file immediately to | |
584 // avoid leaking an open file descriptor. | |
585 var handler = _openHandler; | |
586 if (handler === null) { | |
587 handler = (file) => file.close(); | |
588 } | |
589 var handleOpenResult = (id, ignored) { | 585 var handleOpenResult = (id, ignored) { |
| 586 // If no open handler is present, close the file immediately to |
| 587 // avoid leaking an open file descriptor. |
| 588 var handler = _openHandler; |
| 589 if (handler === null) { |
| 590 handler = (file) => file.close(); |
| 591 } |
590 if (id != 0) { | 592 if (id != 0) { |
591 var randomAccessFile = new _RandomAccessFile(id, _name); | 593 var randomAccessFile = new _RandomAccessFile(id, _name); |
592 handler(randomAccessFile); | 594 handler(randomAccessFile); |
593 } else if (_errorHandler != null) { | 595 } else if (_errorHandler != null) { |
594 _errorHandler("Cannot open file: $_name"); | 596 _errorHandler("Cannot open file: $_name"); |
595 } | 597 } |
596 }; | 598 }; |
597 var operation = new _OpenOperation(_name, mode._mode); | 599 var operation = new _OpenOperation(_name, mode._mode); |
598 _scheduler.enqueue(operation, handleOpenResult); | 600 _scheduler.enqueue(operation, handleOpenResult); |
599 } | 601 } |
(...skipping 11 matching lines...) Expand all Loading... |
611 } | 613 } |
612 var id = _FileUtils.checkedOpen(_name, mode._mode); | 614 var id = _FileUtils.checkedOpen(_name, mode._mode); |
613 if (id == 0) { | 615 if (id == 0) { |
614 throw new FileIOException("Cannot open file: $_name"); | 616 throw new FileIOException("Cannot open file: $_name"); |
615 } | 617 } |
616 return new _RandomAccessFile(id, _name); | 618 return new _RandomAccessFile(id, _name); |
617 } | 619 } |
618 | 620 |
619 void fullPath() { | 621 void fullPath() { |
620 _asyncUsed = true; | 622 _asyncUsed = true; |
621 var handler = _fullPathHandler; | |
622 if (handler == null) handler = (path) => null; | |
623 var handleFullPathResult = (result, ignored) { | 623 var handleFullPathResult = (result, ignored) { |
| 624 var handler = _fullPathHandler; |
| 625 if (handler == null) handler = (path) => null; |
624 if (result != null) { | 626 if (result != null) { |
625 handler(result); | 627 handler(result); |
626 } else if (_errorHandler != null) { | 628 } else if (_errorHandler != null) { |
627 _errorHandler("fullPath failed"); | 629 _errorHandler("fullPath failed"); |
628 } | 630 } |
629 }; | 631 }; |
630 var operation = new _FullPathOperation(_name); | 632 var operation = new _FullPathOperation(_name); |
631 _scheduler.enqueue(operation, handleFullPathResult); | 633 _scheduler.enqueue(operation, handleFullPathResult); |
632 } | 634 } |
633 | 635 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
687 } | 689 } |
688 | 690 |
689 | 691 |
690 class _RandomAccessFile implements RandomAccessFile { | 692 class _RandomAccessFile implements RandomAccessFile { |
691 _RandomAccessFile(int this._id, String this._name) | 693 _RandomAccessFile(int this._id, String this._name) |
692 : _scheduler = new _FileOperationScheduler(), | 694 : _scheduler = new _FileOperationScheduler(), |
693 _asyncUsed = false; | 695 _asyncUsed = false; |
694 | 696 |
695 void close() { | 697 void close() { |
696 _asyncUsed = true; | 698 _asyncUsed = true; |
697 var handler = (_closeHandler != null) ? _closeHandler : () => null; | |
698 var handleCloseResult = (result, ignored) { | 699 var handleCloseResult = (result, ignored) { |
| 700 var handler = (_closeHandler != null) ? _closeHandler : () => null; |
699 if (result != -1) { | 701 if (result != -1) { |
700 _id = result; | 702 _id = result; |
701 handler(); | 703 handler(); |
702 } else if (_errorHandler != null) { | 704 } else if (_errorHandler != null) { |
703 _errorHandler("Cannot close file: $_name"); | 705 _errorHandler("Cannot close file: $_name"); |
704 } | 706 } |
705 }; | 707 }; |
706 var operation = new _CloseOperation(_id); | 708 var operation = new _CloseOperation(_id); |
707 _scheduler.enqueue(operation, handleCloseResult); | 709 _scheduler.enqueue(operation, handleCloseResult); |
708 } | 710 } |
709 | 711 |
710 void closeSync() { | 712 void closeSync() { |
711 if (_asyncUsed) { | 713 if (_asyncUsed) { |
712 throw new FileIOException( | 714 throw new FileIOException( |
713 "Mixed use of synchronous and asynchronous API"); | 715 "Mixed use of synchronous and asynchronous API"); |
714 } | 716 } |
715 var id = _FileUtils.close(_id); | 717 var id = _FileUtils.close(_id); |
716 if (id == -1) { | 718 if (id == -1) { |
717 throw new FileIOException("Cannot close file: $_name"); | 719 throw new FileIOException("Cannot close file: $_name"); |
718 } | 720 } |
719 _id = id; | 721 _id = id; |
720 } | 722 } |
721 | 723 |
722 void readByte() { | 724 void readByte() { |
723 _asyncUsed = true; | 725 _asyncUsed = true; |
724 var handler = | |
725 (_readByteHandler != null) ? _readByteHandler : (byte) => null; | |
726 var handleReadByteResult = (result, ignored) { | 726 var handleReadByteResult = (result, ignored) { |
| 727 var handler = |
| 728 (_readByteHandler != null) ? _readByteHandler : (byte) => null; |
727 if (result != -1) { | 729 if (result != -1) { |
728 handler(result); | 730 handler(result); |
729 } else if (_errorHandler != null) { | 731 } else if (_errorHandler != null) { |
730 _errorHandler("readByte failed"); | 732 _errorHandler("readByte failed"); |
731 } | 733 } |
732 }; | 734 }; |
733 var operation = new _ReadByteOperation(_id); | 735 var operation = new _ReadByteOperation(_id); |
734 _scheduler.enqueue(operation, handleReadByteResult); | 736 _scheduler.enqueue(operation, handleReadByteResult); |
735 } | 737 } |
736 | 738 |
(...skipping 10 matching lines...) Expand all Loading... |
747 } | 749 } |
748 | 750 |
749 void readList(List<int> buffer, int offset, int bytes) { | 751 void readList(List<int> buffer, int offset, int bytes) { |
750 _asyncUsed = true; | 752 _asyncUsed = true; |
751 if (buffer is !List || offset is !int || bytes is !int) { | 753 if (buffer is !List || offset is !int || bytes is !int) { |
752 if (_errorHandler != null) { | 754 if (_errorHandler != null) { |
753 _errorHandler("Invalid arguments to readList"); | 755 _errorHandler("Invalid arguments to readList"); |
754 } | 756 } |
755 return; | 757 return; |
756 }; | 758 }; |
757 var handler = | |
758 (_readListHandler != null) ? _readListHandler : (result) => null; | |
759 var handleReadListResult = (result, ignored) { | 759 var handleReadListResult = (result, ignored) { |
| 760 var handler = |
| 761 (_readListHandler != null) ? _readListHandler : (result) => null; |
760 if (result is _ReadListResult && result.read != -1) { | 762 if (result is _ReadListResult && result.read != -1) { |
761 var read = result.read; | 763 var read = result.read; |
762 buffer.setRange(offset, read, result.buffer); | 764 buffer.setRange(offset, read, result.buffer); |
763 handler(read); | 765 handler(read); |
764 return; | 766 return; |
765 } | 767 } |
766 if (_errorHandler != null) { | 768 if (_errorHandler != null) { |
767 _errorHandler(result is String ? result : "readList failed"); | 769 _errorHandler(result is String ? result : "readList failed"); |
768 } | 770 } |
769 }; | 771 }; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
902 } | 904 } |
903 int result = _FileUtils.checkedWriteString(_id, string); | 905 int result = _FileUtils.checkedWriteString(_id, string); |
904 if (result == -1) { | 906 if (result == -1) { |
905 throw new FileIOException("writeString failed"); | 907 throw new FileIOException("writeString failed"); |
906 } | 908 } |
907 return result; | 909 return result; |
908 } | 910 } |
909 | 911 |
910 void position() { | 912 void position() { |
911 _asyncUsed = true; | 913 _asyncUsed = true; |
912 var handler = (_positionHandler != null) ? _positionHandler : (pos) => null; | |
913 var handlePositionResult = (result, ignored) { | 914 var handlePositionResult = (result, ignored) { |
| 915 var handler = |
| 916 (_positionHandler != null) ? _positionHandler : (pos) => null; |
914 if (result == -1 && _errorHandler != null) { | 917 if (result == -1 && _errorHandler != null) { |
915 _errorHandler("position failed"); | 918 _errorHandler("position failed"); |
916 return; | 919 return; |
917 } | 920 } |
918 handler(result); | 921 handler(result); |
919 }; | 922 }; |
920 var operation = new _PositionOperation(_id); | 923 var operation = new _PositionOperation(_id); |
921 _scheduler.enqueue(operation, handlePositionResult); | 924 _scheduler.enqueue(operation, handlePositionResult); |
922 } | 925 } |
923 | 926 |
924 int positionSync() { | 927 int positionSync() { |
925 if (_asyncUsed) { | 928 if (_asyncUsed) { |
926 throw new FileIOException( | 929 throw new FileIOException( |
927 "Mixed use of synchronous and asynchronous API"); | 930 "Mixed use of synchronous and asynchronous API"); |
928 } | 931 } |
929 int result = _FileUtils.position(_id); | 932 int result = _FileUtils.position(_id); |
930 if (result == -1) { | 933 if (result == -1) { |
931 throw new FileIOException("position failed"); | 934 throw new FileIOException("position failed"); |
932 } | 935 } |
933 return result; | 936 return result; |
934 } | 937 } |
935 | 938 |
936 void setPosition(int position) { | 939 void setPosition(int position) { |
937 _asyncUsed = true; | 940 _asyncUsed = true; |
938 var handler = | |
939 (_setPositionHandler != null) ? _setPositionHandler : () => null; | |
940 var handleSetPositionResult = (result, ignored) { | 941 var handleSetPositionResult = (result, ignored) { |
| 942 var handler = |
| 943 (_setPositionHandler != null) ? _setPositionHandler : () => null; |
941 if (result == false && _errorHandler != null) { | 944 if (result == false && _errorHandler != null) { |
942 _errorHandler("setPosition failed"); | 945 _errorHandler("setPosition failed"); |
943 return; | 946 return; |
944 } | 947 } |
945 handler(); | 948 handler(); |
946 }; | 949 }; |
947 var operation = new _SetPositionOperation(_id, position); | 950 var operation = new _SetPositionOperation(_id, position); |
948 _scheduler.enqueue(operation, handleSetPositionResult); | 951 _scheduler.enqueue(operation, handleSetPositionResult); |
949 } | 952 } |
950 | 953 |
951 void setPositionSync(int position) { | 954 void setPositionSync(int position) { |
952 if (_asyncUsed) { | 955 if (_asyncUsed) { |
953 throw new FileIOException( | 956 throw new FileIOException( |
954 "Mixed use of synchronous and asynchronous API"); | 957 "Mixed use of synchronous and asynchronous API"); |
955 } | 958 } |
956 bool result = _FileUtils.setPosition(_id, position); | 959 bool result = _FileUtils.setPosition(_id, position); |
957 if (result == false) { | 960 if (result == false) { |
958 throw new FileIOException("setPosition failed"); | 961 throw new FileIOException("setPosition failed"); |
959 } | 962 } |
960 } | 963 } |
961 | 964 |
962 void truncate(int length) { | 965 void truncate(int length) { |
963 _asyncUsed = true; | 966 _asyncUsed = true; |
964 var handler = (_truncateHandler != null) ? _truncateHandler : () => null; | |
965 var handleTruncateResult = (result, ignored) { | 967 var handleTruncateResult = (result, ignored) { |
| 968 var handler = (_truncateHandler != null) ? _truncateHandler : () => null; |
966 if (result == false && _errorHandler != null) { | 969 if (result == false && _errorHandler != null) { |
967 _errorHandler("truncate failed"); | 970 _errorHandler("truncate failed"); |
968 return; | 971 return; |
969 } | 972 } |
970 handler(); | 973 handler(); |
971 }; | 974 }; |
972 var operation = new _TruncateOperation(_id, length); | 975 var operation = new _TruncateOperation(_id, length); |
973 _scheduler.enqueue(operation, handleTruncateResult); | 976 _scheduler.enqueue(operation, handleTruncateResult); |
974 } | 977 } |
975 | 978 |
976 void truncateSync(int length) { | 979 void truncateSync(int length) { |
977 if (_asyncUsed) { | 980 if (_asyncUsed) { |
978 throw new FileIOException( | 981 throw new FileIOException( |
979 "Mixed use of synchronous and asynchronous API"); | 982 "Mixed use of synchronous and asynchronous API"); |
980 } | 983 } |
981 bool result = _FileUtils.truncate(_id, length); | 984 bool result = _FileUtils.truncate(_id, length); |
982 if (result == false) { | 985 if (result == false) { |
983 throw new FileIOException("truncate failed"); | 986 throw new FileIOException("truncate failed"); |
984 } | 987 } |
985 } | 988 } |
986 | 989 |
987 void length() { | 990 void length() { |
988 _asyncUsed = true; | 991 _asyncUsed = true; |
989 var handler = (_lengthHandler != null) ? _lengthHandler : (pos) => null; | |
990 var handleLengthResult = (result, ignored) { | 992 var handleLengthResult = (result, ignored) { |
| 993 var handler = (_lengthHandler != null) ? _lengthHandler : (pos) => null; |
991 if (result == -1 && _errorHandler != null) { | 994 if (result == -1 && _errorHandler != null) { |
992 _errorHandler("length failed"); | 995 _errorHandler("length failed"); |
993 return; | 996 return; |
994 } | 997 } |
995 handler(result); | 998 handler(result); |
996 }; | 999 }; |
997 var operation = new _LengthOperation(_id); | 1000 var operation = new _LengthOperation(_id); |
998 _scheduler.enqueue(operation, handleLengthResult); | 1001 _scheduler.enqueue(operation, handleLengthResult); |
999 } | 1002 } |
1000 | 1003 |
1001 int lengthSync() { | 1004 int lengthSync() { |
1002 if (_asyncUsed) { | 1005 if (_asyncUsed) { |
1003 throw new FileIOException( | 1006 throw new FileIOException( |
1004 "Mixed use of synchronous and asynchronous API"); | 1007 "Mixed use of synchronous and asynchronous API"); |
1005 } | 1008 } |
1006 int result = _FileUtils.length(_id); | 1009 int result = _FileUtils.length(_id); |
1007 if (result == -1) { | 1010 if (result == -1) { |
1008 throw new FileIOException("length failed"); | 1011 throw new FileIOException("length failed"); |
1009 } | 1012 } |
1010 return result; | 1013 return result; |
1011 } | 1014 } |
1012 | 1015 |
1013 void flush() { | 1016 void flush() { |
1014 _asyncUsed = true; | 1017 _asyncUsed = true; |
1015 var handler = (_flushHandler != null) ? _flushHandler : (pos) => null; | |
1016 var handleFlushResult = (result, ignored) { | 1018 var handleFlushResult = (result, ignored) { |
| 1019 var handler = (_flushHandler != null) ? _flushHandler : (pos) => null; |
1017 if (result == -1 && _errorHandler != null) { | 1020 if (result == -1 && _errorHandler != null) { |
1018 _errorHandler("flush failed"); | 1021 _errorHandler("flush failed"); |
1019 return; | 1022 return; |
1020 } | 1023 } |
1021 handler(); | 1024 handler(); |
1022 }; | 1025 }; |
1023 var operation = new _FlushOperation(_id); | 1026 var operation = new _FlushOperation(_id); |
1024 _scheduler.enqueue(operation, handleFlushResult); | 1027 _scheduler.enqueue(operation, handleFlushResult); |
1025 } | 1028 } |
1026 | 1029 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1087 var _readByteHandler; | 1090 var _readByteHandler; |
1088 var _readListHandler; | 1091 var _readListHandler; |
1089 var _noPendingWriteHandler; | 1092 var _noPendingWriteHandler; |
1090 var _positionHandler; | 1093 var _positionHandler; |
1091 var _setPositionHandler; | 1094 var _setPositionHandler; |
1092 var _truncateHandler; | 1095 var _truncateHandler; |
1093 var _lengthHandler; | 1096 var _lengthHandler; |
1094 var _flushHandler; | 1097 var _flushHandler; |
1095 var _errorHandler; | 1098 var _errorHandler; |
1096 } | 1099 } |
OLD | NEW |