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

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: Address comments. 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
« no previous file with comments | « sdk/lib/io/file.dart ('k') | tests/standalone/io/file_non_ascii_sync_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..17dc2ab4241b2de96402f46e86e98b27761519b6 100644
--- a/sdk/lib/io/file_impl.dart
+++ b/sdk/lib/io/file_impl.dart
@@ -660,6 +660,52 @@ 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);
+ 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() {
« no previous file with comments | « sdk/lib/io/file.dart ('k') | tests/standalone/io/file_non_ascii_sync_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698