




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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年心理学护理考点归纳题库及答案
- DB51-T 3294-2025 体育场馆病媒生物综合管理技术规范
- 情景模拟题面试题及答案
- 广告画设计考试题及答案
- 2025年大学计算机等级考试二级Python操作系统原理专项训练试卷
- 2025年经济师考试 宏观经济真题解析与模拟试卷
- 美白护肤品包装设计大赛企业制定与实施新质生产力项目商业计划书
- 混凝土桥面铺装施工安全技术保证措施
- 垃圾知识分类培训课件
- 2025-2026学年北师大版二年级上册第二单元《测量(一)》测试卷 及答案(三套)
- 北京市大兴区2024-2025学年高二上学期期中检测数学试题(解析版)
- 汇川PLC培训课件
- 中建二测考试真题及答案
- 矿业权评估全参数确定指导意见
- 2025贵州民航产业集团有限公司招聘120人考试参考试题及答案解析
- XJJ 077-2017 高性能混凝土应用技术规程
- 智能零售门店智能照明系统维护方案
- 员工股权激励分红协议
- 2025年化学检验工(高级技师)职业技能鉴定真题试卷(附答案)
评论
0/150
提交评论