readBytesAt method

Future<Uint8List> readBytesAt(
  1. int offset,
  2. int count, {
  3. CancellationToken? cancelToken,
})

Concurrently sets position to offset and reads count bytes using an available handle from the pool.

Multiple readBytesAt calls can run in parallel up to maxConcurrency without serializing or blocking each other. If cancelToken is provided and cancelled, reading is aborted and an empty Uint8List is returned.

Implementation

Future<Uint8List> readBytesAt(int offset, int count, {CancellationToken? cancelToken}) async {
  if (_isClosed) throw StateError('Cannot read from a closed SvsFile');
  if (count <= 0 || cancelToken?.isCancelled == true) return Uint8List(0);

  // If inside synchronized on this SvsFile, use the already locked primary handle directly:
  if (Zone.current[_syncZoneKey] == this) {
    if (cancelToken?.isCancelled == true) return Uint8List(0);
    await raf.setPosition(offset);
    return await raf.read(count);
  }

  final handle = await _acquireHandle(cancelToken: cancelToken);
  if (handle == null || cancelToken?.isCancelled == true) {
    if (handle != null) _releaseHandle(handle);
    return Uint8List(0);
  }

  try {
    await handle.setPosition(offset);
    return await handle.read(count);
  } finally {
    _releaseHandle(handle);
  }
}