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

Side by Side Diff: sdk/lib/io/io_sink.dart

Issue 2872603002: Revert "Warn when adding something to a closed sink and improve documentation." (Closed)
Patch Set: Created 3 years, 7 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 unified diff | Download patch
« no previous file with comments | « sdk/lib/core/sink.dart ('k') | sdk/lib/io/secure_socket.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 * It is an error to add data to the [IOSink] after the sink is closed. 18 * If data is added to the [IOSink] after the sink is closed, the data will be
19 * ignored. Use the [done] future to be notified when the [IOSink] is closed.
19 */ 20 */
20 abstract class IOSink implements StreamSink<List<int>>, StringSink { 21 abstract class IOSink implements StreamSink<List<int>>, StringSink {
21 /** 22 /**
22 * Create an [IOSink] that outputs to a [target] [StreamConsumer] of bytes. 23 * Create an [IOSink] that outputs to a [target] [StreamConsumer] of bytes.
23 * 24 *
24 * Text written to [StreamSink] methods is encoded to bytes using [encoding] 25 * Text written to [StreamSink] methods is encoded to bytes using [encoding]
25 * before being output on [target]. 26 * before being output on [target].
26 */ 27 */
27 factory IOSink(StreamConsumer<List<int>> target, {Encoding encoding: UTF8}) => 28 factory IOSink(StreamConsumer<List<int>> target, {Encoding encoding: UTF8}) =>
28 new _IOSinkImpl(target, encoding); 29 new _IOSinkImpl(target, encoding);
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 final StreamConsumer<T> _target; 140 final StreamConsumer<T> _target;
140 final Completer _doneCompleter = new Completer(); 141 final Completer _doneCompleter = new Completer();
141 StreamController<T> _controllerInstance; 142 StreamController<T> _controllerInstance;
142 Completer _controllerCompleter; 143 Completer _controllerCompleter;
143 bool _isClosed = false; 144 bool _isClosed = false;
144 bool _isBound = false; 145 bool _isBound = false;
145 bool _hasError = false; 146 bool _hasError = false;
146 147
147 _StreamSinkImpl(this._target); 148 _StreamSinkImpl(this._target);
148 149
149 void _reportClosedSink() {
150 // TODO(29554): this is very brittle and depends on the layout of the
151 // stderr class.
152 if (this == stderr._sink) {
153 // We can't report on stderr anymore (as we would otherwise
154 // have an infinite recursion.
155 throw new StateError("Stderr is closed.");
156 }
157 // TODO(29554): throw a StateError, and don't just report the problem.
158 stderr.writeln("StreamSink is closed and adding to it is an error.");
159 stderr.writeln(" See http://dartbug.com/29554.");
160 stderr.writeln(StackTrace.current);
161 }
162
163 void add(T data) { 150 void add(T data) {
164 if (_isClosed) { 151 if (_isClosed) return;
165 _reportClosedSink();
166 return;
167 }
168 _controller.add(data); 152 _controller.add(data);
169 } 153 }
170 154
171 void addError(error, [StackTrace stackTrace]) { 155 void addError(error, [StackTrace stackTrace]) {
172 if (_isClosed) {
173 _reportClosedSink();
174 return;
175 }
176 _controller.addError(error, stackTrace); 156 _controller.addError(error, stackTrace);
177 } 157 }
178 158
179 Future addStream(Stream<T> stream) { 159 Future addStream(Stream<T> stream) {
180 if (_isBound) { 160 if (_isBound) {
181 throw new StateError("StreamSink is already bound to a stream"); 161 throw new StateError("StreamSink is already bound to a stream");
182 } 162 }
183 _isBound = true; 163 _isBound = true;
184 if (_hasError) return done; 164 if (_hasError) return done;
185 // Wait for any sync operations to complete. 165 // Wait for any sync operations to complete.
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 300
321 void writeln([Object object = ""]) { 301 void writeln([Object object = ""]) {
322 write(object); 302 write(object);
323 write("\n"); 303 write("\n");
324 } 304 }
325 305
326 void writeCharCode(int charCode) { 306 void writeCharCode(int charCode) {
327 write(new String.fromCharCode(charCode)); 307 write(new String.fromCharCode(charCode));
328 } 308 }
329 } 309 }
OLDNEW
« no previous file with comments | « sdk/lib/core/sink.dart ('k') | sdk/lib/io/secure_socket.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698