OLD | NEW |
1 // Copyright (c) 2016, 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 import "dart:async" show Future, Stream; | 5 import "dart:async" show Future, Stream; |
6 import "dart:convert" show Encoding, LATIN1, UTF8; | 6 import "dart:convert" show Encoding; |
7 import "dart:html"; | 7 import "dart:html"; |
8 import "dart:typed_data" show Uint8List, ByteBuffer; | 8 import "dart:typed_data" show ByteBuffer; |
9 | 9 |
10 /// Reads the bytes of a URI as a stream of bytes. | 10 /// Reads the bytes of a URI as a stream of bytes. |
11 Stream<List<int>> readAsStream(Uri uri) async* { | 11 Stream<List<int>> readAsStream(Uri uri) async* { |
12 // TODO(lrn): Should file be run through XmlHTTPRequest too? | 12 // TODO(lrn): Should file be run through XmlHTTPRequest too? |
13 if (uri.scheme == "http" || uri.scheme == "https") { | 13 if (uri.scheme == "http" || uri.scheme == "https") { |
14 // TODO: Stream in chunks if DOM has a way to do so. | 14 // TODO: Stream in chunks if DOM has a way to do so. |
15 List<int> response = await _httpGetBytes(uri); | 15 List<int> response = await _httpGetBytes(uri); |
16 yield response; | 16 yield response; |
17 return; | 17 return; |
18 } | 18 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 } | 51 } |
52 | 52 |
53 Future<List<int>> _httpGetBytes(Uri uri) { | 53 Future<List<int>> _httpGetBytes(Uri uri) { |
54 return HttpRequest | 54 return HttpRequest |
55 .request(uri.toString(), responseType: "arraybuffer") | 55 .request(uri.toString(), responseType: "arraybuffer") |
56 .then((request) { | 56 .then((request) { |
57 ByteBuffer data = request.response; | 57 ByteBuffer data = request.response; |
58 return data.asUint8List(); | 58 return data.asUint8List(); |
59 }); | 59 }); |
60 } | 60 } |
OLD | NEW |