Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(374)

Unified Diff: lib/src/browser/html_io.dart

Issue 1612003002: Add browser-compatible version. (Closed) Base URL: https://github.com/dart-lang/resource.git@master
Patch Set: Made test run Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/resource.dart ('k') | lib/src/browser/loader.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/browser/html_io.dart
diff --git a/lib/src/browser/html_io.dart b/lib/src/browser/html_io.dart
new file mode 100644
index 0000000000000000000000000000000000000000..2896967db50e2e7e09c555bab0feb7424087c0fc
--- /dev/null
+++ b/lib/src/browser/html_io.dart
@@ -0,0 +1,60 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import "dart:async" show Future, Stream;
+import "dart:convert" show Encoding, LATIN1, UTF8;
+import "dart:html";
+import "dart:typed_data" show Uint8List, ByteBuffer;
+
+/// Reads the bytes of a URI as a stream of bytes.
+Stream<List<int>> readAsStream(Uri uri) async* {
+ // TODO(lrn): Should file be run through XmlHTTPRequest too?
+ if (uri.scheme == "http" || uri.scheme == "https") {
+ // TODO: Stream in chunks if DOM has a way to do so.
+ List<int> response = await _httpGetBytes(uri);
+ yield response;
+ return;
+ }
+ if (uri.scheme == "data") {
+ yield uri.data.contentAsBytes();
+ return;
+ }
+ throw new UnsupportedError("Unsupported scheme: $uri");
+}
+
+/// Reads the bytes of a URI as a list of bytes.
+Future<List<int>> readAsBytes(Uri uri) async {
+ if (uri.scheme == "http" || uri.scheme == "https") {
+ return _httpGetBytes(uri);
+ }
+ if (uri.scheme == "data") {
+ return uri.data.contentAsBytes();
+ }
+ throw new UnsupportedError("Unsupported scheme: $uri");
+}
+
+/// Reads the bytes of a URI as a string.
+Future<String> readAsString(Uri uri, Encoding encoding) async {
+ if (uri.scheme == "http" || uri.scheme == "https") {
+ // Fetch as string if the encoding is expected to be understood,
+ // otherwise fetch as bytes and do decoding using the encoding.
+ if (encoding != null) {
+ return encoding.decode(await _httpGetBytes(uri));
+ }
+ return HttpRequest.getString(uri.toString());
+ }
+ if (uri.scheme == "data") {
+ return uri.data.contentAsString(encoding: encoding);
+ }
+ throw new UnsupportedError("Unsupported scheme: $uri");
+}
+
+Future<List<int>> _httpGetBytes(Uri uri) {
+ return HttpRequest
+ .request(uri.toString(), responseType: "arraybuffer")
+ .then((request) {
+ ByteBuffer data = request.response;
+ return data.asUint8List();
+ });
+}
« no previous file with comments | « lib/resource.dart ('k') | lib/src/browser/loader.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698