import 'dart:collection'; import 'dart:ffi'; import 'dart:io'; import "package:ffi/ffi.dart"; class Uint8FlexList extends ListBase { final List _list = []; Uint8FlexList(); late Pointer pointer; @override Uint8 operator [](int index) => _list[index]; set list(List l) { _list.removeWhere((element) => true); _list.addAll(l); Pointer p = pointer; pointer = calloc(l.length); for (int i = 0; i < _list.length; ++i) { pointer[i] = _list[i] as int; } calloc.free(p); } @override void operator []=(int index, Uint8 value) { if (index > _list.length) { // resize the pointer pointer = calloc(index - 1); for (int i = 0; i < _list.length; ++i) { pointer[i] = _list[i] as int; } } _list[index] = value; pointer[index] = value as int; } @override int get length => _list.length; @override set length(int newLength) { throw Error(); } static fromList(List l) { final l2 = Uint8FlexList(); l2.list = l; return l2; } } Uint8FlexList readUint8Ptr(File file) { return Uint8FlexList.fromList(file.readAsBytesSync().cast()); }