readRange method

  1. @override
Future<Result<Uint8List, FileError>> readRange(
  1. String path,
  2. int start,
  3. int end
)
override

Reads the bytes of path in the half-open range [start, end).

Never throws: failures come back as Err. An end past EOF reads to EOF; a start at or past EOF yields an empty list.

Implementation

@override
Future<Result<Uint8List, FileError>> readRange(
  String path,
  int start,
  int end,
) async {
  final resolved = _resolve(path);
  if (end <= start) return Ok(Uint8List(0));
  RandomAccessFile? handle;
  try {
    handle = await File(resolved).open();
    final length = await handle.length();
    final from = start.clamp(0, length);
    final to = end.clamp(from, length);
    if (to <= from) return Ok(Uint8List(0));
    await handle.setPosition(from);
    return Ok(await handle.read(to - from));
  } on Object catch (error) {
    return Err(_toFileError(error, resolved));
  } finally {
    await handle?.close();
  }
}