`
AquariusM
  • 浏览: 146766 次
  • 性别: Icon_minigender_1
  • 来自: 南阳
社区版块
存档分类
最新评论

2010年8月27号---RedDwarf服务端与as数据通讯时byte array类型到其他类型数据转换

阅读更多

要搞这个原因是:

RedDwarf平台在服务端手法数据都是基于字节数组类型的,这样就需要做一个byte数组到基本数据类型的转换。

用到的技术主要有MINA和AMF,先来介绍一下AMF:

 

Action Message Format (AMF) is a binary format used to serialize ActionScript objects. It is used primarily to exchange data between an Adobe Flash application and a remote service, usually over the internet.

 

Typical usage from Adobe Flash Player, consists of an ActionScript program which:[citation needed]

  1. Connects to a specific "gateway" URL on a web server
  2. Accesses the service which handles AMF communication
  3. Calls a method on the service, mentioning a "callback" method
  4. Arguments passed are serialized to AMF and deserialized at the receiving end
  5. The service processes the input, and optionally returns data via AMF
  6. The callback method is invoked by the platform, and returned data is passed                                     
the pragraph below is used to describe the new characteristic which is about some new type in the
ActionScript 3.0. and this content is come from the amf3_spec_121207.pdf

    ActionScript 3.0 introduced a new type to support the manipulation of raw data in the form of an 
Array of bytes, namely flash.utils.ByteArray. To assist with ActionScript Object serialization and 
copying, ByteArray implements flash.utils.IDataInput and flash.utils.IDataOutput. These interfaces 
specify utility methods that help write common types to byte streams. Two methods of interest are 
IDataOutput.writeObject and IDataInput.readObject. These methods encode objects using AMF.
 The version of AMF used to encode object data is controlled by the ByteArray.objectEncoding method, which can be set to either AMF 3 or AMF 0. An enumeration type, flash.net.ObjectEncoding, holds the constants for the versions of AMF - ObjectEncoding.AMF0 and ObjectEncoding.AMF3 respectively.
     Note that ByteArray.writeObject uses one version of AMF to encode the entire object. Unlike 
NetConnection, ByteArray does not start out in AMF 0 and switch to AMF 3 (with the objectEncoding 
property set to AMF 3). Also note that ByteArray uses a new set of implicit reference tables for objects, 
object traits and strings for each readObject and writeObject call.

   根据pdf所介绍,flash.utils.ByteArray 用来协助实现Action Script 对象的序列化和拷贝。该类实现了flash.utils.IDataInput 和flash.utils.IDataOutput接口。实现了它们的writeObject和readObject方法。这两个方法使用的对象就是使用AMF0或者是AMF3进行编码的。

 

     And now the case is that i should convert byte array to the xml type data. I can use the MINA 's 
IoBuffer. org.apache.mina.core.buffer.IoBuffer, it has a function: 
public static IoBuffer wrap(byte[] byteArray). And use it you can make the byte array which received
 from client . After that, use the IoBuffer.asInputStream() convert the it to inputstream . And now you can
use openAMF's XMLUtils.convertToDOM(inputStream). and this function return a DOM which is one type
of org.w3c.dom. It is like that :org.w3c.dom.Document dom = XMLUtils.convertToDOM(inputstream);

   也就是说,用MINA的IoBuffer.wrap(Byte []: org0)可以将从客户端过来的byte数组类型的数值压缩到IoBuffer的ByteBuffer 中,然后利用IoBuffer的实例方法: as InputStream()将byteBuffer 转换为输入流。最后利用openAMF的XMLUtils.convertToDOM(inputStream)方法将输入流转换为DOM树。这样就完成了转换。

 

   利用AMF的好处在于RedDwarf的数据传递时利用Byte数组传递的。但是按照我这样的搞法,最后还是在用xml呀,所以这一点比较纠结,现在先这样吧,至少这样是可以实现的,就行而且基本上这些数据转换也摸了一遍了,下次就会好很多了,只是把xml换为可能json等这些吧。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics