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] completes, | 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 * | 16 * |
17 * If data is added to the [IOSink] after the sink is closed, the data will be | 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. | 18 * ignored. Use the [done] future to be notified when the [IOSink] is closed. |
19 */ | 19 */ |
20 abstract class IOSink implements StreamSink<List<int>>, StringSink { | 20 abstract class IOSink implements StreamSink<List<int>>, StringSink { |
21 factory IOSink(StreamConsumer<List<int>> target, | 21 factory IOSink(StreamConsumer<List<int>> target, |
22 {Encoding encoding: Encoding.UTF_8}) | 22 {Encoding encoding: UTF8}) |
23 => new _IOSinkImpl(target, encoding); | 23 => new _IOSinkImpl(target, encoding); |
24 | 24 |
25 /** | 25 /** |
26 * The [Encoding] used when writing strings. Depending on the | 26 * The [Encoding] used when writing strings. Depending on the |
27 * underlying consumer this property might be mutable. | 27 * underlying consumer this property might be mutable. |
28 */ | 28 */ |
29 Encoding encoding; | 29 Encoding encoding; |
30 | 30 |
31 /** | 31 /** |
32 * Writes the bytes uninterpreted to the consumer. While the call is | 32 * Writes the bytes uninterpreted to the consumer. While the call is |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 String string; | 197 String string; |
198 if (obj is String) { | 198 if (obj is String) { |
199 string = obj; | 199 string = obj; |
200 } else { | 200 } else { |
201 string = obj.toString(); | 201 string = obj.toString(); |
202 if (string is! String) { | 202 if (string is! String) { |
203 throw new ArgumentError('toString() did not return a string'); | 203 throw new ArgumentError('toString() did not return a string'); |
204 } | 204 } |
205 } | 205 } |
206 if (string.isEmpty) return; | 206 if (string.isEmpty) return; |
207 add(_encodeString(string, _encoding)); | 207 add(_encoding.encode(string)); |
208 } | 208 } |
209 | 209 |
210 void writeAll(Iterable objects, [String separator = ""]) { | 210 void writeAll(Iterable objects, [String separator = ""]) { |
211 Iterator iterator = objects.iterator; | 211 Iterator iterator = objects.iterator; |
212 if (!iterator.moveNext()) return; | 212 if (!iterator.moveNext()) return; |
213 if (separator.isEmpty) { | 213 if (separator.isEmpty) { |
214 do { | 214 do { |
215 write(iterator.current); | 215 write(iterator.current); |
216 } while (iterator.moveNext()); | 216 } while (iterator.moveNext()); |
217 } else { | 217 } else { |
218 write(iterator.current); | 218 write(iterator.current); |
219 while (iterator.moveNext()) { | 219 while (iterator.moveNext()) { |
220 write(separator); | 220 write(separator); |
221 write(iterator.current); | 221 write(iterator.current); |
222 } | 222 } |
223 } | 223 } |
224 } | 224 } |
225 | 225 |
226 void writeln([Object obj = ""]) { | 226 void writeln([Object obj = ""]) { |
227 write(obj); | 227 write(obj); |
228 write("\n"); | 228 write("\n"); |
229 } | 229 } |
230 | 230 |
231 void writeCharCode(int charCode) { | 231 void writeCharCode(int charCode) { |
232 write(new String.fromCharCode(charCode)); | 232 write(new String.fromCharCode(charCode)); |
233 } | 233 } |
234 } | 234 } |
OLD | NEW |