Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C struct in Java

Tags:

java

c

I have to code a Java program that will receive messages from network and display their content to the user. The problem is that the messages that I receive are simply binary dumps of C structures. Add to this some of the messages are coming from little endian machines and some from big endian without the fields being converted to network byte order. One way I have is to use JNI and convert c structs to some XML string and then de serialize this XML string to a Java Object. This is a laborous job since there about 122 different structs and each one of them contains more than 20 fields. I wonder if there is a library/tool/methodology which could make my job a bit easy ?

like image 446
Rohin Avatar asked Aug 17 '09 13:08

Rohin


1 Answers

There is a library called Preon that was designed to help you with this type of task: Preon site Basically, they try to keep all the logic for reading your pojo's from the binary stream in annotations tied to each field in your pojo.

An example from their docs where you control the size of the int you are reading:

class Rectangle
{
  @BoundNumber(size="16") private int x1;
  @BoundNumber(size="16") private int y1;
  @BoundNumber(size="16") private int x2;
  @BoundNumber(size="16") private int y2;
}

or to specify endianness:

class Rectangle
{
  @BoundNumber(byteOrder=LittleEndian) private int x1;
  @BoundNumber(byteOrder=LittleEndian) private int y1;
  @BoundNumber(byteOrder=LittleEndian) private int x2;  
  @BoundNumber(byteOrder=LittleEndian) private int y2;
}

You can even use mini-equations with references to values in previous fields to specify size / length, etc.

@BoundList(size="width * height") byte[] pixels;
@BoundNumber(size="nrBits * 2") int value;

Oh, and they also offer conditional logic, all in annotations.

like image 65
Trevor Harrison Avatar answered Sep 23 '22 22:09

Trevor Harrison