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 /** | 5 /** |
6 * Helper functionality to make working with IO easier. | 6 * Helper functionality to make working with IO easier. |
7 */ | 7 */ |
8 #library('io'); | 8 #library('io'); |
9 | 9 |
10 #import('dart:io'); | 10 #import('dart:io'); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 return opened.writeString(contents).chain((ignore) { | 121 return opened.writeString(contents).chain((ignore) { |
122 return opened.close().transform((ignore) => file); | 122 return opened.close().transform((ignore) => file); |
123 }); | 123 }); |
124 }); | 124 }); |
125 } | 125 } |
126 | 126 |
127 /** | 127 /** |
128 * Asynchronously deletes [file], which can be a [String] or a [File]. Returns a | 128 * Asynchronously deletes [file], which can be a [String] or a [File]. Returns a |
129 * [Future] that completes when the deletion is done. | 129 * [Future] that completes when the deletion is done. |
130 */ | 130 */ |
131 Future<Directory> deleteFile(file) { | 131 Future<File> deleteFile(file) { |
132 return new File(_getPath(file)).delete(); | 132 return new File(_getPath(file)).delete(); |
133 } | 133 } |
134 | 134 |
135 /** | 135 /** |
136 * Creates a directory [dir]. Returns a [Future] that completes when the | 136 * Creates a directory [dir]. Returns a [Future] that completes when the |
137 * directory is created. | 137 * directory is created. |
138 */ | 138 */ |
139 Future<Directory> createDir(dir) { | 139 Future<Directory> createDir(dir) { |
140 dir = _getDirectory(dir); | 140 dir = _getDirectory(dir); |
141 return dir.create(); | 141 return dir.create(); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 "HTTP request for $uri failed with status ${response.statusCode}"); | 303 "HTTP request for $uri failed with status ${response.statusCode}"); |
304 } | 304 } |
305 | 305 |
306 pipeInputToInput(response.inputStream, resultStream, client.shutdown); | 306 pipeInputToInput(response.inputStream, resultStream, client.shutdown); |
307 }; | 307 }; |
308 | 308 |
309 return resultStream; | 309 return resultStream; |
310 } | 310 } |
311 | 311 |
312 /** | 312 /** |
| 313 * Opens an input stream for a HTTP GET request to [uri], which may be a |
| 314 * [String] or [Uri]. Completes with the result of the request as a String. |
| 315 */ |
| 316 Future<String> httpGetString(uri) { |
| 317 // TODO(rnystrom): This code is very similar to httpGet() and |
| 318 // consumeInputStream() (but this one propagates errors). Merge them when |
| 319 // #3657 is fixed. |
| 320 uri = _getUri(uri); |
| 321 |
| 322 var completer = new Completer<String>(); |
| 323 var client = new HttpClient(); |
| 324 var connection = client.getUrl(uri); |
| 325 |
| 326 connection.onError = (e) { |
| 327 // Show a friendly error if the URL couldn't be resolved. |
| 328 if (e is SocketIOException && e.osError.errorCode == 8) { |
| 329 e = 'Could not resolve URL "${uri.origin}".'; |
| 330 } |
| 331 |
| 332 client.shutdown(); |
| 333 completer.completeException(e); |
| 334 }; |
| 335 |
| 336 connection.onResponse = (response) { |
| 337 if (response.statusCode >= 400) { |
| 338 client.shutdown(); |
| 339 completer.completeException( |
| 340 new HttpException(response.statusCode, response.reasonPhrase)); |
| 341 return; |
| 342 } |
| 343 |
| 344 var resultStream = new ListInputStream(); |
| 345 var buffer = <int>[]; |
| 346 |
| 347 response.inputStream.onClosed = () { |
| 348 client.shutdown(); |
| 349 completer.complete(new String.fromCharCodes(buffer)); |
| 350 }; |
| 351 |
| 352 response.inputStream.onData = () { |
| 353 buffer.addAll(response.inputStream.read()); |
| 354 }; |
| 355 |
| 356 response.inputStream.onError = (e) { |
| 357 client.shutdown(); |
| 358 completer.completeException(e); |
| 359 }; |
| 360 }; |
| 361 |
| 362 return completer.future; |
| 363 } |
| 364 |
| 365 /** |
313 * Takes all input from [source] and writes it to [sink]. | 366 * Takes all input from [source] and writes it to [sink]. |
314 * | 367 * |
315 * [onClosed] is called when [source] is closed. | 368 * [onClosed] is called when [source] is closed. |
316 */ | 369 */ |
317 void pipeInputToInput(InputStream source, ListInputStream sink, | 370 void pipeInputToInput(InputStream source, ListInputStream sink, |
318 [void onClosed()]) { | 371 [void onClosed()]) { |
319 source.onClosed = () { | 372 source.onClosed = () { |
320 sink.markEndOfStream(); | 373 sink.markEndOfStream(); |
321 if (onClosed != null) onClosed(); | 374 if (onClosed != null) onClosed(); |
322 }; | 375 }; |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 stream.pipe(process.stdin); | 498 stream.pipe(process.stdin); |
446 process.stdout.pipe(stdout, close: false); | 499 process.stdout.pipe(stdout, close: false); |
447 process.stderr.pipe(stderr, close: false); | 500 process.stderr.pipe(stderr, close: false); |
448 | 501 |
449 process.onExit = completer.complete; | 502 process.onExit = completer.complete; |
450 process.onError = completer.completeException; | 503 process.onError = completer.completeException; |
451 return completer.future.transform((exitCode) => exitCode == 0); | 504 return completer.future.transform((exitCode) => exitCode == 0); |
452 } | 505 } |
453 | 506 |
454 /** | 507 /** |
| 508 * Exception thrown when an HTTP operation fails. |
| 509 */ |
| 510 class HttpException implements Exception { |
| 511 final int statusCode; |
| 512 final String reason; |
| 513 |
| 514 const HttpException(this.statusCode, this.reason); |
| 515 } |
| 516 |
| 517 /** |
455 * Contains the results of invoking a [Process] and waiting for it to complete. | 518 * Contains the results of invoking a [Process] and waiting for it to complete. |
456 */ | 519 */ |
457 class PubProcessResult { | 520 class PubProcessResult { |
458 final List<String> stdout; | 521 final List<String> stdout; |
459 final List<String> stderr; | 522 final List<String> stderr; |
460 final int exitCode; | 523 final int exitCode; |
461 | 524 |
462 const PubProcessResult(this.stdout, this.stderr, this.exitCode); | 525 const PubProcessResult(this.stdout, this.stderr, this.exitCode); |
463 | 526 |
464 bool get success => exitCode == 0; | 527 bool get success => exitCode == 0; |
(...skipping 20 matching lines...) Expand all Loading... |
485 return new Directory(entry); | 548 return new Directory(entry); |
486 } | 549 } |
487 | 550 |
488 /** | 551 /** |
489 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 552 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
490 */ | 553 */ |
491 Uri _getUri(uri) { | 554 Uri _getUri(uri) { |
492 if (uri is Uri) return uri; | 555 if (uri is Uri) return uri; |
493 return new Uri.fromString(uri); | 556 return new Uri.fromString(uri); |
494 } | 557 } |
OLD | NEW |