openamf/src/java/org/openamf/io AMFDeserializer.java,1.28,1.29 AMFSerializer.java,1.36,1.37

[email protected] Fri, 15 Aug 2003 11:52:10 -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-serv13549/java/org/openamf/io

Modified Files:
	AMFDeserializer.java AMFSerializer.java 
Log Message:
javadoc fix and code cleanup

Index: AMFDeserializer.java
===================================================================
RCS file: /cvsroot/openamf/openamf/src/java/org/openamf/io/AMFDeserializer.java,v
retrieving revision 1.28
retrieving revision 1.29
diff -C2 -d -r1.28 -r1.29
*** AMFDeserializer.java	9 Aug 2003 16:59:23 -0000	1.28
--- AMFDeserializer.java	15 Aug 2003 18:52:08 -0000	1.29
***************
*** 37,66 ****
  	private List storedObjects;
  
! 	// The AMF input stream
  	protected DataInputStream inputStream;
  
! 	// Number of headers in the packet
  	protected int headerCount;
  
! 	// Content of the headers
! 	protected List headers;
  
! 	// Number of bodies
  	protected int bodyCount;
  
! 	// Content of the bodies
! 	protected List bodies;
! 
! 	// Object to store the deserialized data
! 	protected AMFMessage message;
  
  	public AMFDeserializer(DataInputStream inputStream) throws IOException {
- 
  		if (!log.isDebugEnabled()) {
  			log.info("Deserializing Message, for more info turn on debug level");
  		}
- 
- 		message = new AMFMessage();
- 
  		// Save the input stream for this object
  		this.inputStream = inputStream;
--- 37,79 ----
  	private List storedObjects;
  
! 	/**
!      * 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
+      *
+      * @param inputStream message input stream
+      * @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;
***************
*** 76,83 ****
  		return message;
  	}
! 
  	protected void readHeaders() throws IOException {
! 		// Ignore the first two bytes - version or something
! 		inputStream.readUnsignedShort();
  		// Find total number of header elements
  		headerCount = inputStream.readUnsignedShort();
--- 89,100 ----
  		return message;
  	}
!     /**
!      * Read message header
!      *
!      * @throws IOException
!      */
  	protected void readHeaders() throws IOException {
! 		// version
! 		message.setVersion(inputStream.readUnsignedShort());
  		// Find total number of header elements
  		headerCount = inputStream.readUnsignedShort();
***************
*** 86,94 ****
  		// 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 = readBoolean();
  			// Grab the length of the header element
  			long length = inputStream.readInt();
--- 103,109 ----
  		// Loop over all the header elements
  		for (int i = 0; i < headerCount; i++) {
  			String key = inputStream.readUTF();
  			// Find the must understand flag
! 			boolean required = inputStream.readBoolean();
  			// Grab the length of the header element
  			long length = inputStream.readInt();
***************
*** 101,105 ****
  		}
  	}
! 
  	protected void readBodies() throws IOException {
  		// Find the total number of body elements
--- 116,124 ----
  		}
  	}
!     /**
!      * Read message body
!      *
!      * @throws IOException
!      */
  	protected void readBodies() throws IOException {
  		// Find the total number of body elements
***************
*** 109,114 ****
  		// Loop over all the body elements
  		for (int i = 0; i < bodyCount; i++) {
! 			//clear storedObjects
! 			storedObjects = new ArrayList();
  			String method = inputStream.readUTF();
  			// The target that the client understands
--- 128,132 ----
  		// Loop over all the body elements
  		for (int i = 0; i < bodyCount; i++) {
!             // The target method
  			String method = inputStream.readUTF();
  			// The target that the client understands
***************
*** 118,128 ****
  			// Grab the type of the element
  			byte type = inputStream.readByte();
- 
  			log.debug("type = " + type);
- 
- 			//log.debug("Length InnerObject " + inputStream.readInt());
- 			//type = inputStream.readByte();
- 			//log.debug("Type InnerObject " + type);			
- 
  			// Turn the argument elements into real data
  			Object data = readData(type);
--- 136,140 ----
***************
*** 131,148 ****
  		}
  	}
! 
  	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);
--- 143,161 ----
  		}
  	}
