sha256OfFileRange function
The sha256 of bytes [begin, end) of the file at path, read and hashed
on a spawned isolate (a 5.8 MB section is milliseconds, but not on the UI
isolate). Throws FileSystemException when the file cannot be read or ends
before end.
Implementation
Future<String> sha256OfFileRange(String path, int begin, int end) =>
Isolate.run(() {
final file = File(path).openSync();
try {
file.setPositionSync(begin);
// readIntoSync may return fewer bytes than asked before the end of
// the file (network and virtual file systems), so read until the
// range is consumed; only an empty read is the end of the file.
final bytes = Uint8List(end - begin);
var read = 0;
while (read < bytes.length) {
final n = file.readIntoSync(bytes, read);
if (n == 0) {
throw FileSystemException('the file ends before byte $end', path);
}
read += n;
}
return sha256.convert(bytes).toString();
} finally {
file.closeSync();
}
});