OLD | NEW |
| (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 }); | |
65 | |
66 return completer.future; | |
67 } | |
68 } | |
OLD | NEW |