dart ffi how to copy Uint8List to Pointer<Uint8>?

Viewed 579

I read a file var list = await file.readAsBytes(); and want to copy it to clang Pointer<Uint8> buf = malloc.allocate(list.length);. I know I can do it for (var i = 0; i < list.length; i++) buf.elementAt(i).value = list[i];. Is there anther method to do it just like clang memcpy?

1 Answers

Currently the code you have is what we support.

We would like to support giving access to the contents of Uint8List to native code in leaf FFI calls (GitHub issue), then the memcpy function can be used in native code to do the copying.

Alternatively, we could add support in dart:ffi itself for copying between Pointer and TypedData (GitHub issue).

As a workaround if you're blocked by the speed, you could use dart_api.c (in Dart standalone only, not Flutter):

DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object,
                                                  Dart_TypedData_Type* type,
                                                  void** data,
                                                  intptr_t* len);

You can make pass the TypedData through dart:ffi to native to get a Dart_Handle and then do a memcopy.

Related