forEachBlock method

Future<void> forEachBlock(
  1. int blockSize,
  2. void action(
    1. Uint8List buffer
    )
)

Reads file as byte blocks, where each block has the size blockSize. The last block may be smaller than blockSize.

action is called with each block.

Implementation

Future<void> forEachBlock(
  int blockSize,
  void Function(Uint8List buffer) action,
) async {
  assert(blockSize > 0);

  final raf = await open();

  while (true) {
    final buffer = await raf.read(blockSize);
    if (buffer.isEmpty) break;
    action(buffer);
  }

  await raf.close();
}