downStream method

Future<Stream<ProcessState>> downStream()

Implementation

Future<Stream<ProcessState>> downStream() async {
  controller = StreamController<ProcessState>();
  _controllerClosed = false;
  final req = await client.headUrl(Uri.parse(state.url));
  final resp = await req.close();

  if (!await _isRangeSupported(resp)) {
    throw UnsupportedException();
  }

  final contentLength = resp.headers['content-length'];
  if (contentLength == null || contentLength.isEmpty) {
    throw DownloadFailureException();
  }

  final fileSize = int.parse(contentLength.first);
  state.init(fileSize);

  final indexies = List<int>.generate(processors, (i) => i);
  fetching = true;
  for (var pid in indexies) {
    processor(state, pid, processors);
  }
  return controller!.stream;
}