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 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 644 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 [FileMode mode = FileMode.WRITE]) { | 655 [FileMode mode = FileMode.WRITE]) { |
656 Completer<File> completer = new Completer<File>(); | 656 Completer<File> completer = new Completer<File>(); |
657 try { | 657 try { |
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.completeException(e); | 665 completer.completeError(e); |
666 }; | 666 }; |
667 } catch (e) { | 667 } catch (e) { |
668 new Timer(0, (t) => completer.completeException(e)); | 668 new Timer(0, (t) => 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.completeException(e)); | 688 new Timer(0, (t) => 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 new Timer(0, (t) { |
791 completer.completeException(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; |
801 return _fileService.call(request).then((response) { | 801 return _fileService.call(request).then((response) { |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 } |
OLD | NEW |