How can i pass a byte[] data from foreign code (java) to uno method?

Here is what i wanna do:

[Foreign(Language.Java)]
public static extern(Android) void Init()
{
    // ...
    byte[] data = getData();
    @{Process(sbyte[]):Call(data)};
}

[Foreign(Language.Java)]
public static extern(Android) void Process(sbyte[] data)
{
    // work with data
}

And then it notified this error: byte[] cannot be converted to ByteArray

Hi David, thanks for posting

You are right there is a gap in support here.

I have raised a ticket to fix this and will get on it as soon as I can.

The reason we box uno arrays when passing to java is for performance. We want to avoid unnecessary data copying by default and so we box into a com.uno._Type_Array that boxed array has accessors and a copyArray() method which will actually duplicate the data.

Like you worked out, currently foreign code is expecting a com.uno.ByteArray to be passed to uno. I’m now wondering if maybe we allow both this and java arrays, and just take the performance hit when we pass down the java array.

Thanks again, I’ll be in contact when there is a fix.

Hi Chris,

Can you please advise on how should the array be passed/received?

I am using (not working):

from Java:

@{Camera:Of(_this).saveLastPicture(sbyte[]):Call(new com.uno.ByteArray(data))};

where data is a byte[]

to Uno:

  public void saveLastPicture(sbyte[] newData){
    newData.copyArray(lastPicture);
    debug_log("Saved array has length: " + lastPicture.Length);
  }

to copy newData into lastPicture.
lastPicture is an sbyte[]

Also, how to pass the sbyte[] from Uno to Java and use it in Java as byte[]:

[Foreign(Language.Java)]
void JavaMethod(sbyte[] lastPicture)
@{
     byte[] <= lastPicture

Thanks in advance for your support

Hello, here is a sample project reproducing the error (to be compiled/run on Android only) : https://gist.github.com/anonymous/64a7200531d665799351da916788ce58

which is:
UnoFileJS.uno(56.15): E3104: ‘sbyte[]’ does not contain a member called ‘copyArray’. Could you be missing a package reference?

Ah I think I have confused things :slight_smile: copyArray is a method on the ByteArray java object.

[Foreign(Language.Java)]
public void TestByteArray(byte[] foo)
@{
    // here foo is a com.uno.ByteArray. This is a java box
    // around the Uno byte[]
    
    byte x = foo.get(0);
     // here we get the first byte from the array. This calls
     // down to uno to read the uno array so far we have not
     // copied any data

    foo.set(0, 3);
    // now we set the first byte in the uno array. This
    // modified the uno array
    
    byte[] bar = foo.copyArray();
    // At this point we copy the entire array into Java memory.
    // making changes to bar will not modify the uno array
@}

Does this help?

To those who stumble onto this thread. If you wish to make an uno int array from java you can now simply call. new com.uno.IntArray(lengthVar);. For more details see here: https://www.fusetools.com/docs/native-interop/foreign-code#arrays