readFileBytes method
Reads raw file bytes as Uint8List asynchronously. When maxBytes is set, only reads up to that many bytes.
Implementation
@override
Future<Uint8List> readFileBytes(String fsPath, {int? maxBytes}) async {
if (maxBytes == null) {
return await File(fsPath).readAsBytes();
}
final raf = await File(fsPath).open(mode: FileMode.read);
try {
final size = (await raf.length());
final readSize = min(size, maxBytes);
final buffer = Uint8List(readSize);
int offset = 0;
while (offset < readSize) {
final bytesRead = await raf.readInto(buffer, offset, readSize);
if (bytesRead == 0) break;
offset += bytesRead;
}
return offset < readSize ? buffer.sublist(0, offset) : buffer;
} finally {
await raf.close();
}
}