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 * an [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 // TODO(ajohnsen): Make _encodingMutable an argument. |
21 factory IOSink(StreamConsumer<List<int>> target, | 22 factory IOSink(StreamConsumer<List<int>> target, |
22 {Encoding encoding: UTF8}) | 23 {Encoding encoding: UTF8}) |
23 => new _IOSinkImpl(target, encoding); | 24 => new _IOSinkImpl(target, encoding); |
24 | 25 |
25 /** | 26 /** |
26 * The [Encoding] used when writing strings. Depending on the | 27 * The [Encoding] used when writing strings. Depending on the |
27 * underlying consumer this property might be mutable. | 28 * underlying consumer this property might be mutable. |
28 */ | 29 */ |
29 Encoding encoding; | 30 Encoding encoding; |
30 | 31 |
31 /** | 32 /** |
32 * Writes the bytes uninterpreted to the consumer. While the call is | 33 * Adds [data] to the target consumer, ignoring [encoding]. |
33 * synchronous, the data may be buffered until the underlying resource is | 34 * |
34 * ready. The data should not be modified after a call to [add]. | 35 * The [encoding] does not apply to this method, and the `data` list is passed |
| 36 * directly to the target consumer as a stream event. |
| 37 * |
| 38 * This function must not be called when a stream is currently being added |
| 39 * using [addStream]. |
| 40 * |
| 41 * This operation is non-blocking. See [flush] or [done] for how to get any |
| 42 * errors generated by this call. |
| 43 * |
| 44 * The data list should not be modified after it has been passed to `add`. |
35 */ | 45 */ |
36 void add(List<int> data); | 46 void add(List<int> data); |
37 | 47 |
38 /** | 48 /** |
39 * Writes an error to the consumer. | 49 * Converts [obj] to a String by invoking [Object.toString] and |
| 50 * [add]s the encoding of the result to the target consumer. |
| 51 * |
| 52 * This operation is non-blocking. See [flush] or [done] for how to get any |
| 53 * errors generated by this call. |
| 54 */ |
| 55 void write(Object obj); |
| 56 |
| 57 /** |
| 58 * Iterates over the given [objects] and [write]s them in sequence. |
| 59 * |
| 60 * If [separator] is provided, a `write` with the `separator` is performed |
| 61 * between any two elements of `objects`. |
| 62 * |
| 63 * This operation is non-blocking. See [flush] or [done] for how to get any |
| 64 * errors generated by this call. |
| 65 */ |
| 66 void writeAll(Iterable objects, [String separator = ""]); |
| 67 |
| 68 /** |
| 69 * Converts [obj] to a String by invoking [Object.toString] and |
| 70 * writes the result to `this`, followed by a newline. |
| 71 * |
| 72 * This operation is non-blocking. See [flush] or [done] for how to get any |
| 73 * errors generated by this call. |
| 74 */ |
| 75 void writeln([Object obj = ""]); |
| 76 |
| 77 /** |
| 78 * Writes the [charCode] to `this`. |
| 79 * |
| 80 * This method is equivalent to `write(new String.fromCharCode(charCode))`. |
| 81 * |
| 82 * This operation is non-blocking. See [flush] or [done] for how to get any |
| 83 * errors generated by this call. |
| 84 */ |
| 85 void writeCharCode(int charCode); |
| 86 |
| 87 /** |
| 88 * Passes the error to the target consumer as an error event. |
| 89 * |
| 90 * This function must not be called when a stream is currently being added |
| 91 * using [addStream]. |
| 92 * |
| 93 * This operation is non-blocking. See [flush] or [done] for how to get any |
| 94 * errors generated by this call. |
40 */ | 95 */ |
41 void addError(error, [StackTrace stackTrace]); | 96 void addError(error, [StackTrace stackTrace]); |
42 | 97 |
43 /** | 98 /** |
44 * Adds all elements of the given [stream] to `this`. | 99 * Adds all elements of the given [stream] to `this`. |
45 */ | 100 */ |
46 Future addStream(Stream<List<int>> stream); | 101 Future addStream(Stream<List<int>> stream); |
47 | 102 |
48 /** | 103 /** |
49 * Returns a [Future] that completes once all buffered data is accepted by the | 104 * Returns a [Future] that completes once all buffered data is accepted by the |
50 * to underlying [StreamConsumer]. | 105 * to underlying [StreamConsumer]. |
51 * | 106 * |
52 * It's an error to call this method, while an [addStream] is incomplete. | 107 * It's an error to call this method, while an [addStream] is incomplete. |
53 * | 108 * |
54 * NOTE: This is not necessarily the same as the data being flushed by the | 109 * NOTE: This is not necessarily the same as the data being flushed by the |
55 * operating system. | 110 * operating system. |
56 */ | 111 */ |
57 Future flush(); | 112 Future flush(); |
58 | 113 |
59 /** | 114 /** |
60 * Close the target. | 115 * Close the target consumer. |
61 */ | 116 */ |
62 Future close(); | 117 Future close(); |
63 | 118 |
64 /** | 119 /** |
65 * Get a future that will complete when all synchronous have completed, or an | 120 * Get a future that will complete when the consumer closes, or when an |
66 * error happened. This future is identical to the future returned from close. | 121 * error occurs. This future is identical to the future returned by |
| 122 * [close]. |
67 */ | 123 */ |
68 Future get done; | 124 Future get done; |
69 } | 125 } |
70 | 126 |
71 class _StreamSinkImpl<T> implements StreamSink<T> { | 127 class _StreamSinkImpl<T> implements StreamSink<T> { |
72 final StreamConsumer<T> _target; | 128 final StreamConsumer<T> _target; |
73 Completer _doneCompleter = new Completer(); | 129 Completer _doneCompleter = new Completer(); |
74 Future _doneFuture; | 130 Future _doneFuture; |
75 StreamController<T> _controllerInstance; | 131 StreamController<T> _controllerInstance; |
76 Completer _controllerCompleter; | 132 Completer _controllerCompleter; |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 306 |
251 void writeln([Object obj = ""]) { | 307 void writeln([Object obj = ""]) { |
252 write(obj); | 308 write(obj); |
253 write("\n"); | 309 write("\n"); |
254 } | 310 } |
255 | 311 |
256 void writeCharCode(int charCode) { | 312 void writeCharCode(int charCode) { |
257 write(new String.fromCharCode(charCode)); | 313 write(new String.fromCharCode(charCode)); |
258 } | 314 } |
259 } | 315 } |
OLD | NEW |