fromFile static method Null safety

Future<CacheEntry> fromFile(
  1. File file,
  2. {bool loadContent = true}
)

create CacheEntry from file The file content like: url: xxxx length: xxxx ctime: xxx ttl: xxx content bytes...

Implementation

static Future<CacheEntry> fromFile(
  File file, {
  bool loadContent: true,
}) async {
  final Completer<CacheEntry> completer = Completer<CacheEntry>();

  RandomAccessFile rf = await file.open();
  // url
  String line = await readUtil(rf);
  String url = line.split(':')[1];

  line = await readUtil(rf);
  int length = int.parse(line.split(':')[1]);

  // ctime
  line = await readUtil(rf);
  DateTime ctime = DateTime.parse(line.substring(7));

  // ttl
  line = await readUtil(rf);
  int ttl = int.parse(line.split(':')[1]);

  assert(length == rf.lengthSync() - rf.positionSync());

  // bytes
  Uint8List? bytes =
      loadContent ? await rf.read(rf.lengthSync() - rf.positionSync()) : null;
  await rf.close();

  completer.complete(CacheEntry(
    url: url,
    ctime: ctime,
    ttl: ttl,
    bytes: bytes,
  ));
  return completer.future;
}