3 messages in net.java.dev.jna.usersRe: [jna-users] Converting byte[] to ...
FromSent OnAttachments
Kunal ShahOct 24, 2008 8:06 am 
Timothy WallOct 24, 2008 11:48 am 
Kunal ShahOct 24, 2008 1:18 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Converting byte[] to Structure ReferenceActions...
From:Kunal Shah (kuna@gmail.com)
Date:Oct 24, 2008 1:18:06 pm
List:net.java.dev.jna.users

Thanks for reply. This is what I have resolved it:

In Java: byte[] data = new byte[allocLength]; ... update data ... MessageStructure mm = new MessageStructure(); mm.getPointer().write(0, data, 0, mm.size()); mm.read();

I believe this is a proper way of doing it, please complain if it's not.

On Fri, Oct 24, 2008 at 1:48 PM, Timothy Wall <twal@dev.java.net>wrote:

On Oct 24, 2008, at 11:07 AM, Kunal Shah wrote:

I am receiving a byte[] which is essentially a Structure reference.

Is there any reason you need the type byte[]?

Structure.getPointer() returns the address of the structure's allocated memory. If you want to allocate it yourself (if, for instance, your Structure is of variable size), you can allocate Memory and use Structure.useMemory to point the Structure at it. This is typically done in the Structure ctor.

Note that you can always make multiple function mappings with appropriately typed arguments. For example:

void populateBuffer(byte[] buf); void populateBuffer(MyStructure s);

can both be mapped onto a native function with the signature:

void populateBuffer(void *buf);

It's generally better to introduce type safety rather than copying bad practices from C like casting between integers and pointers or different pointer types.

In C: char *data = new char[allocLength]; ... update data ... MESSAGE *rec = (MESSAGE *)data;

printf("status: %d\n", rec->status);

Where MESSAGE is a simple structure.

In Java: byte[] data = new byte[allocLength]; ... update data ... Now what: How can I convert a "byte[]" into "MessageStructure.ByReference". And then how to read data out of it? I would appreciate actual code.

I am open for other options where I can use other than byte[] .... ByteBuffer or Memory

Thanks, Kunal