I'm using JNA and I get a strange error getting a byte array.
I use this code:
PointerByReference mac=new PointerByReference();
NativeInterface.getMac(mac);
mac.getPointer().getByteArray(0,8)
And it throws a IndexOutOfBoundsException: Bounds exceeds available space : size=4, offset=8 also if I'm sure thate the byte returned is a 8byte length. I tried to get that array as String:
mac.getPointer().getString(0)
And here I get successfully a String 8 chars lenght. Can you understand why?
Thank you.
PointerByReference.getValue()
returns the Pointer
you're looking for. PointerByReference.getPointer()
returns its address.
mac.getPointer().getByteArray(0, 8)
is attempting to read 8 bytes from the PointerByReference
allocated memory (which is a pointer), and put those bytes into a Java primitive array. You're asking for 8 bytes but there are only 4 allocated, thus the corresponding error.
mac.getPointer().getString(0)
is attempting to read a C string from the memory allocated for a pointer value (as if it were const char *
, and convert that C string into a Java String
. It only bounds-checks the start of the string on the Java side, so it will keep reading memory (even if it is technically out of bounds) until it finds a zero value.
EDIT
mac.getValue().getByteArray(0, 8)
will give you what you were originally trying to obtain (an array of 8 bytes).
EDIT
If your called function is supposed to be writing to a buffer (and not writing the address of a buffer), then you should change its signature to accept byte[]
instead, e.g.
byte[] buffer = new byte[8];
getMac(buffer);