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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/io/file_impl.dart
diff --git a/sdk/lib/io/file_impl.dart b/sdk/lib/io/file_impl.dart
index 8e826aaeba28de179dab043c92098386f880821f..26e3f74665cdd9d49305fb188a9fd2b6696c5bba 100644
--- a/sdk/lib/io/file_impl.dart
+++ b/sdk/lib/io/file_impl.dart
@@ -660,6 +660,54 @@ class _File extends _FileBase implements File {
return _getDecodedLines(decoder);
}
+ Future<File> writeAsBytes(List<int> bytes,
+ [FileMode mode = FileMode.WRITE]) {
+ Completer<File> completer = new Completer<File>();
+ try {
+ var stream = openOutputStream(mode);
+ stream.write(bytes);
Søren Gjesse 2012/11/20 13:23:34 You could just do stream.close() right after strea
+ stream.onNoPendingWrites = () {
+ stream.close();
+ stream.onClosed = () {
+ completer.complete(this);
+ };
+ };
+ stream.onError = (e) {
+ completer.completeException(e);
+ };
+ } catch (e) {
+ new Timer(0, (t) => completer.completeException(e));
+ return completer.future;
+ }
+ return completer.future;
+ }
+
+ void writeAsBytesSync(List<int> bytes, [FileMode mode = FileMode.WRITE]) {
+ RandomAccessFile opened = openSync(mode);
+ opened.writeListSync(bytes, 0, bytes.length);
+ opened.closeSync();
+ }
+
+ Future<File> writeAsString(String contents,
+ [FileMode mode = FileMode.WRITE,
+ Encoding encoding = Encoding.UTF_8]) {
+ try {
+ var data = _StringEncoders.encoder(encoding).encodeString(contents);
+ return writeAsBytes(data, mode);
+ } catch (e) {
+ var completer = new Completer();
+ new Timer(0, (t) => completer.completeException(e));
+ return completer.future;
+ }
+ }
+
+ void writeAsStringSync(String contents,
+ [FileMode mode = FileMode.WRITE,
+ Encoding encoding = Encoding.UTF_8]) {
+ var data = _StringEncoders.encoder(encoding).encodeString(contents);
+ writeAsBytesSync(data, mode);
+ }
+
String get name => _name;
void _ensureFileService() {

Powered by Google App Engine
This is Rietveld 408576698