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>, T>] and provide | 8 * Helper class to wrap a [StreamConsumer<List<int>, T>] 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 [write], [writeAll], [writeln], | 10 * [IOSink] buffers the input given by [write], [writeAll], [writeln], |
11 * [writeCharCode] and [writeBytes] and will delay a [consume] or | 11 * [writeCharCode] and [add] and will delay a [consume] or |
12 * [writeStream] until the buffer is flushed. | 12 * [writeStream] until the buffer is flushed. |
13 * | 13 * |
14 * When the [IOSink] is bound to a stream (through either [consume] | 14 * When the [IOSink] is bound to a stream (through either [consume] |
15 * or [writeStream]) any call to the [IOSink] will throw a | 15 * or [writeStream]) any call to the [IOSink] will throw a |
16 * [StateError]. | 16 * [StateError]. |
17 */ | 17 */ |
18 abstract class IOSink<T> implements StreamConsumer<List<int>, T>, StringSink { | 18 abstract class IOSink<T> |
| 19 implements StreamConsumer<List<int>, T>, StringSink, EventSink<List<int>> { |
19 factory IOSink(StreamConsumer<List<int>, T> target, | 20 factory IOSink(StreamConsumer<List<int>, T> target, |
20 {Encoding encoding: Encoding.UTF_8}) | 21 {Encoding encoding: Encoding.UTF_8}) |
21 => new _IOSinkImpl(target, encoding); | 22 => new _IOSinkImpl(target, encoding); |
22 | 23 |
23 /** | 24 /** |
24 * The [Encoding] used when writing strings. Depending on the | 25 * The [Encoding] used when writing strings. Depending on the |
25 * underlying consumer this property might be mutable. | 26 * underlying consumer this property might be mutable. |
26 */ | 27 */ |
27 Encoding encoding; | 28 Encoding encoding; |
28 | 29 |
29 /** | 30 /** |
30 * Writes the bytes uninterpreted to the consumer. | 31 * Writes the bytes uninterpreted to the consumer. |
31 */ | 32 */ |
32 void writeBytes(List<int> data); | 33 void add(List<int> data); |
| 34 |
| 35 /** |
| 36 * Writes an error to the consumer. |
| 37 */ |
| 38 void addError(AsyncError error); |
33 | 39 |
34 /** | 40 /** |
35 * Provide functionality for piping to the [IOSink]. | 41 * Provide functionality for piping to the [IOSink]. |
36 */ | 42 */ |
37 Future<T> consume(Stream<List<int>> stream); | 43 Future<T> consume(Stream<List<int>> stream); |
38 | 44 |
39 /** | 45 /** |
40 * Adds all elements of the given [stream] to `this`. | 46 * Adds all elements of the given [stream] to `this`. |
41 */ | 47 */ |
42 Future<T> addStream(Stream<List<int>> stream); | 48 Future<T> addStream(Stream<List<int>> stream); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 String string; | 99 String string; |
94 if (obj is String) { | 100 if (obj is String) { |
95 string = obj; | 101 string = obj; |
96 } else { | 102 } else { |
97 string = obj.toString(); | 103 string = obj.toString(); |
98 if (string is! String) { | 104 if (string is! String) { |
99 throw new ArgumentError('toString() did not return a string'); | 105 throw new ArgumentError('toString() did not return a string'); |
100 } | 106 } |
101 } | 107 } |
102 if (string.isEmpty) return; | 108 if (string.isEmpty) return; |
103 writeBytes(_encodeString(string, _encoding)); | 109 add(_encodeString(string, _encoding)); |
104 } | 110 } |
105 | 111 |
106 void writeAll(Iterable objects, [String separator = ""]) { | 112 void writeAll(Iterable objects, [String separator = ""]) { |
107 Iterator iterator = objects.iterator; | 113 Iterator iterator = objects.iterator; |
108 if (!iterator.moveNext()) return; | 114 if (!iterator.moveNext()) return; |
109 if (separator.isEmpty) { | 115 if (separator.isEmpty) { |
110 do { | 116 do { |
111 write(iterator.current); | 117 write(iterator.current); |
112 } while (iterator.moveNext()); | 118 } while (iterator.moveNext()); |
113 } else { | 119 } else { |
114 write(iterator.current); | 120 write(iterator.current); |
115 while (iterator.moveNext()) { | 121 while (iterator.moveNext()) { |
116 write(separator); | 122 write(separator); |
117 write(iterator.current); | 123 write(iterator.current); |
118 } | 124 } |
119 } | 125 } |
120 } | 126 } |
121 | 127 |
122 void writeln([Object obj = ""]) { | 128 void writeln([Object obj = ""]) { |
123 write(obj); | 129 write(obj); |
124 write("\n"); | 130 write("\n"); |
125 } | 131 } |
126 | 132 |
127 void writeCharCode(int charCode) { | 133 void writeCharCode(int charCode) { |
128 write(new String.fromCharCode(charCode)); | 134 write(new String.fromCharCode(charCode)); |
129 } | 135 } |
130 | 136 |
131 void writeBytes(List<int> data) { | 137 void add(List<int> data) { |
132 if (_isBound) { | 138 if (_isBound) { |
133 throw new StateError("IOSink is already bound to a stream"); | 139 throw new StateError("IOSink is already bound to a stream"); |
134 } | 140 } |
135 _controller.add(data); | 141 _controller.add(data); |
136 } | 142 } |
137 | 143 |
| 144 void addError(AsyncError error) { |
| 145 if (_isBound) { |
| 146 throw new StateError("IOSink is already bound to a stream"); |
| 147 } |
| 148 _controller.addError(error); |
| 149 } |
| 150 |
138 Future<T> consume(Stream<List<int>> stream) { | 151 Future<T> consume(Stream<List<int>> stream) { |
139 if (_isBound) { | 152 if (_isBound) { |
140 throw new StateError("IOSink is already bound to a stream"); | 153 throw new StateError("IOSink is already bound to a stream"); |
141 } | 154 } |
142 return _fillFromStream(stream); | 155 return _fillFromStream(stream); |
143 } | 156 } |
144 | 157 |
145 Future<T> writeStream(Stream<List<int>> stream) { | 158 Future<T> writeStream(Stream<List<int>> stream) { |
146 return addStream(stream); | 159 return addStream(stream); |
147 } | 160 } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 }, | 264 }, |
252 onError: _controller.addError); | 265 onError: _controller.addError); |
253 if (_paused) _pause(); | 266 if (_paused) _pause(); |
254 if (unbind) { | 267 if (unbind) { |
255 return _writeStreamCompleter.future; | 268 return _writeStreamCompleter.future; |
256 } else { | 269 } else { |
257 return _pipeFuture; | 270 return _pipeFuture; |
258 } | 271 } |
259 } | 272 } |
260 } | 273 } |
OLD | NEW |