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

Unified Diff: runtime/lib/resource_patch.dart

Issue 1264413002: - Implement resource loading via the Resource class. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address review comments, simplified error handling. Created 5 years, 4 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 | « runtime/lib/internal_patch.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/resource_patch.dart
diff --git a/runtime/lib/resource_patch.dart b/runtime/lib/resource_patch.dart
new file mode 100644
index 0000000000000000000000000000000000000000..0643516881ba6b5b7b355cc0ec81ba41a7b43249
--- /dev/null
+++ b/runtime/lib/resource_patch.dart
@@ -0,0 +1,68 @@
+// Copyright (c) 2015, 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.
+
+patch class Resource {
+ /* patch */ const factory Resource(String uri) = _Resource;
+}
+
+class _Resource implements Resource {
+ final String _location;
+
+ const _Resource(this._location);
+
+ Uri get uri => Uri.base.resolve(_location);
+
+ Stream<List<int>> openRead() {
+ if (VMLibraryHooks.resourceReadAsBytes == null) {
+ throw new UnimplementedError("openRead");
+ }
+
+ var controller = new StreamController<List<int>>();
+ // We only need to implement the listener as there is no way to provide
+ // back pressure into the channel.
+ controller.onListen = () {
+ // Once there is a listener, we kick off the loading of the resource.
+ readAsBytes().then((value) {
+ // The resource loading implementation sends all of the data in a
+ // single message. So the stream will only get a single value posted.
+ controller.add(value);
+ controller.close();
+ },
+ onError: (e, s) {
+ // In case the future terminates with an error we propagate it to the
+ // stream.
+ controller.addError(e, s);
+ controller.close();
+ });
+ };
+
+ return controller.stream;
+ }
+
+ Future<List<int>> readAsBytes() {
+ if (VMLibraryHooks.resourceReadAsBytes == null) {
+ throw new UnimplementedError("readAsBytes");
+ }
+
+ return VMLibraryHooks.resourceReadAsBytes(this.uri);
+ }
+
+ Future<String> readAsString({Encoding encoding : UTF8}) {
+ if (VMLibraryHooks.resourceReadAsBytes == null) {
+ throw new UnimplementedError("readAsString");
+ }
+
+ var completer = new Completer<String>();
+
+ readAsBytes().then((bytes) {
+ var str = encoding.decode(bytes);
+ completer.complete(str);
+ },
+ onError: (e, s) {
+ completer.completeError(e,s);
+ });
+
+ return completer.future;
+ }
+}
« no previous file with comments | « runtime/lib/internal_patch.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698