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 |
11 * a [addStream] until the buffer is flushed. | 11 * a [addStream] until the buffer is flushed. |
12 * | 12 * |
13 * When the [IOSink] is bound to a stream (through [addStream]) any call | 13 * When the [IOSink] is bound to a stream (through [addStream]) any call |
14 * to the [IOSink] will throw a [StateError]. When the [addStream] compeltes, | 14 * to the [IOSink] will throw a [StateError]. When the [addStream] completes, |
15 * the [IOSink] will again be open for all calls. | 15 * the [IOSink] will again be open for all calls. |
| 16 * |
| 17 * If data is added to the [IOSink] after the sink is closed, the data will be |
| 18 * ignored. Use the [done] future to be notified when the [IOSink] is closed. |
16 */ | 19 */ |
17 abstract class IOSink implements StreamSink<List<int>>, StringSink { | 20 abstract class IOSink implements StreamSink<List<int>>, StringSink { |
18 factory IOSink(StreamConsumer<List<int>> target, | 21 factory IOSink(StreamConsumer<List<int>> target, |
19 {Encoding encoding: Encoding.UTF_8}) | 22 {Encoding encoding: Encoding.UTF_8}) |
20 => new _IOSinkImpl(target, encoding); | 23 => new _IOSinkImpl(target, encoding); |
21 | 24 |
22 /** | 25 /** |
23 * The [Encoding] used when writing strings. Depending on the | 26 * The [Encoding] used when writing strings. Depending on the |
24 * underlying consumer this property might be mutable. | 27 * underlying consumer this property might be mutable. |
25 */ | 28 */ |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 Completer _controllerCompleter; | 65 Completer _controllerCompleter; |
63 bool _isClosed = false; | 66 bool _isClosed = false; |
64 bool _isBound = false; | 67 bool _isBound = false; |
65 bool _hasError = false; | 68 bool _hasError = false; |
66 | 69 |
67 _StreamSinkImpl(StreamConsumer<T> this._target) { | 70 _StreamSinkImpl(StreamConsumer<T> this._target) { |
68 _doneFuture = _doneCompleter.future; | 71 _doneFuture = _doneCompleter.future; |
69 } | 72 } |
70 | 73 |
71 void add(T data) { | 74 void add(T data) { |
| 75 if (_isClosed) return; |
72 _controller.add(data); | 76 _controller.add(data); |
73 } | 77 } |
74 | 78 |
75 void addError(error) { | 79 void addError(error) { |
76 _controller.addError(error); | 80 _controller.addError(error); |
77 } | 81 } |
78 | 82 |
79 Future addStream(Stream<T> stream) { | 83 Future addStream(Stream<T> stream) { |
80 if (_isBound) { | 84 if (_isBound) { |
81 throw new StateError("StreamSink is already bound to a stream"); | 85 throw new StateError("StreamSink is already bound to a stream"); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 | 225 |
222 void writeln([Object obj = ""]) { | 226 void writeln([Object obj = ""]) { |
223 write(obj); | 227 write(obj); |
224 write("\n"); | 228 write("\n"); |
225 } | 229 } |
226 | 230 |
227 void writeCharCode(int charCode) { | 231 void writeCharCode(int charCode) { |
228 write(new String.fromCharCode(charCode)); | 232 write(new String.fromCharCode(charCode)); |
229 } | 233 } |
230 } | 234 } |
OLD | NEW |