package org.jivesoftware.openfire.plugin.mongodb.file;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.ASObject;
import flex.messaging.io.amf.Amf3Input;
import flex.messaging.io.amf.Amf3Output;
/**
* AMF3 handle
*
* @author dhdu@qq.com
*
*/
public class Amf3 {
private final static Amf3 instance = new Amf3();
private static SerializationContext serializationContext;
//
private Amf3() {
}
//
public static Amf3 getInstance() throws Exception {
return instance;
}
static {
serializationContext = new SerializationContext();
}
public ASObject getASObject(HttpServletRequest request) {
ASObject aso=null;
try {
ServletInputStream in=request.getInputStream();
Amf3Input aip = new Amf3Input(serializationContext);
if (in != null) {
aip.setInputStream(in);
aso = (ASObject) aip.readObject();
//in.close();
aip.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return aso;
}
public void writeASObject(HttpServletResponse response,ASObject a){
Amf3Output amfOut=null;
try {
response.setContentType("application/x-gzip-compressed");
ServletOutputStream os=response.getOutputStream();
amfOut = new Amf3Output(serializationContext);
//ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//DataOutputStream dataOutStream = new DataOutputStream(outStream);
amfOut.setOutputStream(os);
amfOut.writeObject(a);
//byte[] bytes=outStream.toByteArray();
//os.write(bytes);
os.flush();
//os.close();
//outStream.flush();
//outStream.close();
//dataOutStream.flush();
//dataOutStream.close();
amfOut.flush();
amfOut.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}