!     /**
!      * Reads custom class
!      *
!      * @return
!      * @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);
***************
*** 153,167 ****
  		return readObject(aso);
  	}
! 	// Reads an object and converts the binary data into an ArrayList
  	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;
--- 166,182 ----
  		return readObject(aso);
  	}
!     /**
!      * Reads an object and converts the binary data into an List
!      *
!      * @param aso
!      * @return
!      * @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;
***************
*** 174,206 ****
  			} 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;
  	}
! 
  	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++) {
--- 189,215 ----
  			} 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
!      *
!      * @return
!      * @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++) {
***************
*** 211,238 ****
  			array.add(data);
  		}
- 
  		// Return the data
  		return array;
  	}
! 
  	private void storeObject(Object o) {
  		storedObjects.add(o);
  		log.debug("storedObjects.size: " + storedObjects.size());
  	}
! 
! 	// Read the next byte and return its boolean value
! 	protected boolean readBoolean() throws IOException {
! 		byte value = inputStream.readByte();
! 
! 		// If it's a 0x01 return true, else return false
! 		if (value == 1)
! 			return true;
! 		else
! 			return false;
! 	}
! 
  	protected Date readDate() throws IOException {
! 		double ms = inputStream.readDouble(); // Date in millis from 01/01/1970
! 
  		int timeoffset = inputStream.readUnsignedShort();
  		if (timeoffset > 720) {
--- 220,243 ----
  			array.add(data);
  		}
  		// Return the data
  		return array;
  	}
!     /**
!      * Store object in  internal array
!      *
!      * @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) {
***************
*** 240,250 ****
  			timeoffset = - (65536 - timeoffset);
  		}
! 
! 		ms += (double) timeoffset;
! 		Date date = new Date((long) ms);
  
  		return date;
  	}
! 
  	protected Document readXML() throws IOException {
  		Document document = null;
--- 245,259 ----
  			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;
***************
*** 256,264 ****
  			document = builder.parse(new InputSource(inputStream));
  		} catch (Exception e) {
! 			throw new IOException(e.getMessage());
  		}
  		return document;
  	}
! 
  	protected Object readFlushedSO() throws IOException {
  		int index = inputStream.readUnsignedShort();
--- 265,278 ----
  			document = builder.parse(new InputSource(inputStream));
  		} catch (Exception e) {
! 			throw new IOException("Error while parsing xml: "+e.getMessage());
  		}
  		return document;
  	}
!     /**
!      * Reads flushed stored object
!      *
!      * @return
!      * @throws IOException
!      */
  	protected Object readFlushedSO() throws IOException {
  		int index = inputStream.readUnsignedShort();
***************
*** 266,279 ****
  		return storedObjects.get(index);
  	}
! 
  	protected Object readASObject() {
  		return null;
  	}
