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 _httpGetBytes(uri); |
22 yield* response; | 21 yield* response; |
23 return; | 22 return; |
24 } | 23 } |
25 if (uri.scheme == "data") { | 24 if (uri.scheme == "data") { |
26 yield uri.data.contentAsBytes(); | 25 yield uri.data.contentAsBytes(); |
27 return; | 26 return; |
28 } | 27 } |
29 throw new UnsupportedError("Unsupported scheme: $uri"); | 28 throw new UnsupportedError("Unsupported scheme: $uri"); |
30 } | 29 } |
31 | 30 |
32 /// Read the bytes of a URI as a list of bytes. | 31 /// Read the bytes of a URI as a list of bytes. |
33 Future<List<int>> readAsBytes(Uri uri) async { | 32 Future<List<int>> readAsBytes(Uri uri) async { |
34 if (uri.scheme == "file") { | 33 if (uri.scheme == "file") { |
35 return new File.fromUri(uri).readAsBytes(); | 34 return new File.fromUri(uri).readAsBytes(); |
36 } | 35 } |
37 if (uri.scheme == "http" || uri.scheme == "https") { | 36 if (uri.scheme == "http" || uri.scheme == "https") { |
38 HttpClientResponse response = await _httpGet(uri); | 37 HttpClientResponse response = await _httpGetBytes(uri); |
39 int length = response.contentLength; | 38 int length = response.contentLength; |
40 if (length < 0) length = 0; | 39 if (length < 0) length = 0; |
41 var buffer = new Uint8Buffer(length); | 40 var buffer = new Uint8Buffer(length); |
42 await for (var bytes in response) { | 41 await for (var bytes in response) { |
43 buffer.addAll(bytes); | 42 buffer.addAll(bytes); |
44 } | 43 } |
45 return buffer.toList(); | 44 return buffer.toList(); |
46 } | 45 } |
47 if (uri.scheme == "data") { | 46 if (uri.scheme == "data") { |
48 return uri.data.contentAsBytes(); | 47 return uri.data.contentAsBytes(); |
(...skipping 18 matching lines...) Expand all Loading... |
67 encoding ??= Encoding.getByName(response.headers.contentType?.charset); | 66 encoding ??= Encoding.getByName(response.headers.contentType?.charset); |
68 if (encoding == null || encoding == LATIN1) { | 67 if (encoding == null || encoding == LATIN1) { |
69 // Default to LATIN-1 if no encoding found. | 68 // Default to LATIN-1 if no encoding found. |
70 // Special case LATIN-1 since it is common and doesn't need decoding. | 69 // Special case LATIN-1 since it is common and doesn't need decoding. |
71 int length = response.contentLength; | 70 int length = response.contentLength; |
72 if (length < 0) length = 0; | 71 if (length < 0) length = 0; |
73 var buffer = new Uint8Buffer(length); | 72 var buffer = new Uint8Buffer(length); |
74 await for (var bytes in response) { | 73 await for (var bytes in response) { |
75 buffer.addAll(bytes); | 74 buffer.addAll(bytes); |
76 } | 75 } |
77 var byteList = new Uint8List.view(buffer.buffer, 0, buffer.length); | 76 var byteList = buffer.buffer.asUint8List(0, buffer.length); |
78 return new String.fromCharCodes(byteList); | 77 return new String.fromCharCodes(byteList); |
79 } | 78 } |
80 return response.transform(encoding.decoder).join(); | 79 return response.transform(encoding.decoder).join(); |
81 } | 80 } |
82 if (uri.scheme == "data") { | 81 if (uri.scheme == "data") { |
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> _httpGetBytes(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 |