- 5
- 0
- 约6.7千字
- 约 5页
- 2017-11-20 发布于北京
- 举报
javastruct关于java结构体
javastruct
A library to treat java objects as C structs. 窗体顶端
窗体底端
Project HomeDownloadsWikiIssuesSource
Search 窗体顶端
for
窗体底端
HowToUseJavaStruct
This page explains how to use JavaStruct library. Featured, Phase-Implementation
Updated Feb 4, 2010 by 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 Dautelles Javolution library also has an excellent 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 Suns implementation but it differs on other JVMs. 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 it into an object Foo f2 = new Foo(); JavaStruct.unpack(f2, b);}catch(StructException e){}
Struct operations throws a checked StructException if anything goes wrong.
Struct classes can be used with Streams directly too. Please refer to Photoshop ACB file reader example.
public void read(String acbFile){ try { FileInputStream fis = new FileInputStream(new File(acbFile));
原创力文档

文档评论(0)