readRange method
Read a byte range.
Returns bytes starting at start with the given length. For
encrypted files, only the overlapping chunks are decrypted; for
chunked files, only the overlapping chunk records are read.
Implementation
@override
Future<Uint8List> readRange(
String key, {
required int start,
required int length,
}) async {
checkNotDisposed();
final manifest = await _readManifest(key);
if (manifest == null) throw FileNotFoundError(key);
final end = (start + length).clamp(0, manifest.totalSize);
if (end <= start) return Uint8List(0);
final firstChunk = start ~/ chunkSize;
final lastChunk = (end - 1) ~/ chunkSize;
final builder = BytesBuilder(copy: false);
for (var i = firstChunk; i <= lastChunk; i++) {
final chunk = await _readChunk(key, manifest.generation, i);
final chunkStart = i * chunkSize;
final sliceStart = (start > chunkStart) ? start - chunkStart : 0;
final sliceEnd = (end < chunkStart + chunk.length)
? end - chunkStart
: chunk.length;
builder.add(chunk.sublist(sliceStart, sliceEnd));
}
return builder.takeBytes();
}