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

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

Issue 11316102: Add utility methods to write the contents of a file as one operation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month 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(String name) 6 _FileInputStream(String name)
7 : _data = const [], 7 : _data = const [],
8 _position = 0, 8 _position = 0,
9 _filePosition = 0 { 9 _filePosition = 0 {
10 var file = new File(name); 10 var file = new File(name);
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 }); 653 });
654 } 654 }
655 655
656 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) { 656 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) {
657 var decoder = _StringDecoders.decoder(encoding); 657 var decoder = _StringDecoders.decoder(encoding);
658 List<int> bytes = readAsBytesSync(); 658 List<int> bytes = readAsBytesSync();
659 decoder.write(bytes); 659 decoder.write(bytes);
660 return _getDecodedLines(decoder); 660 return _getDecodedLines(decoder);
661 } 661 }
662 662
663 Future<File> writeAsBytes(List<int> bytes,
664 [FileMode mode = FileMode.WRITE]) {
665 Completer<File> completer = new Completer<File>();
666 try {
667 var stream = openOutputStream(mode);
668 stream.write(bytes);
Søren Gjesse 2012/11/20 13:23:34 You could just do stream.close() right after strea
669 stream.onNoPendingWrites = () {
670 stream.close();
671 stream.onClosed = () {
672 completer.complete(this);
673 };
674 };
675 stream.onError = (e) {
676 completer.completeException(e);
677 };
678 } catch (e) {
679 new Timer(0, (t) => completer.completeException(e));
680 return completer.future;
681 }
682 return completer.future;
683 }
684
685 void writeAsBytesSync(List<int> bytes, [FileMode mode = FileMode.WRITE]) {
686 RandomAccessFile opened = openSync(mode);
687 opened.writeListSync(bytes, 0, bytes.length);
688 opened.closeSync();
689 }
690
691 Future<File> writeAsString(String contents,
692 [FileMode mode = FileMode.WRITE,
693 Encoding encoding = Encoding.UTF_8]) {
694 try {
695 var data = _StringEncoders.encoder(encoding).encodeString(contents);
696 return writeAsBytes(data, mode);
697 } catch (e) {
698 var completer = new Completer();
699 new Timer(0, (t) => completer.completeException(e));
700 return completer.future;
701 }
702 }
703
704 void writeAsStringSync(String contents,
705 [FileMode mode = FileMode.WRITE,
706 Encoding encoding = Encoding.UTF_8]) {
707 var data = _StringEncoders.encoder(encoding).encodeString(contents);
708 writeAsBytesSync(data, mode);
709 }
710
663 String get name => _name; 711 String get name => _name;
664 712
665 void _ensureFileService() { 713 void _ensureFileService() {
666 if (_fileService == null) { 714 if (_fileService == null) {
667 _fileService = _FileUtils._newServicePort(); 715 _fileService = _FileUtils._newServicePort();
668 } 716 }
669 } 717 }
670 718
671 static throwIfError(Object result, String msg) { 719 static throwIfError(Object result, String msg) {
672 if (result is OSError) { 720 if (result is OSError) {
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 new FileIOException("File closed '$_name'")); 1128 new FileIOException("File closed '$_name'"));
1081 }); 1129 });
1082 return completer.future; 1130 return completer.future;
1083 } 1131 }
1084 1132
1085 final String _name; 1133 final String _name;
1086 int _id; 1134 int _id;
1087 1135
1088 SendPort _fileService; 1136 SendPort _fileService;
1089 } 1137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698