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

Unified Diff: sdk/lib/io/file_impl.dart

Issue 219243008: Clean up file writing. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | 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 f9eef32f004ab2da85675aeadcdd09085ba99fa4..c21a204e816d5e43e4b6a2044e2b22b9c37c64d4 100644
--- a/sdk/lib/io/file_impl.dart
+++ b/sdk/lib/io/file_impl.dart
@@ -175,16 +175,16 @@ class _FileStream extends Stream<List<int>> {
class _FileStreamConsumer extends StreamConsumer<List<int>> {
File _file;
Future<RandomAccessFile> _openFuture;
- StreamSubscription _subscription;
_FileStreamConsumer(File this._file, FileMode mode) {
_openFuture = _file.open(mode: mode);
}
Future<File> addStream(Stream<List<int>> stream) {
- Completer<File> completer = new Completer<File>();
+ Completer<File> completer = new Completer<File>.sync();
_openFuture
.then((openedFile) {
+ var _subscription;
void error(e, [StackTrace stackTrace]) {
_subscription.cancel();
openedFile.close();
@@ -530,18 +530,14 @@ class _File extends FileSystemEntity implements File {
Future<File> writeAsBytes(List<int> bytes,
{FileMode mode: FileMode.WRITE,
bool flush: false}) {
- try {
- IOSink sink = openWrite(mode: mode);
- sink.add(bytes);
- if (flush) {
- sink.flush().then((_) => sink.close());
- } else {
- sink.close();
- }
- return sink.done.then((_) => this);
- } catch (e) {
- return new Future.error(e);
- }
+ return open(mode: mode).then((file) {
+ return file.writeFrom(bytes, 0, bytes.length)
+ .then((_) {
+ if (flush) return file.flush().then((_) => this);
+ return this;
+ })
+ .whenComplete(file.close);
+ });
}
void writeAsBytesSync(List<int> bytes,
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698