




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
javastructA library to treat java objects as C structs.窗体顶端 窗体底端Project HomeDownloadsWikiIssuesSourceSearch 窗体顶端 for 窗体底端HowToUseJavaStruct This page explains how to use JavaStruct library. Featured, Phase-ImplementationUpdated Feb 4, 2010 by IntroductionStruct 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 usageJavaStruct faade 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); header = new ACBHeader(); StructUnpacker up = JavaStruct.getUnpacker(fis, ByteOrder.BIG_ENDIAN); up.readObject(header);.PrimitivesUsing primitives. Note that private and protected fields requires appropriate getter and setter methods. Transient fields are automatically excluded.StructClasspublic class PublicPrimitives implements Serializable StructField(order = 0) public byte b; StructField(order = 1) public char c; StructField(order = 2) public short s; StructField(order = 3) public int i; StructField(order = 4) public long lo; StructField(order = 5) protected float f; StructField(order = 6) private double d; transient int blah; transient double foo; public float getF() return f; public void setF(float f) this.f = f; public double getD() return d; public void setD(double d) this.d = d; public boolean equals(Object o) PublicPrimitives other = (PublicPrimitives)o; return (this.b = other.b & this.c = other.c & this.s = other.s & this.i = other.i & this.lo = other.lo & this.f = other.f & this.d = other.d); ArraysArrays have some prerequisites. When unpacking, sufficent space should be reserved in the array. Only arrays, whose lengths are defined in another field using ArrayLengthMarker (see below section) can be null, they are automatically allocated while unpacking. Other than that they can not be null and uninitialized.StructClasspublic class PublicPrimitiveArrays StructField(order = 0) public byte b = new byte5; StructField(order = 1) public char c = new byte10; StructField(order = 2) public short s; StructField(order = 3) public int i;Array Length MarkersArray length markers are very useful for the fields whose legth is defined in anoher field. consider the following example. This is a special String struct which has a length fields and length number of 16 bit characters following it. +-+-/-+ | Length | UTF-16 Characters | +-+-/-+In order to process this, we have to represent these strings as a special Struct class, lets say AStruct. The length field should be annotated as ArrayLengthMarker. This way javastruct can automatically use the value in length field while processing the array field during packing and unpacking operations.StructClasspublic class AString StructField (order = 0 ) ArrayLengthMarker (fieldName = chars) public int length; StructField (order = 1) public char chars; public AString(String content) this.length = content.length(); this.chars = content.toCharArray(); .Comment by , Dec 19, 2011Thanks for this example. But how to read a struct from buffer ?Comment by , Feb 10, 2012Is there anything special that needs to be done to allow this to work with 2D arrays?Here is the object that I am trying to packStructClasspublic class SEND_DATA_STRUCTURE StructField(order = 0) public float gyro_offset = new float3; StructField(order = 1) public float accel_offset = new float3; StructField(order = 2) public float MagCalRotationMat = new float33; StructField(order = 3) public float MagCalScaleMat = new float33; StructField(order = 4) public float MagCalShiftMat = new float13; Here is the code Im using to pack it:byte b; SEND_DATA_STRUCTURE txData = new SEND_DATA_STRUCTURE(); b = JavaStruct.pack(txData,ByteOrder.LITTLE_ENDIAN);and this is the runtime error that I get: struct.StructException?: struct.StructException?: No struct Annotation found for FIf I get rid of the 2D arrays, I dont get that exception.Comment by , Feb 6, 2013What if there are multiple fields in the
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025届广东省深圳市龙岗区中考试题猜想数学试卷含解析
- 2025年工业机器人编程与操作习题及解析
- 2025年应急局安全员资格认证题解
- 2025年建安安全员考试重点突破题及答案
- 2025年安全员C证法规题及答案
- 2025年空调维修工程师执业技能考核试题及答案解析
- 2025年金融风险管理师职业资格考试试卷及答案解析
- 2025年建筑学试题及答案解析
- 2025年婚姻家庭咨询师职业素质评定试题及答案解析
- 2025年国际贸易专家认证考核试题及答案解析
- 军队文职管理学备考指南
- 胖东来考试试题及答案
- 乐天地产(成都)有限公司乐天广场四期项目环评报告
- 人教版初二地理上册课件:从世界看中国第一节 疆域
- 初中生叛逆期教育主题班会
- 《农村基层干部廉洁履行职责规定》知识培训
- 符合标准2025年乡村全科助理医师考试试题及答案
- 2025年矿产权评估师练习题及参考答案一套
- 人工智能技术在中职语文教学中的实践
- 中职新能源汽车专业实训虚实结合教学模式创新研究
- 2025年《新课程标准解读》标准课件
评论
0/150
提交评论