! 
  	protected Object readData(byte type) throws IOException {
  		Object data = null;
! 
! 		log.debug("Reding data of type: " + type);
! 
  		switch (type) {
  			case AMFBody.DATA_TYPE_NUMBER :
--- 280,301 ----
  		return storedObjects.get(index);
  	}
!     /**
!      * Reads object
!      *
!      * @return
!      */
  	protected Object readASObject() {
  		return null;
  	}
!     /**
!      * Reads object from inputstream with selected type
!      *
!      * @param type
!      * @return
!      * @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 :
***************
*** 281,293 ****
  				break;
  			case AMFBody.DATA_TYPE_BOOLEAN :
! 				data = new Boolean(readBoolean());
  				break;
  			case AMFBody.DATA_TYPE_STRING :
  				data = inputStream.readUTF();
  				break;
  			case AMFBody.DATA_TYPE_OBJECT :
  				data = readObject();
  				break;
  			case AMFBody.DATA_TYPE_NULL :
  				data = null;
  				break;
--- 303,321 ----
  				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;
***************
*** 319,323 ****
  				break;
  		}
- 
  		return data;
  	}
--- 347,350 ----

Index: AMFSerializer.java
===================================================================
RCS file: /cvsroot/openamf/openamf/src/java/org/openamf/io/AMFSerializer.java,v
retrieving revision 1.36
retrieving revision 1.37
diff -C2 -d -r1.36 -r1.37
*** AMFSerializer.java	9 Aug 2003 16:59:23 -0000	1.36
--- AMFSerializer.java	15 Aug 2003 18:52:08 -0000	1.37
***************
*** 40,66 ****
  	private static final int MILLS_PER_HOUR = 60000;
  
! 	// The output stream
  	protected DataOutputStream outputStream;
! 
  	public AMFSerializer(DataOutputStream outputStream) {
  		this.outputStream = outputStream;
  	}
! 
  	public void serializeMessage(AMFMessage message) throws IOException {
- 
  		if (!log.isDebugEnabled()) {
  			log.info("Serializing Message, for more info turn on debug level");
  		}
! 
! 		//version ?
! 		outputStream.writeShort(0);
! 
  		outputStream.writeShort(message.getHeaderCount());
! 		Iterator headers = message.getHeaders();
  		while (headers.hasNext()) {
  			AMFHeader header = (AMFHeader) headers.next();
  			writeHeader(header);
  		}
! 
  		outputStream.writeShort(message.getBodyCount());
  		Iterator bodies = message.getBodies();
--- 40,74 ----
  	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()) {
  			log.info("Serializing Message, for more info turn on debug level");
  		}
! 		outputStream.writeShort(message.getVersion());
!         // write header
  		outputStream.writeShort(message.getHeaderCount());
! 		Iterator headers = message.getHeaders().iterator();
  		while (headers.hasNext()) {
  			AMFHeader header = (AMFHeader) headers.next();
  			writeHeader(header);
  		}
!         // write body
  		outputStream.writeShort(message.getBodyCount());
  		Iterator bodies = message.getBodies();
***************
*** 69,75 ****
  			writeBody(body);
  		}
- 
  	}
! 
  	protected void writeHeader(AMFHeader header) throws IOException {
  		outputStream.writeUTF(header.getKey());
--- 77,87 ----
  			writeBody(body);
  		}
  	}
!     /**
!      * Write message header
!      *
!      * @param header
!      * @throws IOException
!      */
  	protected void writeHeader(AMFHeader header) throws IOException {
  		outputStream.writeUTF(header.getKey());
***************
*** 79,86 ****
  		writeData(header.getValue());
  	}
! 
  	protected void writeBody(AMFBody body) throws IOException {
! 		outputStream.writeUTF(body.getTarget());
! 		outputStream.writeUTF(body.getResponse());
  		// Always, always there is four bytes of FF, which is -1 of course
  		outputStream.writeInt(-1);
--- 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);
***************
*** 91,105 ****
--- 116,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());
***************
*** 132,136 ****
  
  	protected void writeObject(Object object) throws IOException {
- 
  		log.debug("Writting Object");
  		String customClassName =
--- 162,165 ----
***************
*** 177,185 ****
  	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];
--- 206,211 ----
***************
*** 187,210 ****
  		}
  	}
! 
  	protected void writeIterator(Object value) throws IOException {
  		Iterator iterator = (Iterator) value;
- 
  		List list = new ArrayList();
- 
  		while (iterator.hasNext()) {
  			list.add(iterator.next());
  		}
- 
  		writeCollection(list);
  	}
! 
  	protected void writeCollection(Object value) throws IOException {
  		Collection collection = (Collection) value;
- 
  		outputStream.writeByte(AMFBody.DATA_TYPE_ARRAY);
- 
  		outputStream.writeInt(collection.size());
- 
  		for (Iterator objects = collection.iterator(); objects.hasNext();) {
  			Object object = objects.next();
--- 213,240 ----
  		}
  	}
!     /**
!      * 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());
  		for (Iterator objects = collection.iterator(); objects.hasNext();) {
  			Object object = objects.next();
***************
*** 212,221 ****
  		}
  	}
! 
  	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);
--- 242,254 ----
  		}
  	}
!     /**
!      * 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);
***************
*** 227,231 ****
  		for (Iterator entrys = map.entrySet().iterator(); entrys.hasNext();) {
  			Map.Entry entry = (Map.Entry) entrys.next();
! 			log.debug(entry.getKey().toString() + ": " + entry.getValue());
  			outputStream.writeUTF(entry.getKey().toString());
  			writeData(entry.getValue());
--- 260,264 ----
  		for (Iterator entrys = map.entrySet().iterator(); entrys.hasNext();) {
  			Map.Entry entry = (Map.Entry) entrys.next();
! 			log.debug(entry.getKey() + ": " + entry.getValue());
  			outputStream.writeUTF(entry.getKey().toString());
  			writeData(entry.getValue());




-------------------------------------------------------
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