read method
Reads exactly the bytes covered by range.
Implementation
@override
Future<Uint8List> read(ByteRange range) async {
if (!_bounds.containsRange(range)) {
throw ByteRangeOutOfBoundsException(range, _bounds);
}
final result = Uint8List(range.length);
var sourceOffset = range.start;
var destinationOffset = 0;
while (sourceOffset < range.end) {
final remaining = range.end - sourceOffset;
final count = remaining < chunkSize ? remaining : chunkSize;
final end = sourceOffset + count;
Uint8List bytes;
try {
final buffer = await _blob
.slice(sourceOffset, end)
.arrayBuffer()
.toDart;
bytes = buffer.toDart.asUint8List();
} catch (error) {
throw ByteSourceIoException(operation: 'read Blob bytes', cause: error);
}
if (bytes.length != count) {
throw TruncatedReadException(
range: ByteRange(sourceOffset, end),
expectedLength: count,
actualLength: bytes.length,
);
}
result.setRange(destinationOffset, destinationOffset + count, bytes);
sourceOffset = end;
destinationOffset += count;
}
return result;
}