readSlice method

  1. @override
Future<ByteData> readSlice(
  1. int start,
  2. int end
)
override

Returns the bytes as ByteData in the given section. The start is inclusive, the end is exclusive.

Implementation

@override
Future<ByteData> readSlice(int start, int end) async {
  final size = await byteLength;
  if (start >= size) return ByteData(0);
  if (end > size) end = size;
  final blocks = await file.openRead(start, end).toList();
  if (blocks.isNotEmpty) {
    final overallSize =
        blocks.fold<int>(0, (sum, block) => sum + block.length);
    final bytes = Uint8List(overallSize);
    var offset = 0;
    for (var block in blocks) {
      bytes.setRange(offset, block.length, block);
      offset += block.length;
    }
    return bytes.buffer.asByteData();
  } else {
    if (blocks.isEmpty) return ByteData(0);
    var block = blocks.first;
    if (block is! TypedData) block = Uint8List.fromList(block);
    final b = block as TypedData;
    return b.buffer.asByteData(b.offsetInBytes, b.lengthInBytes);
  }
}