Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package siedleronlineproxy.network;
- import flex.messaging.io.MessageIOConstants;
- import flex.messaging.io.SerializationContext;
- import flex.messaging.io.amf.ActionMessage;
- import flex.messaging.io.amf.Amf0Input;
- import flex.messaging.io.amf.MessageBody;
- import flex.messaging.io.amf.MessageHeader;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import org.apache.commons.lang.ArrayUtils;
- /**
- *
- * @author nspecht
- */
- public class MyAMFDeserializer {
- private byte[] message = new byte[0];
- private ActionMessage am = null;
- public MyAMFDeserializer(String message) {
- this.message = message.getBytes();
- }
- public MyAMFDeserializer(byte[] message) {
- this.message = message;
- }
- public MyAMFDeserializer() {
- }
- public void add(byte[] mess,int length) {
- while (mess.length > length) {
- mess = ArrayUtils.remove(mess, mess.length-1);
- }
- this.message = ArrayUtils.addAll(this.message, mess);
- }
- public void readMessage() {
- try {
- byte[] mess;
- boolean removeHeader=true;
- if (removeHeader) {
- String tmp = new String(this.message);
- String pattern = "\r\n\r\n";
- int start = tmp.indexOf(pattern)+pattern.length();
- if (start>=tmp.length()) return;
- if (start > 0) {
- tmp = tmp.substring(start);
- }
- mess = tmp.getBytes();
- } else {
- mess = this.message;
- }
- if (mess.length > 0) {
- SerializationContext sc = SerializationContext.getSerializationContext();
- Amf0Input amf = new Amf0Input(sc);
- amf.setInputStream(new ByteArrayInputStream(mess));
- int amf_version = amf.readUnsignedShort();
- if (
- amf_version != MessageIOConstants.AMF0 &&
- amf_version != MessageIOConstants.AMF3
- ) return;
- this.am = new ActionMessage();
- this.am.setVersion(amf_version);
- int amf_headerCount = amf.readUnsignedShort();
- for(int i=0;i<amf_headerCount;i++) {
- this.am.addHeader(this.readHeader(amf,i));
- }
- int amf_bodyCount = amf.readUnsignedShort();
- for(int i=0;i<amf_bodyCount;i++) {
- this.am.addBody(this.readBody(amf,i));
- }
- }
- } catch (Exception e) {
- e.printStackTrace(System.out);
- this.am = null;
- } finally {
- }
- }
- private MessageHeader readHeader(Amf0Input amf,int index) throws IOException, ClassNotFoundException {
- amf.reset();
- System.out.println("parsing header "+index);
- String name = amf.readUTF();
- boolean mu = amf.readBoolean();
- //int length = amf.readInt();
- Object o = amf.readObject();
- MessageHeader ret = new MessageHeader();
- ret.setName(name);
- ret.setMustUnderstand(mu);
- ret.setData(o);
- return ret;
- }
- private MessageBody readBody(Amf0Input amf, int index) throws IOException, ClassNotFoundException {
- amf.reset();
- System.out.println("parsing body "+index);
- String tu = amf.readUTF();
- String ru = amf.readUTF();
- //int length = amf.readInt();
- Object o = amf.readObject();
- MessageBody ret = new MessageBody();
- ret.setTargetURI(tu);
- ret.setResponseURI(ru);
- ret.setData(o);
- return ret;
- }
- public ActionMessage getMessage() {
- return this.am;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment