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 import "dart:async" show Future, Stream; | |
6 import "dart:convert" show Encoding; | |
7 import "dart:isolate" show Isolate; | |
8 import "io.dart" as io; // TODO: make this import configuration dependent. | |
9 | |
10 /// Resource loading strategy. | |
11 /// | |
12 /// An abstraction of the functionality needed to load resources. | |
13 /// | |
14 /// An implementation decides which URI schemes it supports. | |
floitsch
2016/01/14 15:11:04
Implementations of this interface decide which URI
Lasse Reichstein Nielsen
2016/01/15 09:40:02
Done.
| |
15 abstract class ResourceLoader { | |
16 /// A resource loader that can load as many of the following URI | |
17 /// schemes as are supported by the platform: | |
18 /// * file | |
19 /// * http | |
20 /// * https | |
21 /// * data | |
22 /// * package | |
23 /// (For example, file: URIs are not supported in the browser). | |
floitsch
2016/01/14 15:11:04
You need a new line after * package.
Otherwise the
Lasse Reichstein Nielsen
2016/01/15 09:40:02
Done.
| |
24 /// Relative URI references are accepted - they are resolved against | |
25 /// [Uri.base] before being loaded. | |
26 static const ResourceLoader defaultLoader = const PackageLoader(); | |
27 | |
28 /// Reads the file located by [uri] as a stream of bytes. | |
29 Stream<List<int>> openRead(Uri uri); | |
30 | |
31 /// Reads the file located by [uri] as a list of bytes. | |
32 Future<List<int>> readAsBytes(Uri uri); | |
33 | |
34 /// Reads the file located by [uri] as a [String]. | |
35 /// | |
36 /// The file bytes are decoded using [encoding], if provided. | |
37 /// | |
38 /// If [encoding] is omitted, the default for the `file:` scheme is UTF-8. | |
39 /// For `http`, `https` and `data` URIs, the Content-Type header's charset | |
40 /// is used, if available and recognized by [Encoding.getByName], | |
41 /// otherwise it defaults to Latin-1 for `http` and `https` | |
42 /// and to ASCII for `data` URIs. | |
43 Future<String> readAsString(Uri uri, { Encoding encoding }); | |
44 } | |
45 | |
46 /// Default implementation of [ResourceLoader]. | |
47 /// | |
48 /// Uses the system's available loading functionality to implement the | |
49 /// loading functions. | |
50 /// | |
51 /// Supports `http:`, `https:`, `file:` and `data:` URIs. | |
52 class DefaultLoader implements ResourceLoader { | |
53 const DefaultLoader(); | |
54 | |
55 Stream<List<int>> openRead(Uri uri) => io.readAsStream(uri); | |
56 | |
57 Future<List<int>> readAsBytes(Uri uri) => io.readAsBytes(uri); | |
58 | |
59 Future<String> readAsString(Uri uri, { Encoding encoding }) => | |
60 io.readAsString(uri, encoding); | |
61 } | |
62 | |
63 | |
64 /// Implementation of [ResourceLoader] that accepts relative and package: URIs. | |
65 /// | |
66 /// Like [DefaultLoader] except that it resolves package URIs and relative | |
67 /// URI references as well. | |
68 /// | |
69 /// This class may be useful when you don't want to bother creating a [Resource] | |
70 /// object, and just want to load a resource directly. | |
71 class PackageLoader implements ResourceLoader { | |
72 const PackageLoader(); | |
73 | |
74 Stream<List<int>> openRead(Uri uri) async* { | |
75 yield* io.readAsStream(await resolveUri(uri)); | |
76 } | |
77 | |
78 Future<List<int>> readAsBytes(Uri uri) async => | |
79 io.readAsBytes(await resolveUri(uri)); | |
80 | |
81 Future<String> readAsString(Uri uri, { Encoding encoding }) async => | |
82 io.readAsString(await resolveUri(uri), encoding); | |
83 } | |
84 | |
85 /// Helper function for resolving to a non-relative, non-package URI. | |
86 Future<Uri> resolveUri(Uri uri) async { | |
87 if (uri.scheme == "package") { | |
88 return Isolate.resolvePackageUri(uri); | |
89 } | |
90 return Uri.base.resolveUri(uri); | |
91 } | |
OLD | NEW |