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