readSource static method

Future<Uint8List> readSource(
  1. String source
)

Read .rva bytes from a path, file:// or http(s):// URL.

Implementation

static Future<Uint8List> readSource(String source) async {
  final uri = Uri.tryParse(source);
  if (uri != null &&
      uri.hasScheme &&
      (uri.scheme == 'file' || uri.scheme == 'http' || uri.scheme == 'https')) {
    if (uri.scheme == 'file') {
      return File.fromUri(uri).readAsBytes();
    }
    final client = HttpClient();
    try {
      final request = await client.getUrl(uri);
      final response = await request.close();
      if (response.statusCode != HttpStatus.ok) {
        throw HttpException('HTTP ${response.statusCode} for $source');
      }
      final builder = BytesBuilder(copy: false);
      await for (final chunk in response) {
        builder.add(chunk);
      }
      return builder.takeBytes();
    } finally {
      client.close(force: true);
    }
  }
  // No recognised scheme: treat as a filesystem path.
  return File(source).readAsBytes();
}