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等這些吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章