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 * A combined byte and text output. | 8 * A combined byte and text output. |
9 * | 9 * |
10 * An [IOSink] combines a [StreamSink] of bytes with a [StringSink], | 10 * An [IOSink] combines a [StreamSink] of bytes with a [StringSink], |
11 * and allows easy output of both bytes and text. | 11 * and allows easy output of both bytes and text. |
12 * | 12 * |
13 * Writing text ([write]) and adding bytes ([add]) may be interleaved freely. | 13 * Writing text ([write]) and adding bytes ([add]) may be interleaved freely. |
14 * | 14 * |
15 * While a stream is being added using [addStream], any further attempts | 15 * While a stream is being added using [addStream], any further attempts |
16 * to add or write to the [IOSink] will fail until the [addStream] completes. | 16 * to add or write to the [IOSink] will fail until the [addStream] completes. |
17 * | 17 * |
18 * If data is added to the [IOSink] after the sink is closed, the data will be | 18 * It is an error to add data to the [IOSink] after the sink is closed. |
19 * ignored. Use the [done] future to be notified when the [IOSink] is closed. | |
20 */ | 19 */ |
21 abstract class IOSink implements StreamSink<List<int>>, StringSink { | 20 abstract class IOSink implements StreamSink<List<int>>, StringSink { |
22 /** | 21 /** |
23 * Create an [IOSink] that outputs to a [target] [StreamConsumer] of bytes. | 22 * Create an [IOSink] that outputs to a [target] [StreamConsumer] of bytes. |
24 * | 23 * |
25 * Text written to [StreamSink] methods is encoded to bytes using [encoding] | 24 * Text written to [StreamSink] methods is encoded to bytes using [encoding] |
26 * before being output on [target]. | 25 * before being output on [target]. |
27 */ | 26 */ |
28 factory IOSink(StreamConsumer<List<int>> target, {Encoding encoding: UTF8}) => | 27 factory IOSink(StreamConsumer<List<int>> target, {Encoding encoding: UTF8}) => |
29 new _IOSinkImpl(target, encoding); | 28 new _IOSinkImpl(target, encoding); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 final Completer _doneCompleter = new Completer(); | 140 final Completer _doneCompleter = new Completer(); |
142 StreamController<T> _controllerInstance; | 141 StreamController<T> _controllerInstance; |
143 Completer _controllerCompleter; | 142 Completer _controllerCompleter; |
144 bool _isClosed = false; | 143 bool _isClosed = false; |
145 bool _isBound = false; | 144 bool _isBound = false; |
146 bool _hasError = false; | 145 bool _hasError = false; |
147 | 146 |
148 _StreamSinkImpl(this._target); | 147 _StreamSinkImpl(this._target); |
149 | 148 |
150 void add(T data) { | 149 void add(T data) { |
151 if (_isClosed) return; | 150 if (_isClosed) { |
151 // TODO(29554): throw a StateError, and don't just report the problem. | |
152 stderr.writeln("StreamSink is closed and adding to it is an error."); | |
153 stderr.writeln(" See http://dartbug.com/29554."); | |
154 stderr.writeln(StackTrace.current); | |
Lasse Reichstein Nielsen
2017/05/08 11:31:08
I don't know enough about stderr to be sure this d
floitsch
2017/05/08 12:22:39
It completely interferes with the user's stderr.
H
| |
155 return; | |
156 } | |
152 _controller.add(data); | 157 _controller.add(data); |
153 } | 158 } |
154 | 159 |
155 void addError(error, [StackTrace stackTrace]) { | 160 void addError(error, [StackTrace stackTrace]) { |
161 if (_isClosed) { | |
162 // TODO(29554): throw a StateError, and don't just report the problem. | |
163 stderr.writeln("StreamSink is closed and adding to it is an error."); | |
164 stderr.writeln(" See http://dartbug.com/29554."); | |
165 stderr.writeln(StackTrace.current); | |
166 return; | |
167 } | |
156 _controller.addError(error, stackTrace); | 168 _controller.addError(error, stackTrace); |
157 } | 169 } |
158 | 170 |
159 Future addStream(Stream<T> stream) { | 171 Future addStream(Stream<T> stream) { |
160 if (_isBound) { | 172 if (_isBound) { |
161 throw new StateError("StreamSink is already bound to a stream"); | 173 throw new StateError("StreamSink is already bound to a stream"); |
162 } | 174 } |
163 _isBound = true; | 175 _isBound = true; |
164 if (_hasError) return done; | 176 if (_hasError) return done; |
165 // Wait for any sync operations to complete. | 177 // Wait for any sync operations to complete. |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 | 312 |
301 void writeln([Object object = ""]) { | 313 void writeln([Object object = ""]) { |
302 write(object); | 314 write(object); |
303 write("\n"); | 315 write("\n"); |
304 } | 316 } |
305 | 317 |
306 void writeCharCode(int charCode) { | 318 void writeCharCode(int charCode) { |
307 write(new String.fromCharCode(charCode)); | 319 write(new String.fromCharCode(charCode)); |
308 } | 320 } |
309 } | 321 } |
OLD | NEW |