javastruct()关于java结构体
javastruct
A library to treat java objects as C structs.窗体顶端
窗体底端
HYPERLINK "/p/javastruct/"Project HomeHYPERLINK "/p/javastruct/downloads/list"DownloadsHYPERLINK "/p/javastruct/w/list"WikiHYPERLINK "/p/javastruct/issues/list"IssuesHYPERLINK "/p/javastruct/source/checkout"Source
Search 窗体顶端
for
窗体底端
HowToUseJavaStruct
This page explains how to use JavaStruct library. HYPERLINK "/p/javastruct/w/list?q=label:Featured"Featured, HYPERLINK "/p/javastruct/w/list?q=label:Phase-Implementation"Phase-Implementation
Updated Feb 4, 2010 by HYPERLINK "/u/102564017663053832542/"mda...@
Introduction
Struct classes can be used to greatly simplfy network protocol codes of Java applications when working with embedded devices and other applications which uses C style structs.
Instead of manually encoding and decoding messages, JavaStruct allows programmers to treat java classes as c structs.
JavaStruct uses Java 5 annotations to mark Classes and fields as structs. JavaStruct is not the first attempt to provide struct like functionality, Jean-Marie Dautelle's HYPERLINK "/"Javolution library also has an excellent HYPERLINK "/api/javolution/io/Struct.html"struct implementation. But instead of using special classes in Javolution, POJO approach is preferred JavaStruct.
General usage
JavaStruct fa?ade class is used to pack and unpack struct classes. Below is a simple unit test method for checking a struct class. Struct fields hasan order value, because Java JVM specification does not tell anything about order of the class members. They are ordered as their appearance in Sun's implementation but it differs on other JVM's. So every Struct field has to supply an order value.
@StructClasspublic class Foo{ @StructField(order = 0) public byte b; @StructField(order = 1) public int i;}try{ // Pack the class as a byte buffer Foo f = new Foo(); //媒体处理器 f.b = (byte)1; f.i = 1; byte[] b = JavaStruct.pack(f); // Unpack