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