openRead method

  1. @override
Stream<Uint8List> openRead([
  1. int? start,
  2. int? end
])

Create a new independent Stream for the contents of this file.

If start is present, the file will be read from byte-offset start. Otherwise from the beginning (index 0).

If end is present, only up to byte-index end will be read. Otherwise, until end of file.

In order to make sure that system resources are freed, the stream must be read to completion or the subscription on the stream must be cancelled.

Implementation

@override
Stream<Uint8List> openRead([int? start, int? end]) {
  if (kTesting)
    print('[NativeWebXFile] openRead called with start: $start, end: $end');

  // Calculate range. We treat the provided 'end' as exclusive.
  final int fileSize = _file.size;
  final int from = start ?? 0;
  final int to = (end != null && end < fileSize) ? end - 1 : fileSize - 1;

  if (kTesting) {
    print(
        '[NativeWebXFile] Calculated range: from $from to $to (fileSize: $fileSize)');
  }

  if (from > to) {
    if (kTesting)
      print('[NativeWebXFile] from > to, returning empty stream.');
    return const Stream<Uint8List>.empty();
  }

  // Use Blob slicing API directly on the File object (File extends Blob)
  return _createBlobSliceStream(from, to + 1);
}