readCapped function

Future<String?> readCapped(
  1. RandomAccessFile handle
)

Read a capped portion of a file. Returns null if the file exceeds maxScanBytes.

Implementation

Future<String?> readCapped(RandomAccessFile handle) async {
  var buf = Uint8List(chunkSize);
  var total = 0;

  while (true) {
    if (total == buf.length) {
      final grown = Uint8List(
        math.min(buf.length * 2, maxScanBytes + chunkSize),
      );
      grown.setRange(0, total, buf);
      buf = grown;
    }
    await handle.setPosition(total);
    final bytesRead = await handle.readInto(buf, total, buf.length);
    final actualRead = bytesRead - total;
    if (actualRead <= 0) break;
    total = bytesRead;
    if (total > maxScanBytes) return null;
  }

  return _normalizeCRLF(buf, total);
}