Javolution
Javolution提供Struct和Union类具有直接使用C/C++应用程序的协同工作的能力.
Here is an example of C/C++ struct:
struct UDPMessage {
unsigned short year;
unsigned char month;
unsigned char day;
};
struct Student {
char name[64];
struct UDPMessage birth;
float grades[10];
Student* next;
};
and here is the Java equivalent using Javolution 2.2.5:
public static class Date extends Struct {
public final Unsigned16 year = new Unsigned16();
public final Unsigned8 month = new Unsigned8();
public final Unsigned8 day = new Unsigned8();
}
public static class Student extends Struct {
public final Utf8String name = new Utf8String(64);
public final Date birth = (Date) inner(new Date());
public final Float32[] grades = (Float32[]) array(new Float32[10]);
public final Reference32 next = new Reference32(Student.class);
}
public void testJavolution(){
UDPMessage message = new UDPMessage();
message.year.set(2);
message.month.set( (short) 3);
message.day.set((short) 5);
System.out.println(bytes2HexStr(message.getByteBuffer().array()," "));
byte[] bytes=new byte[4];
for(int i=0;i<4;i++){
bytes[i]=(byte) i;
}
message.setByteBuffer(ByteBuffer.wrap(bytes),0);
System.out.println(":: day="+message.day.get()+" month="+message.month.get()+" year="+message.year.get());
}
Multi-dimensional arrays of struct/union or of primitive types are also supported.
Struct API: http://javolution.org/api/javolution/io/Struct.html
jar download:
http://www.java2s.com/Code/Jar/j/Downloadjavolution541jar.htm
api doc:
http://javolution.org/target/site/apidocs/javolution/io/Struct.html