openamf/src/java/org/openamf/io AMFDeserializer.java,1.32,1.33 AMFSerializer.java,1.37,1.38

[email protected] Sat, 16 Aug 2003 06:11:18 -0700
Newsgroups gmane.comp.java.openamf.cvs
Message-ID <[email protected]>
Update of /cvsroot/openamf/openamf/src/java/org/openamf/io
In directory sc8-pr-cvs1:/tmp/cvs-serv26881/java/org/openamf/io

Modified Files:
	AMFDeserializer.java AMFSerializer.java 
Log Message:
Serializer/Deserializer cleanup

Index: AMFDeserializer.java
===================================================================
RCS file: /cvsroot/openamf/openamf/src/java/org/openamf/io/AMFDeserializer.java,v
retrieving revision 1.32
retrieving revision 1.33
diff -C2 -d -r1.32 -r1.33
*** AMFDeserializer.java	15 Aug 2003 22:40:41 -0000	1.32
--- AMFDeserializer.java	16 Aug 2003 13:11:16 -0000	1.33
***************
*** 14,20 ****
  import java.util.List;
  
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- 
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
--- 14,17 ----
***************
*** 23,69 ****
  import org.openamf.ASObject;
  import org.openamf.config.OpenAMFConfig;
! import org.w3c.dom.Document;
! import org.xml.sax.InputSource;
  
  /**
   * @author Jason Calabrese <[email protected]>
   * @author Pat Maddox <[email protected]>
   * @version $Revision$, $Date$
   */
  public class AMFDeserializer {
  
! 	private static Log log = LogFactory.getLog(AMFDeserializer.class);
  
! 	private List storedObjects = null;
  
! 	/**
       * The AMF input stream
       */
! 	protected DataInputStream inputStream;
  
      /**
       * Number of headers in the packet
       */
! 	protected int headerCount;
  
! 	/**
       * Content of the headers
       */
! 	protected List headers = new ArrayList();
  
      /**
       * Number of bodies
       */
! 	protected int bodyCount;
  
      /**
       * Content of the bodies
       */
! 	protected List bodies = new ArrayList();
  
! 	/**
       * Object to store the deserialized data
       */
! 	protected AMFMessage message = new AMFMessage();
      /**
       * Deserialize message
--- 20,69 ----
  import org.openamf.ASObject;
  import org.openamf.config.OpenAMFConfig;
! import org.openamf.util.XMLUtils;
  
  /**
+  * AMF Deserializer
+  *
   * @author Jason Calabrese <[email protected]>
   * @author Pat Maddox <[email protected]>
+  * @author Sylwester Lachiewicz <[email protected]>
   * @version $Revision$, $Date$
   */
  public class AMFDeserializer {
  
!     private static Log log = LogFactory.getLog(AMFDeserializer.class);
  
!     private List storedObjects = null;
  
!     /**
       * The AMF input stream
       */
!     protected DataInputStream inputStream;
  
      /**
       * Number of headers in the packet
       */
!     protected int headerCount;
  
!     /**
       * Content of the headers
       */
!     protected List headers = new ArrayList();
  
      /**
       * Number of bodies
       */
!     protected int bodyCount;
  
      /**
       * Content of the bodies
       */
!     protected List bodies = new ArrayList();
  
!     /**
       * Object to store the deserialized data
       */
!     protected AMFMessage message = new AMFMessage();
! 
      /**
       * Deserialize message
***************
*** 72,92 ****
       * @throws IOException
       */
! 	public AMFDeserializer(DataInputStream inputStream) throws IOException {
! 		if (!log.isDebugEnabled()) {
! 			log.info("Deserializing Message, for more info turn on debug level");
! 		}
! 		// Save the input stream for this object
! 		this.inputStream = inputStream;
! 		// Read the binary header
! 		readHeaders();
! 		log.debug("readHeader");
! 		// Read the binary body
! 		readBodies();
! 		log.debug("readBody");
! 	}
  
- 	public AMFMessage getAMFMessage() {
- 		return message;
- 	}
      /**
       * Read message header
--- 72,93 ----
       * @throws IOException
       */
!     public AMFDeserializer(DataInputStream inputStream) throws IOException {
!         if (!log.isDebugEnabled()) {
!             log.info("Deserializing Message, for more info turn on debug level");
!         }
!         // Save the input stream for this object
!         this.inputStream = inputStream;
!         // Read the binary header
!         readHeaders();
!         log.debug("readHeader");
!         // Read the binary body
!         readBodies();
!         log.debug("readBody");
!     }
! 
!     public AMFMessage getAMFMessage() {
!         return message;
!     }
  
      /**
       * Read message header
***************
*** 94,121 ****
       * @throws IOException
       */
! 	protected void readHeaders() throws IOException {
! 		// version
! 		message.setVersion(inputStream.readUnsignedShort());
! 		// Find total number of header elements
! 		headerCount = inputStream.readUnsignedShort();
! 		log.debug("headerCount = " + headerCount);
  
- 		// Loop over all the header elements
- 		for (int i = 0; i < headerCount; i++) {
- 			//clear storedObjects
- 			storedObjects = new ArrayList();
- 			String key = inputStream.readUTF();
- 			// Find the must understand flag
- 			boolean required = inputStream.readBoolean();
- 			// Grab the length of the header element
- 			long length = inputStream.readInt();
- 			// Grab the type of the element
- 			byte type = inputStream.readByte();
- 			// Turn the element into real data
- 			Object value = readData(type);
- 			// Save the name/value into the headers array
- 			message.addHeader(key, required, value);
- 		}
- 	}
      /**
       * Read message body
--- 95,122 ----
       * @throws IOException
       */
!     protected void readHeaders() throws IOException {
!         // version
!         message.setVersion(inputStream.readUnsignedShort());
!         // Find total number of header elements
!         headerCount = inputStream.readUnsignedShort();
!         log.debug("headerCount = " + headerCount);
!         // Loop over all the header elements
!         for (int i = 0; i < headerCount; i++) {
!             // clear storedObjects - references are new for every header
!             storedObjects = new ArrayList();
!             String key = inputStream.readUTF();
!             // Find the must understand flag
!             boolean required = inputStream.readBoolean();
!             // Grab the length of the header element
!             long length = inputStream.readInt();
!             // Grab the type of the element
!             byte type = inputStream.readByte();
!             // Turn the element into real data
!             Object value = readData(type);
!             // Save the name/value into the headers array
!             message.addHeader(key, required, value);
!         }
!     }
  
      /**
       * Read message body
***************
*** 123,150 ****
       * @throws IOException
       */
! 	protected void readBodies() throws IOException {
! 		// Find the total number of body elements
! 		bodyCount = inputStream.readUnsignedShort();
! 		log.debug("bodyCount = " + bodyCount);
  
- 		// Loop over all the body elements
- 		for (int i = 0; i < bodyCount; i++) {
- 			//clear storedObjects
- 			storedObjects = new ArrayList();
-             // The target method
- 			String method = inputStream.readUTF();
- 			// The target that the client understands
- 			String target = inputStream.readUTF();
- 			// Get the length of the body element
- 			long length = inputStream.readInt();
- 			// Grab the type of the element
- 			byte type = inputStream.readByte();
- 			log.debug("type = " + type);
- 			// Turn the argument elements into real data
- 			Object data = readData(type);
- 			// Add the body element to the body object
- 			message.addBody(method, target, data, type);
- 		}
- 	}
      /**
       * Reads custom class
--- 124,152 ----
       * @throws IOException
       */
!     protected void readBodies() throws IOException {
!         // Find the total number of body elements
!         bodyCount = inputStream.readUnsignedShort();
!         log.debug("bodyCount = " + bodyCount);
! 
!         // Loop over all the body elements
!         for (int i = 0; i < bodyCount; i++) {
!             //clear storedObjects
!             storedObjects = new ArrayList();
! // The target method
!             String method = inputStream.readUTF();
!             // The target that the client understands
!             String target = inputStream.readUTF();
!             // Get the length of the body element
!             long length = inputStream.readInt();
!             // Grab the type of the element
!             byte type = inputStream.readByte();
!             log.debug("type = " + type);
!             // Turn the argument elements into real data
!             Object data = readData(type);
!             // Add the body element to the body object
!             message.addBody(method, target, data, type);
!         }
!     }
  
      /**
       * Reads custom class
***************
*** 153,173 ****
       * @throws IOException
       */
! 	protected Object readCustomClass() throws IOException {
! 		// Grab the explicit type - somehow it works
! 		String type = inputStream.readUTF();
! 		log.debug("Reading Custom Class: " + type);
! 		String mappedJavaClass = OpenAMFConfig.getInstance().getJavaClassName(type);
! 		if (mappedJavaClass != null) {
! 			type = mappedJavaClass;
! 		}
! 		ASObject aso = new ASObject(type);
! 		// The rest of the bytes are an object without the 0x03 header
! 		return readObject(aso);
! 	}
  
- 	protected ASObject readObject() throws IOException {
- 		ASObject aso = new ASObject();
- 		return readObject(aso);
- 	}
      /**
       * Reads an object and converts the binary data into an List
--- 155,176 ----
       * @throws IOException
       */
!     protected Object readCustomClass() throws IOException {
!         // Grab the explicit type - somehow it works
!         String type = inputStream.readUTF();
!         log.debug("Reading Custom Class: " + type);
!         String mappedJavaClass = OpenAMFConfig.getInstance().getJavaClassName(type);
!         if (mappedJavaClass != null) {
!             type = mappedJavaClass;
!         }
!         ASObject aso = new ASObject(type);
!         // The rest of the bytes are an object without the 0x03 header
!         return readObject(aso);
!     }
! 
!     protected ASObject readObject() throws IOException {
!         ASObject aso = new ASObject();
!         return readObject(aso);
!     }
  
      /**
       * Reads an object and converts the binary data into an List
***************
*** 177,205 ****
       * @throws IOException
       */
! 	protected ASObject readObject(ASObject aso) throws IOException {
! 		storeObject(aso);
! 		// Init the array
! 		log.debug("reading object");
! 		// Grab the key
! 		String key = inputStream.readUTF();
! 		for (byte type = inputStream.readByte();
! 			type != 9;
! 			type = inputStream.readByte()) {
! 			// Grab the value
! 			Object value = readData(type);
! 			// Save the name/value pair in the map
! 			if (value == null) {
! 				log.info("Skipping NULL value for :" + key);
! 			} else {
! 				aso.put(key, value);
! 				log.debug(" adding {key=" + key + ", value=" + value + ", type=" + type + "}");
! 			}
! 			// Get the next name
! 			key = inputStream.readUTF();
! 		}
! 		log.debug("finished reading object");
! 		// Return the map
! 		return aso;
! 	}
      /**
       * Reads array
--- 180,209 ----
       * @throws IOException
       */
!     protected ASObject readObject(ASObject aso) throws IOException {
!         storeObject(aso);
!         // Init the array
!         log.debug("reading object");
!         // Grab the key
!         String key = inputStream.readUTF();
!         for (byte type = inputStream.readByte();
!              type != 9;
!              type = inputStream.readByte()) {
!             // Grab the value
!             Object value = readData(type);
!             // Save the name/value pair in the map
!             if (value == null) {
!                 log.info("Skipping NULL value for :" + key);
!             } else {
!                 aso.put(key, value);
!                 log.debug(" adding {key=" + key + ", value=" + value + ", type=" + type + "}");
!             }
!             // Get the next name
!             key = inputStream.readUTF();
!         }
!         log.debug("finished reading object");
!         // Return the map
!         return aso;
!     }
! 
      /**
       * Reads array
***************
*** 208,230 ****
       * @throws IOException
       */
! 	protected List readArray() throws IOException {
! 		// Init the array
! 		List array = new ArrayList();
! 		storeObject(array);
! 		log.debug("Reading array");
! 		// Grab the length of the array
! 		long length = inputStream.readInt();
! 		log.debug("array length = " + length);
! 		// Loop over all the elements in the data
! 		for (long i = 0; i < length; i++) {
! 			// Grab the type for each element
! 			byte type = inputStream.readByte();
! 			// Grab the element
! 			Object data = readData(type);
! 			array.add(data);
! 		}
! 		// Return the data
! 		return array;
! 	}
      /**
       * Store object in  internal array
--- 212,235 ----
       * @throws IOException
       */
!     protected List readArray() throws IOException {
!         // Init the array
!         List array = new ArrayList();
!         storeObject(array);
!         log.debug("Reading array");
!         // Grab the length of the array
!         long length = inputStream.readInt();
!         log.debug("array length = " + length);
!         // Loop over all the elements in the data
!         for (long i = 0; i < length; i++) {
!             // Grab the type for each element
!             byte type = inputStream.readByte();
!             // Grab the element
!             Object data = readData(type);
!             array.add(data);
!         }
!         // Return the data
!         return array;
!     }
! 
      /**
       * Store object in  internal array
***************
*** 232,276 ****
       * @param o
       */
! 	private void storeObject(Object o) {
! 		storedObjects.add(o);
! 		log.debug("storedObjects.size: " + storedObjects.size());
! 	}
!     /**
!      * Reads date
!      *
!      * @return
!      * @throws IOException
!      */
! 	protected Date readDate() throws IOException {
! 		long ms = (long)inputStream.readDouble(); // Date in millis from 01/01/1970
! 		int timeoffset = inputStream.readUnsignedShort();
! 		if (timeoffset > 720) {
! 			// I haven't tested this
! 			timeoffset = - (65536 - timeoffset);
! 		}
! 		ms += timeoffset;
! 		Date date = new Date(ms);
  
- 		return date;
- 	}
      /**
!      * Reads XML document
       *
       * @return
       * @throws IOException
       */
! 	protected Document readXML() throws IOException {
! 		Document document = null;
! 		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  
- 		inputStream.skip(4); // skip length
- 		try {
- 			DocumentBuilder builder = factory.newDocumentBuilder();
- 			document = builder.parse(new InputSource(inputStream));
- 		} catch (Exception e) {
- 			throw new IOException("Error while parsing xml: "+e.getMessage());
- 		}
- 		return document;
- 	}
      /**
       * Reads flushed stored object
--- 237,263 ----
       * @param o
       */
!     private void storeObject(Object o) {
!         storedObjects.add(o);
!         log.debug("storedObjects.size: " + storedObjects.size());
!     }
  
      /**
!      * Reads date
       *
       * @return
       * @throws IOException
       */
!     protected Date readDate() throws IOException {
!         long ms = (long) inputStream.readDouble(); // Date in millis from 01/01/1970
!         int timeoffset = inputStream.readUnsignedShort();
!         if (timeoffset > 720) {
!             // I haven't tested this
!             timeoffset = -(65536 - timeoffset);
!         }
!         ms += timeoffset;
!         Date date = new Date(ms);
!         return date;
!     }
  
      /**
       * Reads flushed stored object
***************
*** 279,287 ****
       * @throws IOException
       */
! 	protected Object readFlushedSO() throws IOException {
! 		int index = inputStream.readUnsignedShort();
! 		log.debug("Object Index: " + index);
! 		return storedObjects.get(index);
! 	}
      /**
       * Reads object
--- 266,275 ----
       * @throws IOException
       */
!     protected Object readFlushedSO() throws IOException {
!         int index = inputStream.readUnsignedShort();
!         log.debug("Object Index: " + index);
!         return storedObjects.get(index);
!     }
! 
      /**
       * Reads object
***************
*** 289,295 ****
       * @return
       */
! 	protected Object readASObject() {
! 		return null;
! 	}
      /**
       * Reads object from inputstream with selected type
--- 277,284 ----
       * @return
       */
!     protected Object readASObject() {
!         return null;
!     }
! 
      /**
       * Reads object from inputstream with selected type
***************
*** 299,355 ****
       * @throws IOException
       */
! 	protected Object readData(byte type) throws IOException {
! 		Object data = null;
! 		log.debug("Reading data of type: " + type);
! 		switch (type) {
! 			case AMFBody.DATA_TYPE_NUMBER :
! 				data = new Double(inputStream.readDouble());
! 				break;
! 			case AMFBody.DATA_TYPE_BOOLEAN :
! 				data = new Boolean(inputStream.readBoolean());
! 				break;
! 			case AMFBody.DATA_TYPE_STRING :
! 				data = inputStream.readUTF();
! 				break;
!             case AMFBody.DATA_TYPE_STRING_LONG :
!                 // test this
                  int utflen = inputStream.readInt();
!                 data = inputStream.readUTF();
!                 break;
! 			case AMFBody.DATA_TYPE_OBJECT :
! 				data = readObject();
! 				break;
! 			case AMFBody.DATA_TYPE_NULL :
!             case AMFBody.DATA_TYPE_NULL2 :
! 				data = null;
! 				break;
! 			case AMFBody.DATA_TYPE_FLUSHED_SHARED_OBJECT :
! 				data = readFlushedSO();
! 				break;
! 			case AMFBody.DATA_TYPE_MIXED_ARRAY :
! 				long length = inputStream.readInt();
! 				//don't do anything with the length
! 				data = readObject();
! 				break;
! 			case AMFBody.DATA_TYPE_ARRAY :
! 				data = readArray();
! 				break;
! 			case AMFBody.DATA_TYPE_DATE :
! 				data = readDate();
! 				break;
! 			case AMFBody.DATA_TYPE_AS_OBJECT :
! 				data = readASObject();
! 				break;
! 			case AMFBody.DATA_TYPE_XML :
! 				data = readXML();
! 				break;
! 			case AMFBody.DATA_TYPE_CUSTOM_CLASS :
! 				data = readCustomClass();
! 				break;
! 			default :
! 				data = null;
! 				break;
! 		}
! 		return data;
! 	}
  }
--- 288,335 ----
       * @throws IOException
       */
!     protected Object readData(byte type) throws IOException {
!         log.debug("Reading data of type " + AMFBody.getObjectTypeDescription(type));
!         switch (type) {
!             case AMFBody.DATA_TYPE_NUMBER: // 0
!                 return new Double(inputStream.readDouble());
!             case AMFBody.DATA_TYPE_BOOLEAN: // 1
!                 return new Boolean(inputStream.readBoolean());
!             case AMFBody.DATA_TYPE_STRING: // 2
!                 return inputStream.readUTF();
!             case AMFBody.DATA_TYPE_OBJECT: // 3
!                 return readObject();
!             case AMFBody.DATA_TYPE_MOVIE_CLIP: // 4
!                 throw new IOException("Unknown/unsupported object type " + AMFBody.getObjectTypeDescription(type));
!             case AMFBody.DATA_TYPE_NULL: // 5
!             case AMFBody.DATA_TYPE_UNDEFINED: //6
!                 return null;
!             case AMFBody.DATA_TYPE_REFERENCE_OBJECT: // 7
!                 return readFlushedSO();
!             case AMFBody.DATA_TYPE_MIXED_ARRAY: // 8
!                 long length = inputStream.readInt();
!                 //don't do anything with the length
!                 return readObject();
!             case AMFBody.DATA_TYPE_OBJECT_END: // 9
!                 return null;
!             case AMFBody.DATA_TYPE_ARRAY: // 10
!                 return readArray();
!             case AMFBody.DATA_TYPE_DATE: // 11
!                 return readDate();
!             case AMFBody.DATA_TYPE_LONG_STRING: // 12
! // TODO - fixme
                  int utflen = inputStream.readInt();
!                 return inputStream.readUTF();
!             case AMFBody.DATA_TYPE_AS_OBJECT: // 13
!                 return readASObject();
!             case AMFBody.DATA_TYPE_RECORDSET: // 14
! // TODO - fixme
!                 return null;
!             case AMFBody.DATA_TYPE_XML: // 15
!                 return XMLUtils.convertToDOM(inputStream);
!             case AMFBody.DATA_TYPE_CUSTOM_CLASS: // 16
!                 return readCustomClass();
!             default :
!                 throw new IOException("Unknown/unsupported object type " + AMFBody.getObjectTypeDescription(type));
!         }
!     }
  }

