Index: lib/src/resource.dart |
diff --git a/lib/src/resource.dart b/lib/src/resource.dart |
index 924ec051b364e38c6fa90bd3867704bb2100f69f..2b19d9eb414f3aab6af69126799d4122d428fa86 100644 |
--- a/lib/src/resource.dart |
+++ b/lib/src/resource.dart |
@@ -1,87 +1,57 @@ |
-// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
+// Copyright (c) 2016, 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. |
+import "dart:core" hide Resource; |
import "dart:async" show Future, Stream; |
import "dart:convert" show Encoding; |
-import "dart:isolate" show Isolate; |
-import "loader.dart"; |
-/// A resource that can be read into the program. |
-/// |
-/// A resource is data that can be located using a URI and read into |
-/// the program at runtime. |
-/// The URI may use the `package` scheme to read resources provided |
-/// along with package sources. |
-abstract class Resource { |
- /// Creates a resource object with the given [uri] as location. |
- /// |
- /// The [uri] must be either a [Uri] or a string containing a valid URI. |
- /// If the string is not a valid URI, using any of the functions on |
- /// the resource object will fail. |
- /// |
- /// The URI may be relative, in which case it will be resolved |
- /// against [Uri.base] before being used. |
- /// |
- /// The URI may use the `package` scheme, which is always supported. |
- /// Other schemes may also be supported where possible. |
- /// |
- /// If [loader] is provided, it is used to load absolute non-package URIs. |
- /// Package: URIs are resolved to a non-package URI before being loaded, so |
- /// the loader doesn't have to support package: URIs, nor does it need to |
- /// support relative URI references. |
- /// If [loader] is omitted, a default implementation is used which supports |
- /// as many of `http`, `https`, `file` and `data` as are available on the |
- /// current platform. |
- const factory Resource(uri, {ResourceLoader loader}) = _Resource; |
+import "resource_loader.dart"; |
+import "resolve.dart"; |
- /// The location URI of this resource. |
- /// |
- /// This is a [Uri] of the `uri` parameter given to the constructor. |
- /// If the parameter was a string that did not contain a valid URI, |
- /// reading `uri` will fail. |
- Uri get uri; |
- |
- /// Reads the resource content as a stream of bytes. |
- Stream<List<int>> openRead(); |
+/// Base resource implementation. |
+class Resource { |
+ // Default implementation of a generic `Resource` class. |
+ // |
+ // Actually exposed `Resource` interfaces uses this as implementation, |
+ // but expose a different `Resource` class with plaform-dependent statics. |
+ // Requires a loader to be provided. |
- /// Reads the resource content as a single list of bytes. |
- Future<List<int>> readAsBytes(); |
- |
- /// Reads the resource content as a string. |
- /// |
- /// The content is decoded into a string using an [Encoding]. |
- /// If no other encoding is provided, it defaults to UTF-8. |
- Future<String> readAsString({Encoding encoding}); |
-} |
- |
-class _Resource implements Resource { |
/// Loading strategy for the resource. |
final ResourceLoader _loader; |
/// The URI of the resource. |
final _uri; |
- const _Resource(uri, {ResourceLoader loader}) |
- : _uri = uri, _loader = (loader != null) ? loader : const DefaultLoader(); |
- // TODO: Make this `loader ?? const DefaultLoader()` when ?? is const. |
+ const Resource(uri, ResourceLoader loader) |
+ : _uri = uri, _loader = loader; |
+ /// The location URI of this resource. |
+ /// |
+ /// This is a [Uri] of the `uri` parameter given to the constructor. |
+ /// If the parameter was a string that did not contain a valid URI, |
+ /// reading `uri` will fail. |
Uri get uri => (_uri is String) ? Uri.parse(_uri) : (_uri as Uri); |
+ /// Reads the resource content as a stream of bytes. |
Stream<List<int>> openRead() async* { |
- Uri uri = await _resolvedUri; |
+ Uri uri = await resolveUri(this.uri); |
yield* _loader.openRead(uri); |
} |
+ /// Reads the resource content as a single list of bytes. |
Future<List<int>> readAsBytes() async { |
- Uri uri = await _resolvedUri; |
+ Uri uri = await resolveUri(this.uri); |
return _loader.readAsBytes(uri); |
} |
+ /// Reads the resource content as a string. |
+ /// |
+ /// The content is decoded into a string using an [Encoding]. |
+ /// If no encoding is provided, an encoding is chosen depending on the |
+ /// protocol and/or available metadata. |
Future<String> readAsString({Encoding encoding}) async { |
- Uri uri = await _resolvedUri; |
+ Uri uri = await resolveUri(this.uri); |
return _loader.readAsString(uri, encoding: encoding); |
} |
- |
- Future<Uri> get _resolvedUri => resolveUri(uri); |
} |