| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // dart:io based strategy for loading resources. | |
| 6 | |
| 7 import "dart:async" show Future, Stream; | |
| 8 import "dart:convert" show Encoding, LATIN1, UTF8; | |
| 9 import "dart:io" show | |
| 10 File, HttpClient, HttpClientResponse, HttpClientRequest, HttpHeaders; | |
| 11 import "dart:typed_data" show Uint8List; | |
| 12 import "package:typed_data/typed_buffers.dart" show Uint8Buffer; | |
| 13 | |
| 14 /// Read the bytes of a URI as a stream of bytes. | |
| 15 Stream<List<int>> readAsStream(Uri uri) async* { | |
| 16 if (uri.scheme == "file") { | |
| 17 yield* new File.fromUri(uri).openRead(); | |
| 18 return; | |
| 19 } | |
| 20 if (uri.scheme == "http" || uri.scheme == "https") { | |
| 21 HttpClientResponse response = await _httpGet(uri); | |
| 22 yield* response; | |
| 23 return; | |
| 24 } | |
| 25 if (uri.scheme == "data") { | |
| 26 yield uri.data.contentAsBytes(); | |
| 27 return; | |
| 28 } | |
| 29 throw new UnsupportedError("Unsupported scheme: $uri"); | |
| 30 } | |
| 31 | |
| 32 /// Read the bytes of a URI as a list of bytes. | |
| 33 Future<List<int>> readAsBytes(Uri uri) async { | |
| 34 if (uri.scheme == "file") { | |
| 35 return new File.fromUri(uri).readAsBytes(); | |
| 36 } | |
| 37 if (uri.scheme == "http" || uri.scheme == "https") { | |
| 38 HttpClientResponse response = await _httpGet(uri); | |
| 39 int length = response.contentLength; | |
| 40 if (length < 0) length = 0; | |
| 41 var buffer = new Uint8Buffer(length); | |
| 42 await for (var bytes in response) { | |
| 43 buffer.addAll(bytes); | |
| 44 } | |
| 45 return buffer.toList(); | |
| 46 } | |
| 47 if (uri.scheme == "data") { | |
| 48 return uri.data.contentAsBytes(); | |
| 49 } | |
| 50 throw new UnsupportedError("Unsupported scheme: $uri"); | |
| 51 } | |
| 52 | |
| 53 /// Read the bytes of a URI as a string. | |
| 54 Future<String> readAsString(Uri uri, Encoding encoding) async { | |
| 55 if (uri.scheme == "file") { | |
| 56 if (encoding == null) encoding = UTF8; | |
| 57 return new File.fromUri(uri).readAsString(encoding: encoding); | |
| 58 } | |
| 59 if (uri.scheme == "http" || uri.scheme == "https") { | |
| 60 HttpClientRequest request = await new HttpClient().getUrl(uri); | |
| 61 // Prefer text/plain, text/* if possible, otherwise take whatever is there. | |
| 62 request.headers.set(HttpHeaders.ACCEPT, "text/plain, text/*, */*"); | |
| 63 if (encoding != null) { | |
| 64 request.headers.set(HttpHeaders.ACCEPT_CHARSET, encoding.name); | |
| 65 } | |
| 66 HttpClientResponse response = await request.close(); | |
| 67 encoding ??= Encoding.getByName(response.headers.contentType?.charset); | |
| 68 if (encoding == null || encoding == LATIN1) { | |
| 69 // Default to LATIN-1 if no encoding found. | |
| 70 // Special case LATIN-1 since it is common and doesn't need decoding. | |
| 71 int length = response.contentLength; | |
| 72 if (length < 0) length = 0; | |
| 73 var buffer = new Uint8Buffer(length); | |
| 74 await for (var bytes in response) { | |
| 75 buffer.addAll(bytes); | |
| 76 } | |
| 77 var byteList = new Uint8List.view(buffer.buffer, 0, buffer.length); | |
| 78 return new String.fromCharCodes(byteList); | |
| 79 } | |
| 80 return response.transform(encoding.decoder).join(); | |
| 81 } | |
| 82 if (uri.scheme == "data") { | |
| 83 return uri.data.contentAsString(encoding: encoding); | |
| 84 } | |
| 85 throw new UnsupportedError("Unsupported scheme: $uri"); | |
| 86 } | |
| 87 | |
| 88 Future<HttpClientResponse> _httpGet(Uri uri) async { | |
| 89 HttpClientRequest request = await new HttpClient().getUrl(uri); | |
| 90 request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*"); | |
| 91 return request.close(); | |
| 92 } | |
| OLD | NEW |