| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // dart:io based strategy for loading resources. | 5 // dart:io based strategy for loading resources. |
| 6 | 6 |
| 7 import "dart:io" show File, HttpClient, HttpClientRequest, HttpClientResponse; | 7 import "dart:io" show File, HttpClient, HttpClientRequest, HttpClientResponse; |
| 8 import "dart:async" show Future, Stream; | 8 import "dart:async" show Future, Stream; |
| 9 import "dart:convert" show Encoding, UTF8; | 9 import "dart:convert" show Encoding, UTF8; |
| 10 import "package:typed_data/typed_buffers.dart" show Uint8Buffer; | 10 import "package:typed_data/typed_buffers.dart" show Uint8Buffer; |
| 11 | 11 |
| 12 // TODO(lrn): Add support for "data:" URIs. |
| 13 |
| 14 /// Read the bytes of a URI as a stream of bytes. |
| 12 Stream<List<int>> readAsStream(Uri uri) async* { | 15 Stream<List<int>> readAsStream(Uri uri) async* { |
| 13 if (uri.scheme == "file") { | 16 if (uri.scheme == "file") { |
| 14 yield* new File.fromUri(uri).openRead(); | 17 yield* new File.fromUri(uri).openRead(); |
| 15 return; | 18 return; |
| 16 } | 19 } |
| 17 if (uri.scheme == "http" || uri.scheme == "https") { | 20 if (uri.scheme == "http" || uri.scheme == "https") { |
| 18 HttpClientResponse response = await _httpGet(uri); | 21 HttpClientResponse response = await _httpGet(uri); |
| 19 yield* response; | 22 yield* response; |
| 20 return; | 23 return; |
| 21 } | 24 } |
| 22 throw new UnsupportedError("Unsupported scheme: $uri"); | 25 throw new UnsupportedError("Unsupported scheme: $uri"); |
| 23 } | 26 } |
| 24 | 27 |
| 28 /// Read the bytes of a URI as a list of bytes. |
| 25 Future<List<int>> readAsBytes(Uri uri) async { | 29 Future<List<int>> readAsBytes(Uri uri) async { |
| 26 if (uri.scheme == "file") { | 30 if (uri.scheme == "file") { |
| 27 return new File.fromUri(uri).readAsBytes(); | 31 return new File.fromUri(uri).readAsBytes(); |
| 28 } | 32 } |
| 29 if (uri.scheme == "http" || uri.scheme == "https") { | 33 if (uri.scheme == "http" || uri.scheme == "https") { |
| 30 HttpClientResponse response = await _httpGet(uri); | 34 HttpClientResponse response = await _httpGet(uri); |
| 31 Uint8Buffer buffer = new Uint8Buffer(); | 35 Uint8Buffer buffer = new Uint8Buffer(); |
| 32 await for (var bytes in response) { | 36 await for (var bytes in response) { |
| 33 buffer.addAll(bytes); | 37 buffer.addAll(bytes); |
| 34 } | 38 } |
| 35 return buffer.toList(); | 39 return buffer.toList(); |
| 36 } | 40 } |
| 37 throw new UnsupportedError("Unsupported scheme: $uri"); | 41 throw new UnsupportedError("Unsupported scheme: $uri"); |
| 38 } | 42 } |
| 39 | 43 |
| 44 /// Read the bytes of a URI as a string. |
| 45 /// |
| 46 /// Encoding defaults to UTF-8. |
| 40 Future<String> readAsString(Uri uri, Encoding encoding) async { | 47 Future<String> readAsString(Uri uri, Encoding encoding) async { |
| 41 if (encoding == null) encoding = UTF8; | 48 if (encoding == null) encoding = UTF8; |
| 42 if (uri.scheme == "file") { | 49 if (uri.scheme == "file") { |
| 43 return new File.fromUri(uri).readAsString(encoding: encoding); | 50 return new File.fromUri(uri).readAsString(encoding: encoding); |
| 44 } | 51 } |
| 45 if (uri.scheme == "http" || uri.scheme == "https") { | 52 if (uri.scheme == "http" || uri.scheme == "https") { |
| 46 HttpClientResponse response = await _httpGet(uri); | 53 HttpClientResponse response = await _httpGet(uri); |
| 47 Uint8Buffer buffer = new Uint8Buffer(); | 54 Uint8Buffer buffer = new Uint8Buffer(); |
| 48 await for (var bytes in response) { | 55 await for (var bytes in response) { |
| 49 buffer.addAll(bytes); | 56 buffer.addAll(bytes); |
| 50 } | 57 } |
| 51 new String.fromCharCodes(buffer.toList()); | 58 new String.fromCharCodes(buffer.toList()); |
| 52 } | 59 } |
| 53 throw new UnsupportedError("Unsupported scheme: $uri"); | 60 throw new UnsupportedError("Unsupported scheme: $uri"); |
| 54 } | 61 } |
| 55 | 62 |
| 56 Future<HttpClientResponse> _httpGet(Uri uri) async { | 63 Future<HttpClientResponse> _httpGet(Uri uri) async { |
| 57 HttpClientRequest request = await new HttpClient().getUrl(uri); | 64 HttpClientRequest request = await new HttpClient().getUrl(uri); |
| 58 return await request.close(); | 65 return await request.close(); |
| 59 } | 66 } |
| OLD | NEW |