| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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. | |
| 6 | |
| 7 import "dart:async" show Future, Stream; | 5 import "dart:async" show Future, Stream; |
| 8 import "dart:convert" show Encoding, LATIN1, UTF8; | 6 import "dart:convert" show Encoding, LATIN1, UTF8; |
| 9 import "dart:io" show | 7 import "dart:io" show |
| 10 File, HttpClient, HttpClientResponse, HttpClientRequest, HttpHeaders; | 8 File, HttpClient, HttpClientResponse, HttpClientRequest, HttpHeaders; |
| 11 import "dart:typed_data" show Uint8List; | 9 import "dart:typed_data" show Uint8List; |
| 10 |
| 12 import "package:typed_data/typed_buffers.dart" show Uint8Buffer; | 11 import "package:typed_data/typed_buffers.dart" show Uint8Buffer; |
| 13 | 12 |
| 14 /// Read the bytes of a URI as a stream of bytes. | 13 /// Read the bytes of a URI as a stream of bytes. |
| 15 Stream<List<int>> readAsStream(Uri uri) async* { | 14 Stream<List<int>> readAsStream(Uri uri) async* { |
| 16 if (uri.scheme == "file") { | 15 if (uri.scheme == "file") { |
| 17 yield* new File.fromUri(uri).openRead(); | 16 yield* new File.fromUri(uri).openRead(); |
| 18 return; | 17 return; |
| 19 } | 18 } |
| 20 if (uri.scheme == "http" || uri.scheme == "https") { | 19 if (uri.scheme == "http" || uri.scheme == "https") { |
| 21 HttpClientResponse response = await _httpGet(uri); | 20 HttpClientResponse response = await _httpGet(uri); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 return uri.data.contentAsString(encoding: encoding); | 82 return uri.data.contentAsString(encoding: encoding); |
| 84 } | 83 } |
| 85 throw new UnsupportedError("Unsupported scheme: $uri"); | 84 throw new UnsupportedError("Unsupported scheme: $uri"); |
| 86 } | 85 } |
| 87 | 86 |
| 88 Future<HttpClientResponse> _httpGet(Uri uri) async { | 87 Future<HttpClientResponse> _httpGet(Uri uri) async { |
| 89 HttpClientRequest request = await new HttpClient().getUrl(uri); | 88 HttpClientRequest request = await new HttpClient().getUrl(uri); |
| 90 request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*"); | 89 request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*"); |
| 91 return request.close(); | 90 return request.close(); |
| 92 } | 91 } |
| OLD | NEW |