OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 /** | 7 /** |
8 * Helper class to wrap a [StreamConsumer<List<int>>] and provide | 8 * Helper class to wrap a [StreamConsumer<List<int>>] and provide |
9 * utility functions for writing to the StreamConsumer directly. The | 9 * utility functions for writing to the StreamConsumer directly. The |
10 * [IOSink] buffers the input given by all [StringSink] methods and will delay | 10 * [IOSink] buffers the input given by all [StringSink] methods and will delay |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 void addError(AsyncError error) { | 127 void addError(AsyncError error) { |
128 _controller.addError(error); | 128 _controller.addError(error); |
129 } | 129 } |
130 | 130 |
131 Future addStream(Stream<List<int>> stream) { | 131 Future addStream(Stream<List<int>> stream) { |
132 if (_isBound) { | 132 if (_isBound) { |
133 throw new StateError("IOSink is already bound to a stream"); | 133 throw new StateError("IOSink is already bound to a stream"); |
134 } | 134 } |
135 _isBound = true; | 135 _isBound = true; |
136 // Wait for any sync operations to complete. | 136 // Wait for any sync operations to complete. |
137 return _closeController().then((_) { | 137 Future targetAddStream() { |
138 return _target.addStream(stream) | 138 return _target.addStream(stream) |
139 .whenComplete(() { | 139 .whenComplete(() { |
140 _isBound = false; | 140 _isBound = false; |
141 }); | 141 }); |
142 }); | 142 } |
143 } | 143 if (_controllerInstance == null) return targetAddStream(); |
144 | |
145 Future _closeController() { | |
146 if (_controllerInstance == null) return new Future.immediate(null); | |
147 var future = _controllerCompleter.future; | 144 var future = _controllerCompleter.future; |
148 _controllerInstance.close(); | 145 _controllerInstance.close(); |
149 return future; | 146 return future.then((_) => targetAddStream()); |
150 } | 147 } |
151 | 148 |
152 Future close() { | 149 Future close() { |
153 if (_isBound) { | 150 if (_isBound) { |
154 throw new StateError("IOSink is bound to a stream"); | 151 throw new StateError("IOSink is bound to a stream"); |
155 } | 152 } |
156 if (!_isClosed) { | 153 if (!_isClosed) { |
157 _isClosed = true; | 154 _isClosed = true; |
158 if (_controllerInstance != null) { | 155 if (_controllerInstance != null) { |
159 _controllerInstance.close(); | 156 _controllerInstance.close(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } else { | 214 } else { |
218 // No new stream. No need to close target, as it have already | 215 // No new stream. No need to close target, as it have already |
219 // failed. | 216 // failed. |
220 _completeDone(error: error); | 217 _completeDone(error: error); |
221 } | 218 } |
222 }); | 219 }); |
223 } | 220 } |
224 return _controllerInstance; | 221 return _controllerInstance; |
225 } | 222 } |
226 } | 223 } |
OLD | NEW |