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

Side by Side 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: Simplify Stream creation. 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 unified diff | Download patch
« runtime/bin/builtin.dart ('K') | « runtime/lib/internal_patch.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 patch class Resource {
6 /* patch */ const factory Resource(String uri) = _Resource;
7 }
8
9 class _Resource implements Resource {
10 final String _location;
11
12 const _Resource(this._location);
13
14 Uri get uri => Uri.base.resolve(_location);
15
16 Stream<List<int>> openRead() {
17 if (VMLibraryHooks.resourceReadAsBytes == null) {
18 throw new UnimplementedError("openRead");
19 }
20
21 var controller = new StreamController<List<int>>();
22 // We only need to implement the listener as there is no way to provide
23 // back pressure into the channel.
24 controller.onListen = () {
25 // Once there is a listener, we kick off the loading of the resource.
26 readAsBytes().then((value) {
27 // The resource loading implementation sends all of the data in a
28 // single message. So the stream will only get a single value posted.
29 controller.add(value);
30 controller.close();
31 },
32 onError: (e, s) {
33 // In case the future terminates with an error we propagate it to the
34 // stream.
35 controller.addError(e, s);
36 controller.close();
37 });
38 };
39
40 return controller.stream;
41 }
42
43 Future<List<int>> readAsBytes() {
44 if (VMLibraryHooks.resourceReadAsBytes == null) {
45 throw new UnimplementedError("readAsBytes");
46 }
47
48 return VMLibraryHooks.resourceReadAsBytes(this.uri);
49 }
50
51 Future<String> readAsString({Encoding encoding : UTF8}) {
52 if (VMLibraryHooks.resourceReadAsBytes == null) {
53 throw new UnimplementedError("readAsString");
54 }
55
56 var completer = new Completer<String>();
57
58 readAsBytes().then((bytes) {
59 var str = encoding.decode(bytes);
60 completer.complete(str);
61 },
62 onError: (e, s) {
63 completer.completeError(e,s);
64 }
siva 2015/08/04 21:48:23 indentation is off from rest of file.
65 );
66
67 return completer.future;
68 }
69 }
OLDNEW
« runtime/bin/builtin.dart ('K') | « runtime/lib/internal_patch.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698