Index: AMFSerializer.java
===================================================================
RCS file: /cvsroot/openamf/openamf/src/java/org/openamf/io/AMFSerializer.java,v
retrieving revision 1.37
retrieving revision 1.38
diff -C2 -d -r1.37 -r1.38
*** AMFSerializer.java	15 Aug 2003 18:52:08 -0000	1.37
--- AMFSerializer.java	16 Aug 2003 13:11:16 -0000	1.38
***************
*** 30,61 ****
  
  /**
   * @author Jason Calabrese <[email protected]>
   * @author Pat Maddox <[email protected]>
   * @version $Revision$, $Date$
   */
  public class AMFSerializer {
  
! 	private static Log log = LogFactory.getLog(AMFSerializer.class);
  
  	private static final int MILLS_PER_HOUR = 60000;
  
  	/**
!      * The output stream
!      */
  	protected DataOutputStream outputStream;
!     /**
!      * Constructor
!      *
!      * @param outputStream
!      */
  	public AMFSerializer(DataOutputStream outputStream) {
  		this.outputStream = outputStream;
  	}
!     /**
!      * Writes message
!      *
!      * @param message
!      * @throws IOException
!      */
  	public void serializeMessage(AMFMessage message) throws IOException {
  		if (!log.isDebugEnabled()) {
--- 30,68 ----
  
  /**
+  * AMF Serializer
+  * 
   * @author Jason Calabrese <[email protected]>
   * @author Pat Maddox <[email protected]>
+  * @author Sylwester Lachiewicz <[email protected]>
   * @version $Revision$, $Date$
   */
  public class AMFSerializer {
  
! 	private static final Log log = LogFactory.getLog(AMFSerializer.class);
  
  	private static final int MILLS_PER_HOUR = 60000;
+ 	/**
+ 	 * Null message
+ 	 */
+ 	private static final String NULL_MESSAGE = "null";
  
  	/**
! 	 * The output stream
! 	 */
  	protected DataOutputStream outputStream;
! 	/**
! 	 * Constructor
! 	 *
! 	 * @param outputStream
! 	 */
  	public AMFSerializer(DataOutputStream outputStream) {
  		this.outputStream = outputStream;
  	}
! 	/**
! 	 * Writes message
! 	 *
! 	 * @param message
! 	 * @throws IOException
! 	 */
  	public void serializeMessage(AMFMessage message) throws IOException {
  		if (!log.isDebugEnabled()) {
***************
*** 63,67 ****
  		}
  		outputStream.writeShort(message.getVersion());
!         // write header
  		outputStream.writeShort(message.getHeaderCount());
  		Iterator headers = message.getHeaders().iterator();
--- 70,74 ----
  		}
  		outputStream.writeShort(message.getVersion());
! 		// write header
  		outputStream.writeShort(message.getHeaderCount());
  		Iterator headers = message.getHeaders().iterator();
***************
*** 70,74 ****
  			writeHeader(header);
  		}
!         // write body
  		outputStream.writeShort(message.getBodyCount());
  		Iterator bodies = message.getBodies();
--- 77,81 ----
  			writeHeader(header);
  		}
! 		// write body
  		outputStream.writeShort(message.getBodyCount());
  		Iterator bodies = message.getBodies();
***************
*** 78,87 ****
  		}
  	}
