forEachBlock method

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

Reads file by byte blocks and calls action for each block read.

This functions passes the byte buffer to the action function.

You can use this function for huge files.

Implementation

Future<void> forEachBlock(
  int blockSize,
  void Function(Uint8List buffer) action,
) async {
  final raf = await open();
  // ignore: literal_only_boolean_expressions
  while (true) {
    final buffer = await raf.read(blockSize);
    if (buffer.length == blockSize) {
      action(buffer);
    } else if (buffer.isNotEmpty) {
      action(buffer);
      break;
    } else {
      break;
    }
  }
  await raf.close();
}