OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library utils; | 5 library utils; |
6 | 6 |
7 import 'dart:crypto'; | 7 import 'dart:crypto'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 import 'dart:scalarlist'; | 10 import 'dart:scalarlist'; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 137 |
138 var completer = new Completer<List<int>>(); | 138 var completer = new Completer<List<int>>(); |
139 /// TODO(nweiz): use BufferList when issue 6409 is fixed | 139 /// TODO(nweiz): use BufferList when issue 6409 is fixed |
140 var buffer = <int>[]; | 140 var buffer = <int>[]; |
141 stream.onClosed = () => completer.complete(buffer); | 141 stream.onClosed = () => completer.complete(buffer); |
142 stream.onData = () => buffer.addAll(stream.read()); | 142 stream.onData = () => buffer.addAll(stream.read()); |
143 stream.onError = completer.completeException; | 143 stream.onError = completer.completeException; |
144 return completer.future; | 144 return completer.future; |
145 } | 145 } |
146 | 146 |
| 147 // TODO(nweiz): this wouldn't be necessary were it not for issue 7013. |
| 148 /// Wrap an InputStream in a ListInputStream. This ensures that if the input |
| 149 /// stream has invalid onClosed/onError behavior (see issue 7013), that behavior |
| 150 /// is papered over. |
| 151 InputStream wrapInputStream(InputStream source) { |
| 152 var sink = new ListInputStream(); |
| 153 // TODO(nweiz): Due to issuee 3657, pipeInputToInput naturally avoids calling |
| 154 // both onClosed and onError. If 3657 gets fixed before 7013, we'll need to do |
| 155 // that explicitly. |
| 156 pipeInputToInput(source, sink); |
| 157 return sink; |
| 158 } |
| 159 |
147 /// Takes all input from [source] and writes it to [sink], then closes [sink]. | 160 /// Takes all input from [source] and writes it to [sink], then closes [sink]. |
| 161 /// Returns a [Future] that completes when [source] is exhausted. |
148 void pipeInputToInput(InputStream source, ListInputStream sink) { | 162 void pipeInputToInput(InputStream source, ListInputStream sink) { |
149 source.onClosed = () => sink.markEndOfStream(); | 163 source.onClosed = sink.markEndOfStream; |
150 source.onData = () => sink.write(source.read()); | 164 source.onData = () => sink.write(source.read()); |
151 // TODO(nweiz): propagate source errors to the sink. See issue 3657. | 165 // TODO(nweiz): propagate source errors to the sink. See issue 3657. |
| 166 // TODO(nweiz): we need to use async here to avoid issue 4974. |
| 167 source.onError = (e) => async.then((_) { |
| 168 throw e; |
| 169 }); |
152 } | 170 } |
153 | 171 |
154 /// Takes all input from [source] and writes it to [sink], but does not close | 172 /// Takes all input from [source] and writes it to [sink], but does not close |
155 /// [sink] when [source] is closed. Returns a [Future] that completes when | 173 /// [sink] when [source] is closed. Returns a [Future] that completes when |
156 /// [source] is closed. | 174 /// [source] is closed. |
157 Future writeInputToInput(InputStream source, ListInputStream sink) { | 175 Future writeInputToInput(InputStream source, ListInputStream sink) { |
158 var completer = new Completer(); | 176 var completer = new Completer(); |
159 source.onClosed = () => completer.complete(null); | 177 source.onClosed = () => completer.complete(null); |
160 source.onData = () => sink.write(source.read()); | 178 source.onData = () => sink.write(source.read()); |
161 // TODO(nweiz): propagate source errors to the sink. See issue 3657. | 179 // TODO(nweiz): propagate source errors to the sink. See issue 3657. |
(...skipping 16 matching lines...) Expand all Loading... |
178 /// The return values of all [Future]s are discarded. Any errors will cause the | 196 /// The return values of all [Future]s are discarded. Any errors will cause the |
179 /// iteration to stop and will be piped through the return value. | 197 /// iteration to stop and will be piped through the return value. |
180 Future forEachFuture(Iterable input, Future fn(element)) { | 198 Future forEachFuture(Iterable input, Future fn(element)) { |
181 var iterator = input.iterator(); | 199 var iterator = input.iterator(); |
182 Future nextElement(_) { | 200 Future nextElement(_) { |
183 if (!iterator.hasNext) return new Future.immediate(null); | 201 if (!iterator.hasNext) return new Future.immediate(null); |
184 return fn(iterator.next()).chain(nextElement); | 202 return fn(iterator.next()).chain(nextElement); |
185 } | 203 } |
186 return nextElement(null); | 204 return nextElement(null); |
187 } | 205 } |
OLD | NEW |