!     /**
!      * Write message header
!      *
!      * @param header
!      * @throws IOException
!      */
  	protected void writeHeader(AMFHeader header) throws IOException {
  		outputStream.writeUTF(header.getKey());
--- 85,94 ----
  		}
  	}
! 	/**
! 	 * Writes message header
! 	 *
! 	 * @param header AMF message header
! 	 * @throws IOException
! 	 */
  	protected void writeHeader(AMFHeader header) throws IOException {
  		outputStream.writeUTF(header.getKey());
***************
*** 91,111 ****
  		writeData(header.getValue());
  	}
!     /**
!      * Write message body
!      *
!      * @param body
!      * @throws IOException
!      */
  	protected void writeBody(AMFBody body) throws IOException {
!         if (body.getTarget() == null) {
!             outputStream.writeUTF("null");
!         } else {
!             outputStream.writeUTF(body.getTarget());
!         }
!         if (body.getResponse() == null) {
!             outputStream.writeUTF("null");
!         } else {
!             outputStream.writeUTF(body.getResponse());
!         }
  		// Always, always there is four bytes of FF, which is -1 of course
  		outputStream.writeInt(-1);
--- 98,120 ----
  		writeData(header.getValue());
  	}
! 	/**
! 	 * Writes message body
! 	 *
! 	 * @param body AMF message body
! 	 * @throws IOException
! 	 */
  	protected void writeBody(AMFBody body) throws IOException {
! 		// write url
! 		if (body.getTarget() == null) {
! 			outputStream.writeUTF(NULL_MESSAGE);
! 		} else {
! 			outputStream.writeUTF(body.getTarget());
! 		}
! 		// write response
! 		if (body.getResponse() == null) {
! 			outputStream.writeUTF(NULL_MESSAGE);
! 		} else {
! 			outputStream.writeUTF(body.getResponse());
! 		}
  		// Always, always there is four bytes of FF, which is -1 of course
  		outputStream.writeInt(-1);
***************
*** 114,135 ****
  	}
  
  	protected void writeData(Object value) throws IOException {
  		if (value == null) {
!             // write null object
  			outputStream.writeByte(AMFBody.DATA_TYPE_NULL);
  		} else if (value instanceof Number) {
!             // write number object
  			outputStream.writeByte(AMFBody.DATA_TYPE_NUMBER);
  			outputStream.writeDouble(((Number) value).doubleValue());
  		} else if (value instanceof String) {
!             // write String object
  			outputStream.writeByte(AMFBody.DATA_TYPE_STRING);
  			outputStream.writeUTF((String) value);
  		} else if (value instanceof Boolean) {
!             // write boolean object
  			outputStream.writeByte(AMFBody.DATA_TYPE_BOOLEAN);
  			outputStream.writeBoolean(((Boolean) value).booleanValue());
  		} else if (value instanceof Date) {
!             // write Date object
  			outputStream.writeByte(AMFBody.DATA_TYPE_DATE);
  			outputStream.writeDouble(((Date) value).getTime());
--- 123,150 ----
  	}
  
+ 	/**
+ 	 * Writes Data
+ 	 * 
+ 	 * @param value
+ 	 * @throws IOException
+ 	 */
  	protected void writeData(Object value) throws IOException {
  		if (value == null) {
! 			// write null object
  			outputStream.writeByte(AMFBody.DATA_TYPE_NULL);
  		} else if (value instanceof Number) {
! 			// write number object
  			outputStream.writeByte(AMFBody.DATA_TYPE_NUMBER);
  			outputStream.writeDouble(((Number) value).doubleValue());
  		} else if (value instanceof String) {
! 			// write String object
  			outputStream.writeByte(AMFBody.DATA_TYPE_STRING);
  			outputStream.writeUTF((String) value);
  		} else if (value instanceof Boolean) {
! 			// write boolean object
  			outputStream.writeByte(AMFBody.DATA_TYPE_BOOLEAN);
  			outputStream.writeBoolean(((Boolean) value).booleanValue());
  		} else if (value instanceof Date) {
! 			// write Date object
  			outputStream.writeByte(AMFBody.DATA_TYPE_DATE);
  			outputStream.writeDouble(((Date) value).getTime());
***************
*** 137,147 ****
  			outputStream.writeShort(offset / MILLS_PER_HOUR);
  		} else if (value instanceof Object[]) {
! 			writeArray(value);
  		} else if (value instanceof Iterator) {
! 			writeIterator(value);
  		} else if (value instanceof Collection) {
! 			writeCollection(value);
  		} else if (value instanceof Map) {
! 			writeMap(value);
  		} else if (value instanceof ResultSet) {
  			ASRecordSet asRecordSet = new ASRecordSet();
--- 152,163 ----
  			outputStream.writeShort(offset / MILLS_PER_HOUR);
  		} else if (value instanceof Object[]) {
! 			// write Object Array
! 			writeArray((Object[]) value);
  		} else if (value instanceof Iterator) {
! 			write((Iterator) value);
  		} else if (value instanceof Collection) {
! 			write((Collection) value);
  		} else if (value instanceof Map) {
! 			writeMap((Map) value);
  		} else if (value instanceof ResultSet) {
  			ASRecordSet asRecordSet = new ASRecordSet();
***************
*** 149,153 ****
  			writeData(asRecordSet);
  		} else if (value instanceof Document) {
! 			writeXML((Document) value);
  		} else {
  			/*
--- 165,169 ----
  			writeData(asRecordSet);
  		} else if (value instanceof Document) {
! 			write((Document) value);
  		} else {
  			/*
***************
*** 161,164 ****
--- 177,186 ----
  	}
  
+ 	/**
+ 	 * Writes Object
+ 	 * 
+ 	 * @param object
+ 	 * @throws IOException
+ 	 */
  	protected void writeObject(Object object) throws IOException {
  		log.debug("Writting Object");
***************
*** 204,238 ****
  	}
  
! 	protected void writeArray(Object value) throws IOException {
! 		Object[] array = (Object[]) value;
  		outputStream.writeByte(AMFBody.DATA_TYPE_ARRAY);
  		outputStream.writeInt(array.length);
  		for (int i = 0; i < array.length; i++) {
! 			Object object = array[i];
! 			writeData(object);
  		}
  	}
!     /**
!      * Writes Iterator (convert to List and call writeCollection)
!      *
!      * @param value
!      * @throws IOException
!      */
! 	protected void writeIterator(Object value) throws IOException {
! 		Iterator iterator = (Iterator) value;
  		List list = new ArrayList();
  		while (iterator.hasNext()) {
  			list.add(iterator.next());
  		}
! 		writeCollection(list);
  	}
!     /**
!      * Wites object Array
!      *
!      * @param value
!      * @throws IOException
!      */
! 	protected void writeCollection(Object value) throws IOException {
! 		Collection collection = (Collection) value;
  		outputStream.writeByte(AMFBody.DATA_TYPE_ARRAY);
  		outputStream.writeInt(collection.size());
--- 226,262 ----
  	}
  
! 	/**
! 	 * Writes Array Object - call <code>writeData</code> foreach element
! 	 * 
! 	 * @param array 
! 	 * @throws IOException
! 	 */
! 	protected void writeArray(Object[] array) throws IOException {
  		outputStream.writeByte(AMFBody.DATA_TYPE_ARRAY);
  		outputStream.writeInt(array.length);
  		for (int i = 0; i < array.length; i++) {
! 			writeData(array[i]);
  		}
  	}
! 	/**
! 	 * Writes Iterator - convert to List and call <code>writeCollection</code>
! 	 *
! 	 * @param iterator Iterator
! 	 * @throws IOException
! 	 */
! 	protected void write(Iterator iterator) throws IOException {
  		List list = new ArrayList();
  		while (iterator.hasNext()) {
  			list.add(iterator.next());
  		}
! 		write(list);
  	}
! 	/**
! 	 * Writes collection
! 	 *
! 	 * @param collection Collection
! 	 * @throws IOException
! 	 */
! 	protected void write(Collection collection) throws IOException {
  		outputStream.writeByte(AMFBody.DATA_TYPE_ARRAY);
  		outputStream.writeInt(collection.size());
***************
*** 242,257 ****
  		}
  	}
!     /**
!      * Writes Map
!      *
!      * @param value
!      * @throws IOException
!      */
! 	protected void writeMap(Object value) throws IOException {
! 		Map map = (Map) value;
! 		if (value instanceof ASObject && ((ASObject) value).getType() != null) {
! 			log.debug("Writting Custom Class: " + ((ASObject) value).getType());
  			outputStream.writeByte(AMFBody.DATA_TYPE_CUSTOM_CLASS);
! 			outputStream.writeUTF(((ASObject) value).getType());
  		} else {
  			log.debug("Writting Map");
--- 266,280 ----
  		}
  	}
! 	/**
! 	 * Writes Object Map
! 	 *
! 	 * @param value 
! 	 * @throws IOException
! 	 */
! 	protected void writeMap(Map map) throws IOException {
! 		if (map instanceof ASObject && ((ASObject) map).getType() != null) {
! 			log.debug("Writting Custom Class: " + ((ASObject) map).getType());
  			outputStream.writeByte(AMFBody.DATA_TYPE_CUSTOM_CLASS);
! 			outputStream.writeUTF(((ASObject) map).getType());
  		} else {
  			log.debug("Writting Map");
***************
*** 268,276 ****
  	}
  
! 	protected void writeXML(Document document) throws IOException {
  		outputStream.writeByte(AMFBody.DATA_TYPE_XML);
  		Element docElement = document.getDocumentElement();
  		String xmlData = XMLUtils.convertDOMToString(docElement);
! 		log.debug("Writting xmlData: \n" + xmlData);
  		ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
  		baOutputStream.write(xmlData.getBytes());
--- 291,307 ----
  	}
  
! 	/**
! 	 * Writes XML Document
! 	 * 
! 	 * @param document
! 	 * @throws IOException
! 	 */
! 	protected void write(Document document) throws IOException {
  		outputStream.writeByte(AMFBody.DATA_TYPE_XML);
  		Element docElement = document.getDocumentElement();
  		String xmlData = XMLUtils.convertDOMToString(docElement);
! 		if (log.isDebugEnabled()) {
! 			log.debug("Writting xmlData: \n" + xmlData);
! 		}
  		ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
  		baOutputStream.write(xmlData.getBytes());




-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01