在java中使用amf3input反序列化amf二进制格式

在java中使用amf3input反序列化amf二进制格式,java,serialization,amf,Java,Serialization,Amf,我正在尝试使用java中的flex.messaging.io.amf.Amf3Input反序列化amf二进制格式。但是没有找到反序列化的流程。有人能帮我给出一些步骤或代码片段吗? 我尝试使用下面的代码…但是readObject()返回null…请帮助 package amfnew; import java.net.ServerSocket; import java.net.Socket; import flex.messaging.io.SerializationContext;

我正在尝试使用java中的flex.messaging.io.amf.Amf3Input反序列化amf二进制格式。但是没有找到反序列化的流程。有人能帮我给出一些步骤或代码片段吗? 我尝试使用下面的代码…但是readObject()返回null…请帮助

 package amfnew;

  import java.net.ServerSocket;
  import java.net.Socket;
  import flex.messaging.io.SerializationContext;
  import flex.messaging.io.amf.Amf3Input;
  import flex.messaging.io.amf.Amf3Output;
  import java.io.BufferedInputStream;
  import java.io.ByteArrayInputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;

  public class MainAmf {
   byte[] read(String aInputFileName){
   File file = new File(aInputFileName);
byte[] result = new byte[(int)file.length()];

try {
  InputStream input = null;
  try {
    int totalBytesRead = 0;

    input = new BufferedInputStream(new FileInputStream(file));
    while(totalBytesRead < result.length){
      int bytesRemaining = result.length - totalBytesRead;

      int bytesRead = input.read(result, totalBytesRead, bytesRemaining); 
      if (bytesRead > 0){
        totalBytesRead = totalBytesRead + bytesRead;
      }
    }
  }
  finally {
    input.close();
  }
}
catch (FileNotFoundException ex) {      
}
catch (IOException ex) {

}
return result;
}

 public static void main(String[] args) throws FileNotFoundException {

    MainAmf ma= new MainAmf();
    byte[] amfBytes;
        amfBytes = ma.read("C:\\JavaApp\\AmfNew\\sample.amf");
    InputStream bais = new ByteArrayInputStream(amfBytes);   
Amf3Input amf3Input = new Amf3Input(SerializationContext.getSerializationContext());
amf3Input.setInputStream(bais);

    while(true)
    {
        try
        {

            Object obj = amf3Input.readObject();
            System.out.println("Reading..");
            System.out.println(obj);
            if(obj!=null)
            {
                System.out.println(obj.getClass());

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            break;
        }
    }

}

}
包amfnew;
导入java.net.ServerSocket;
导入java.net.Socket;
导入flex.messaging.io.SerializationContext;
导入flex.messaging.io.amf.Amf3Input;
导入flex.messaging.io.amf.amf3输出;
导入java.io.BufferedInputStream;
导入java.io.ByteArrayInputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.InputStream;
公共类维护{
字节[]读取(字符串aInputFileName){
文件文件=新文件(aInputFileName);
字节[]结果=新字节[(int)file.length()];
试一试{
InputStream输入=null;
试一试{
int totalBytesRead=0;
输入=新的BufferedInputStream(新文件输入流(文件));
while(totalBytesRead0){
totalBytesRead=totalBytesRead+bytesRead;
}
}
}
最后{
input.close();
}
}
catch(FileNotFoundException ex){
}
捕获(IOEX异常){
}
返回结果;
}
公共静态void main(字符串[]args)引发FileNotFoundException{
MainAmf ma=新的MainAmf();
字节[]amfBytes;
amfBytes=ma.read(“C:\\JavaApp\\AmfNew\\sample.amf”);
InputStream BAI=新的ByteArrayInputStream(amfBytes);
Amf3Input Amf3Input=新的Amf3Input(SerializationContext.getSerializationContext());
amf3Input.setInputStream(BAI);
while(true)
{
尝试
{
Object obj=amf3Input.readObject();
System.out.println(“读取…”);
系统输出打印项次(obj);
如果(obj!=null)
{
System.out.println(obj.getClass());
}
}
捕获(例外e)
{
e、 printStackTrace();
打破
}
}
}
}

我能让我的工作。这是一个AmfDeserializer:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Input;
import flex.messaging.messages.AcknowledgeMessage;

public class AmfDeserializer
{

   /**
    * @param args
    * @throws IOException 
    * @throws ClassNotFoundException 
    */
   public static void main( String[] args ) throws ClassNotFoundException, IOException
   {
      if(null == args || args.length != 1)
      {
         usage();
         System.exit( 1 );
      }

      File f = new File(args[0]);
      if(f.exists() && f.canRead())
      {
         Amf3Input deserializer = new Amf3Input(   SerializationContext.getSerializationContext() );
         deserializer.setInputStream( new FileInputStream( f ) );
         Object o = deserializer.readObject();

         if(o instanceof AcknowledgeMessage)
     {
            AcknowledgeMessage ack = (AcknowledgeMessage)o;
            System.out.println(ack.getCorrelationId());
            System.out.println(ack.getClientId());
            Object myObj = ack.getBody();
            System.out.println( ToStringBuilder.reflectionToString( myObj, ToStringStyle.SHORT_PREFIX_STYLE ) );
         }
      }
   }

   private static void usage()
   {
      System.out.println("args: absolute path to the .amf file");
   }
}