I just wanted to pass abytearray to my JNI native code. i will have a
long, string or Calendar objects.
used the following method to create byte[]:
Perhaps you are trying to create something that looks like a C struct
in memory. Then once C has the address of it, it can go to town
without fooling around with JNI field by field access?
If that is true, if you are on a little-endian machine you can use
LEDatastream see
http://mindprod.com/products1.html#LEDATASTREAM
Write to a byte array by combining it with a ByteArrayOutputStream.
See
http://mindprod.com/applets/fileio.html for details. Tell it you
have little-endian binary data and want to write to a byte array.
For character data, write native platform-encoded bytes, null
terminated in fixed length fields.
If you don't have to worry about compatibility with older JVMs, you
can use nio instead of LEDataStream.
If your machine is big-endian, just tell the file i/o amanuensis you
have big endian binary data instead.
Similarly when you return, you can take the C struct apart again to
get the value in the fields using LEDataInputStream.
This technique should be very fast on the C side, with a fairly heavy
set up cost. The slightly ugly thing about it is if you implement it
on a variety of platforms, on the Java side you need a big and little
endian version, and if there are padding/alignment bytes in the C
struct, they too may be platform dependent. With normal JNI the Java
side is identical for all platforms. Only the C side changes.