Index: sdk/lib/io/io_sink.dart |
diff --git a/sdk/lib/io/io_sink.dart b/sdk/lib/io/io_sink.dart |
index e2ea2bd63abb9bc065d8108862febc832f6c074a..74d12b475c032497e22378729bb08c3def0919a5 100644 |
--- a/sdk/lib/io/io_sink.dart |
+++ b/sdk/lib/io/io_sink.dart |
@@ -110,10 +110,18 @@ class _StreamSinkImpl<T> implements StreamSink<T> { |
} |
Future flush() { |
+ if (_isBound) { |
+ throw new StateError("StreamSink is bound to a stream"); |
+ } |
+ if (_controllerInstance == null) return new Future.value(this); |
// Adding an empty stream-controller will return a future that will complete |
// when all data is done. |
- var controller = new StreamController()..close(); |
- return addStream(controller.stream).then((_) => this); |
+ _isBound = true; |
+ var future = _controllerCompleter.future; |
+ _controllerInstance.close(); |
+ return future.whenComplete(() { |
+ _isBound = false; |
+ }); |
} |
Future close() { |
@@ -165,7 +173,7 @@ class _StreamSinkImpl<T> implements StreamSink<T> { |
(_) { |
if (_isBound) { |
// A new stream takes over - forward values to that stream. |
- _controllerCompleter.complete(); |
+ _controllerCompleter.complete(this); |
_controllerCompleter = null; |
_controllerInstance = null; |
} else { |
@@ -221,7 +229,7 @@ class _IOSinkImpl extends _StreamSinkImpl<List<int>> implements IOSink { |
} |
} |
if (string.isEmpty) return; |
- add(_encoding.encode(string)); |
+ add(encoding.encode(string)); |
} |
void writeAll(Iterable objects, [String separator = ""]) { |