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

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

Issue 26968003: Remove DirectoryException and LinkException from dart:io and use FileException instaed. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Merge with master. Created 7 years, 2 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/file.dart ('k') | sdk/lib/io/file_system_entity.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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 // Read the file in blocks of size 64k. 7 // Read the file in blocks of size 64k.
8 const int _BLOCK_SIZE = 64 * 1024; 8 const int _BLOCK_SIZE = 64 * 1024;
9 9
10 10
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 throwIfError(ms, "Cannot retrieve modification time", path); 362 throwIfError(ms, "Cannot retrieve modification time", path);
363 return new DateTime.fromMillisecondsSinceEpoch(ms); 363 return new DateTime.fromMillisecondsSinceEpoch(ms);
364 } 364 }
365 365
366 external static _open(String path, int mode); 366 external static _open(String path, int mode);
367 367
368 RandomAccessFile openSync({FileMode mode: FileMode.READ}) { 368 RandomAccessFile openSync({FileMode mode: FileMode.READ}) {
369 if (mode != FileMode.READ && 369 if (mode != FileMode.READ &&
370 mode != FileMode.WRITE && 370 mode != FileMode.WRITE &&
371 mode != FileMode.APPEND) { 371 mode != FileMode.APPEND) {
372 throw new FileException("Unknown file mode. Use FileMode.READ, " 372 throw new FileSystemException("Unknown file mode. Use FileMode.READ, "
373 "FileMode.WRITE or FileMode.APPEND.", 373 "FileMode.WRITE or FileMode.APPEND.",
374 path); 374 path);
375 } 375 }
376 var id = _open(path, mode._mode); 376 var id = _open(path, mode._mode);
377 throwIfError(id, "Cannot open file", path); 377 throwIfError(id, "Cannot open file", path);
378 return new _RandomAccessFile(id, path); 378 return new _RandomAccessFile(id, path);
379 } 379 }
380 380
381 external static int _openStdio(int fd); 381 external static int _openStdio(int fd);
382 382
383 static RandomAccessFile _openStdioSync(int fd) { 383 static RandomAccessFile _openStdioSync(int fd) {
384 var id = _openStdio(fd); 384 var id = _openStdio(fd);
385 if (id == 0) { 385 if (id == 0) {
386 throw new FileException("Cannot open stdio file for: $fd"); 386 throw new FileSystemException("Cannot open stdio file for: $fd");
387 } 387 }
388 return new _RandomAccessFile(id, ""); 388 return new _RandomAccessFile(id, "");
389 } 389 }
390 390
391 Future<String> fullPath() => resolveSymbolicLinks(); 391 Future<String> fullPath() => resolveSymbolicLinks();
392 392
393 String fullPathSync() => resolveSymbolicLinksSync(); 393 String fullPathSync() => resolveSymbolicLinksSync();
394 394
395 Stream<List<int>> openRead([int start, int end]) { 395 Stream<List<int>> openRead([int start, int end]) {
396 return new _FileStream(path, start, end); 396 return new _FileStream(path, start, end);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 builder.add(data); 430 builder.add(data);
431 } 431 }
432 opened.closeSync(); 432 opened.closeSync();
433 return builder.takeBytes(); 433 return builder.takeBytes();
434 } 434 }
435 435
436 String _tryDecode(List<int> bytes, Encoding encoding) { 436 String _tryDecode(List<int> bytes, Encoding encoding) {
437 try { 437 try {
438 return encoding.decode(bytes); 438 return encoding.decode(bytes);
439 } catch (_) { 439 } catch (_) {
440 throw new FileException( 440 throw new FileSystemException(
441 "Failed to decode data using encoding '${encoding.name}'", path); 441 "Failed to decode data using encoding '${encoding.name}'", path);
442 } 442 }
443 } 443 }
444 444
445 Future<String> readAsString({Encoding encoding: UTF8}) { 445 Future<String> readAsString({Encoding encoding: UTF8}) {
446 return readAsBytes().then((bytes) { 446 return readAsBytes().then((bytes) {
447 return _tryDecode(bytes, encoding); 447 return _tryDecode(bytes, encoding);
448 }); 448 });
449 } 449 }
450 450
451 String readAsStringSync({Encoding encoding: UTF8}) { 451 String readAsStringSync({Encoding encoding: UTF8}) {
452 List<int> bytes = readAsBytesSync(); 452 List<int> bytes = readAsBytesSync();
453 return _tryDecode(bytes, encoding); 453 return _tryDecode(bytes, encoding);
454 } 454 }
455 455
456 List<String> _decodeLines(List<int> bytes, Encoding encoding) { 456 List<String> _decodeLines(List<int> bytes, Encoding encoding) {
457 if (bytes.length == 0) return []; 457 if (bytes.length == 0) return [];
458 var list = []; 458 var list = [];
459 var controller = new StreamController(sync: true); 459 var controller = new StreamController(sync: true);
460 var error = null; 460 var error = null;
461 controller.stream 461 controller.stream
462 .transform(encoding.decoder) 462 .transform(encoding.decoder)
463 .transform(new LineSplitter()) 463 .transform(new LineSplitter())
464 .listen((line) => list.add(line), onError: (e) => error = e); 464 .listen((line) => list.add(line), onError: (e) => error = e);
465 controller.add(bytes); 465 controller.add(bytes);
466 controller.close(); 466 controller.close();
467 if (error != null) { 467 if (error != null) {
468 throw new FileException( 468 throw new FileSystemException(
469 "Failed to decode data using encoding '${encoding.name}'", path); 469 "Failed to decode data using encoding '${encoding.name}'", path);
470 } 470 }
471 return list; 471 return list;
472 } 472 }
473 473
474 Future<List<String>> readAsLines({Encoding encoding: UTF8}) { 474 Future<List<String>> readAsLines({Encoding encoding: UTF8}) {
475 return readAsBytes().then((bytes) { 475 return readAsBytes().then((bytes) {
476 return _decodeLines(bytes, encoding); 476 return _decodeLines(bytes, encoding);
477 }); 477 });
478 } 478 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 void writeAsStringSync(String contents, 512 void writeAsStringSync(String contents,
513 {FileMode mode: FileMode.WRITE, 513 {FileMode mode: FileMode.WRITE,
514 Encoding encoding: UTF8}) { 514 Encoding encoding: UTF8}) {
515 writeAsBytesSync(encoding.encode(contents), mode: mode); 515 writeAsBytesSync(encoding.encode(contents), mode: mode);
516 } 516 }
517 517
518 String toString() => "File: '$path'"; 518 String toString() => "File: '$path'";
519 519
520 static throwIfError(Object result, String msg, String path) { 520 static throwIfError(Object result, String msg, String path) {
521 if (result is OSError) { 521 if (result is OSError) {
522 throw new FileException(msg, path, result); 522 throw new FileSystemException(msg, path, result);
523 } 523 }
524 } 524 }
525 } 525 }
526 526
527 527
528 class _RandomAccessFile implements RandomAccessFile { 528 class _RandomAccessFile implements RandomAccessFile {
529 final String path; 529 final String path;
530 int _id; 530 int _id;
531 bool _asyncDispatched = false; 531 bool _asyncDispatched = false;
532 SendPort _fileService; 532 SendPort _fileService;
533 533
534 _RandomAccessFile(int this._id, String this.path); 534 _RandomAccessFile(int this._id, String this.path);
535 535
536 Future<RandomAccessFile> close() { 536 Future<RandomAccessFile> close() {
537 return _dispatch(_FILE_CLOSE, [_id], markClosed: true).then((result) { 537 return _dispatch(_FILE_CLOSE, [_id], markClosed: true).then((result) {
538 if (result != -1) { 538 if (result != -1) {
539 _id = result; 539 _id = result;
540 return this; 540 return this;
541 } else { 541 } else {
542 throw new FileException("Cannot close file", path); 542 throw new FileSystemException("Cannot close file", path);
543 } 543 }
544 }); 544 });
545 } 545 }
546 546
547 external static int _close(int id); 547 external static int _close(int id);
548 548
549 void closeSync() { 549 void closeSync() {
550 _checkAvailable(); 550 _checkAvailable();
551 var id = _close(_id); 551 var id = _close(_id);
552 if (id == -1) { 552 if (id == -1) {
553 throw new FileException("Cannot close file", path); 553 throw new FileSystemException("Cannot close file", path);
554 } 554 }
555 _id = id; 555 _id = id;
556 } 556 }
557 557
558 Future<int> readByte() { 558 Future<int> readByte() {
559 return _dispatch(_FILE_READ_BYTE, [_id]).then((response) { 559 return _dispatch(_FILE_READ_BYTE, [_id]).then((response) {
560 if (_isErrorResponse(response)) { 560 if (_isErrorResponse(response)) {
561 throw _exceptionFromResponse(response, "readByte failed", path); 561 throw _exceptionFromResponse(response, "readByte failed", path);
562 } 562 }
563 return response; 563 return response;
564 }); 564 });
565 } 565 }
566 566
567 external static _readByte(int id); 567 external static _readByte(int id);
568 568
569 int readByteSync() { 569 int readByteSync() {
570 _checkAvailable(); 570 _checkAvailable();
571 var result = _readByte(_id); 571 var result = _readByte(_id);
572 if (result is OSError) { 572 if (result is OSError) {
573 throw new FileException("readByte failed", path, result); 573 throw new FileSystemException("readByte failed", path, result);
574 } 574 }
575 return result; 575 return result;
576 } 576 }
577 577
578 Future<List<int>> read(int bytes) { 578 Future<List<int>> read(int bytes) {
579 if (bytes is !int) { 579 if (bytes is !int) {
580 throw new ArgumentError(bytes); 580 throw new ArgumentError(bytes);
581 } 581 }
582 return _dispatch(_FILE_READ, [_id, bytes]).then((response) { 582 return _dispatch(_FILE_READ, [_id, bytes]).then((response) {
583 if (_isErrorResponse(response)) { 583 if (_isErrorResponse(response)) {
584 throw _exceptionFromResponse(response, "read failed", path); 584 throw _exceptionFromResponse(response, "read failed", path);
585 } 585 }
586 return response[1]; 586 return response[1];
587 }); 587 });
588 } 588 }
589 589
590 external static _read(int id, int bytes); 590 external static _read(int id, int bytes);
591 591
592 List<int> readSync(int bytes) { 592 List<int> readSync(int bytes) {
593 _checkAvailable(); 593 _checkAvailable();
594 if (bytes is !int) { 594 if (bytes is !int) {
595 throw new ArgumentError(bytes); 595 throw new ArgumentError(bytes);
596 } 596 }
597 var result = _read(_id, bytes); 597 var result = _read(_id, bytes);
598 if (result is OSError) { 598 if (result is OSError) {
599 throw new FileException("readSync failed", path, result); 599 throw new FileSystemException("readSync failed", path, result);
600 } 600 }
601 return result; 601 return result;
602 } 602 }
603 603
604 Future<int> readInto(List<int> buffer, [int start, int end]) { 604 Future<int> readInto(List<int> buffer, [int start, int end]) {
605 if (buffer is !List || 605 if (buffer is !List ||
606 (start != null && start is !int) || 606 (start != null && start is !int) ||
607 (end != null && end is !int)) { 607 (end != null && end is !int)) {
608 throw new ArgumentError(); 608 throw new ArgumentError();
609 } 609 }
(...skipping 27 matching lines...) Expand all
637 (start != null && start is !int) || 637 (start != null && start is !int) ||
638 (end != null && end is !int)) { 638 (end != null && end is !int)) {
639 throw new ArgumentError(); 639 throw new ArgumentError();
640 } 640 }
641 if (start == null) start = 0; 641 if (start == null) start = 0;
642 if (end == null) end = buffer.length; 642 if (end == null) end = buffer.length;
643 if (end == start) return 0; 643 if (end == start) return 0;
644 _checkReadWriteListArguments(buffer.length, start, end); 644 _checkReadWriteListArguments(buffer.length, start, end);
645 var result = _readInto(_id, buffer, start, end); 645 var result = _readInto(_id, buffer, start, end);
646 if (result is OSError) { 646 if (result is OSError) {
647 throw new FileException("readInto failed", path, result); 647 throw new FileSystemException("readInto failed", path, result);
648 } 648 }
649 return result; 649 return result;
650 } 650 }
651 651
652 Future<RandomAccessFile> writeByte(int value) { 652 Future<RandomAccessFile> writeByte(int value) {
653 if (value is !int) { 653 if (value is !int) {
654 throw new ArgumentError(value); 654 throw new ArgumentError(value);
655 } 655 }
656 return _dispatch(_FILE_WRITE_BYTE, [_id, value]).then((response) { 656 return _dispatch(_FILE_WRITE_BYTE, [_id, value]).then((response) {
657 if (_isErrorResponse(response)) { 657 if (_isErrorResponse(response)) {
658 throw _exceptionFromResponse(response, "writeByte failed", path); 658 throw _exceptionFromResponse(response, "writeByte failed", path);
659 } 659 }
660 return this; 660 return this;
661 }); 661 });
662 } 662 }
663 663
664 external static _writeByte(int id, int value); 664 external static _writeByte(int id, int value);
665 665
666 int writeByteSync(int value) { 666 int writeByteSync(int value) {
667 _checkAvailable(); 667 _checkAvailable();
668 if (value is !int) { 668 if (value is !int) {
669 throw new ArgumentError(value); 669 throw new ArgumentError(value);
670 } 670 }
671 var result = _writeByte(_id, value); 671 var result = _writeByte(_id, value);
672 if (result is OSError) { 672 if (result is OSError) {
673 throw new FileException("writeByte failed", path, result); 673 throw new FileSystemException("writeByte failed", path, result);
674 } 674 }
675 return result; 675 return result;
676 } 676 }
677 677
678 Future<RandomAccessFile> writeFrom(List<int> buffer, [int start, int end]) { 678 Future<RandomAccessFile> writeFrom(List<int> buffer, [int start, int end]) {
679 if ((buffer is !List && buffer is !ByteData) || 679 if ((buffer is !List && buffer is !ByteData) ||
680 (start != null && start is !int) || 680 (start != null && start is !int) ||
681 (end != null && end is !int)) { 681 (end != null && end is !int)) {
682 throw new ArgumentError("Invalid arguments to writeFrom"); 682 throw new ArgumentError("Invalid arguments to writeFrom");
683 } 683 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 if (end == null) end = buffer.length; 715 if (end == null) end = buffer.length;
716 if (end == start) return; 716 if (end == start) return;
717 _checkReadWriteListArguments(buffer.length, start, end); 717 _checkReadWriteListArguments(buffer.length, start, end);
718 _BufferAndStart bufferAndStart = 718 _BufferAndStart bufferAndStart =
719 _ensureFastAndSerializableByteData(buffer, start, end); 719 _ensureFastAndSerializableByteData(buffer, start, end);
720 var result = _writeFrom(_id, 720 var result = _writeFrom(_id,
721 bufferAndStart.buffer, 721 bufferAndStart.buffer,
722 bufferAndStart.start, 722 bufferAndStart.start,
723 end - (start - bufferAndStart.start)); 723 end - (start - bufferAndStart.start));
724 if (result is OSError) { 724 if (result is OSError) {
725 throw new FileException("writeFrom failed", path, result); 725 throw new FileSystemException("writeFrom failed", path, result);
726 } 726 }
727 } 727 }
728 728
729 Future<RandomAccessFile> writeString(String string, 729 Future<RandomAccessFile> writeString(String string,
730 {Encoding encoding: UTF8}) { 730 {Encoding encoding: UTF8}) {
731 if (encoding is! Encoding) { 731 if (encoding is! Encoding) {
732 throw new ArgumentError(encoding); 732 throw new ArgumentError(encoding);
733 } 733 }
734 var data = encoding.encode(string); 734 var data = encoding.encode(string);
735 return writeFrom(data, 0, data.length); 735 return writeFrom(data, 0, data.length);
(...skipping 15 matching lines...) Expand all
751 return response; 751 return response;
752 }); 752 });
753 } 753 }
754 754
755 external static _position(int id); 755 external static _position(int id);
756 756
757 int positionSync() { 757 int positionSync() {
758 _checkAvailable(); 758 _checkAvailable();
759 var result = _position(_id); 759 var result = _position(_id);
760 if (result is OSError) { 760 if (result is OSError) {
761 throw new FileException("position failed", path, result); 761 throw new FileSystemException("position failed", path, result);
762 } 762 }
763 return result; 763 return result;
764 } 764 }
765 765
766 Future<RandomAccessFile> setPosition(int position) { 766 Future<RandomAccessFile> setPosition(int position) {
767 return _dispatch(_FILE_SET_POSITION, [_id, position]) 767 return _dispatch(_FILE_SET_POSITION, [_id, position])
768 .then((response) { 768 .then((response) {
769 if (_isErrorResponse(response)) { 769 if (_isErrorResponse(response)) {
770 throw _exceptionFromResponse(response, "setPosition failed", path); 770 throw _exceptionFromResponse(response, "setPosition failed", path);
771 } 771 }
772 return this; 772 return this;
773 }); 773 });
774 } 774 }
775 775
776 external static _setPosition(int id, int position); 776 external static _setPosition(int id, int position);
777 777
778 void setPositionSync(int position) { 778 void setPositionSync(int position) {
779 _checkAvailable(); 779 _checkAvailable();
780 var result = _setPosition(_id, position); 780 var result = _setPosition(_id, position);
781 if (result is OSError) { 781 if (result is OSError) {
782 throw new FileException("setPosition failed", path, result); 782 throw new FileSystemException("setPosition failed", path, result);
783 } 783 }
784 } 784 }
785 785
786 Future<RandomAccessFile> truncate(int length) { 786 Future<RandomAccessFile> truncate(int length) {
787 return _dispatch(_FILE_TRUNCATE, [_id, length]).then((response) { 787 return _dispatch(_FILE_TRUNCATE, [_id, length]).then((response) {
788 if (_isErrorResponse(response)) { 788 if (_isErrorResponse(response)) {
789 throw _exceptionFromResponse(response, "truncate failed", path); 789 throw _exceptionFromResponse(response, "truncate failed", path);
790 } 790 }
791 return this; 791 return this;
792 }); 792 });
793 } 793 }
794 794
795 external static _truncate(int id, int length); 795 external static _truncate(int id, int length);
796 796
797 void truncateSync(int length) { 797 void truncateSync(int length) {
798 _checkAvailable(); 798 _checkAvailable();
799 var result = _truncate(_id, length); 799 var result = _truncate(_id, length);
800 if (result is OSError) { 800 if (result is OSError) {
801 throw new FileException("truncate failed", path, result); 801 throw new FileSystemException("truncate failed", path, result);
802 } 802 }
803 } 803 }
804 804
805 Future<int> length() { 805 Future<int> length() {
806 return _dispatch(_FILE_LENGTH, [_id]).then((response) { 806 return _dispatch(_FILE_LENGTH, [_id]).then((response) {
807 if (_isErrorResponse(response)) { 807 if (_isErrorResponse(response)) {
808 throw _exceptionFromResponse(response, "length failed", path); 808 throw _exceptionFromResponse(response, "length failed", path);
809 } 809 }
810 return response; 810 return response;
811 }); 811 });
812 } 812 }
813 813
814 external static _length(int id); 814 external static _length(int id);
815 815
816 int lengthSync() { 816 int lengthSync() {
817 _checkAvailable(); 817 _checkAvailable();
818 var result = _length(_id); 818 var result = _length(_id);
819 if (result is OSError) { 819 if (result is OSError) {
820 throw new FileException("length failed", path, result); 820 throw new FileSystemException("length failed", path, result);
821 } 821 }
822 return result; 822 return result;
823 } 823 }
824 824
825 Future<RandomAccessFile> flush() { 825 Future<RandomAccessFile> flush() {
826 return _dispatch(_FILE_FLUSH, [_id]).then((response) { 826 return _dispatch(_FILE_FLUSH, [_id]).then((response) {
827 if (_isErrorResponse(response)) { 827 if (_isErrorResponse(response)) {
828 throw _exceptionFromResponse(response, 828 throw _exceptionFromResponse(response,
829 "flush failed", 829 "flush failed",
830 path); 830 path);
831 } 831 }
832 return this; 832 return this;
833 }); 833 });
834 } 834 }
835 835
836 external static _flush(int id); 836 external static _flush(int id);
837 837
838 void flushSync() { 838 void flushSync() {
839 _checkAvailable(); 839 _checkAvailable();
840 var result = _flush(_id); 840 var result = _flush(_id);
841 if (result is OSError) { 841 if (result is OSError) {
842 throw new FileException("flush failed", path, result); 842 throw new FileSystemException("flush failed", path, result);
843 } 843 }
844 } 844 }
845 845
846 bool get closed => _id == 0; 846 bool get closed => _id == 0;
847 847
848 Future _dispatch(int request, List data, { bool markClosed: false }) { 848 Future _dispatch(int request, List data, { bool markClosed: false }) {
849 if (closed) { 849 if (closed) {
850 return new Future.error(new FileException("File closed", path)); 850 return new Future.error(new FileSystemException("File closed", path));
851 } 851 }
852 if (_asyncDispatched) { 852 if (_asyncDispatched) {
853 var msg = "An async operation is currently pending"; 853 var msg = "An async operation is currently pending";
854 return new Future.error(new FileException(msg, path)); 854 return new Future.error(new FileSystemException(msg, path));
855 } 855 }
856 if (markClosed) { 856 if (markClosed) {
857 // Set the id_ to 0 (NULL) to ensure the no more async requests 857 // Set the id_ to 0 (NULL) to ensure the no more async requests
858 // can be issued for this file. 858 // can be issued for this file.
859 _id = 0; 859 _id = 0;
860 } 860 }
861 _asyncDispatched = true; 861 _asyncDispatched = true;
862 return _IOService.dispatch(request, data) 862 return _IOService.dispatch(request, data)
863 .whenComplete(() { 863 .whenComplete(() {
864 _asyncDispatched = false; 864 _asyncDispatched = false;
865 }); 865 });
866 } 866 }
867 867
868 void _checkAvailable() { 868 void _checkAvailable() {
869 if (_asyncDispatched) { 869 if (_asyncDispatched) {
870 throw new FileException("An async operation is currently pending", path); 870 throw new FileSystemException("An async operation is currently pending", p ath);
871 } 871 }
872 if (closed) { 872 if (closed) {
873 throw new FileException("File closed", path); 873 throw new FileSystemException("File closed", path);
874 } 874 }
875 } 875 }
876 } 876 }
OLDNEW
« no previous file with comments | « sdk/lib/io/file.dart ('k') | sdk/lib/io/file_system_entity.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698