[Red5commits] [568] lhubbard

luke@codegent.com luke at codegent.com
Mon Mar 20 11:20:07 EST 2006


Fixing the problem that D has with the serialzer.
Added core type map. Removed core type list. Its the same as Array really.
Perhaps we should rename Core.Array to Core.List (but that can wait till another day)
All the tests pass, It should all work fine.


Timestamp: 03/21/06 01:50:09 (less than one hour ago) 
Change: 568 
Author: lhubbard

Files (see diff or trac for details): 
java/io/trunk/src/org/red5/io/amf/Input.java
java/io/trunk/src/org/red5/io/amf/Output.java
java/io/trunk/src/org/red5/io/amf3/Input.java
java/io/trunk/src/org/red5/io/mock/Input.java
java/io/trunk/src/org/red5/io/mock/Mock.java
java/io/trunk/src/org/red5/io/mock/Output.java
java/io/trunk/src/org/red5/io/object/DataTypes.java
java/io/trunk/src/org/red5/io/object/Deserializer.java
java/io/trunk/src/org/red5/io/object/Input.java
java/io/trunk/src/org/red5/io/object/Output.java
java/io/trunk/src/org/red5/io/object/Serializer.java


Trac: http://mirror1.cvsdude.com/trac/osflash/red5/changeset/568

Index: /java/io/trunk/src/org/red5/io/amf/Input.java
===================================================================
--- /java/io/trunk/src/org/red5/io/amf/Input.java (revision 345)
+++ /java/io/trunk/src/org/red5/io/amf/Input.java (revision 568)
@@ -100,5 +100,5 @@
 			
 			case AMF.TYPE_MIXED_ARRAY:
-				coreType = DataTypes.CORE_LIST;
+				coreType = DataTypes.CORE_MAP;
 				break;
 				
@@ -162,12 +162,10 @@
 	public Number readNumber() {
 		double num = buf.getDouble();
-		//if(num < Byte.MAX_VALUE) 
-		//	return new Byte( (byte) num);
-		//if(num < Short.MAX_VALUE) 
-		//	return new Short( (short) num);
-		//if(num < Integer.MAX_VALUE) 
-		//	return new Integer( (int) num);
-		//else 
-		return new Double(num);
+		if(num == (double) Math.round(num)){
+			if(num < Integer.MAX_VALUE) 
+				return new Integer( (int) num);
+			else 
+				return new Long(Math.round(num));
+		} else return new Double(num);
 	}
 
@@ -267,5 +265,5 @@
 	 * @return int
 	 */	
-	public int readStartList() {
+	public int readStartMap() {
 		return buf.getInt();
 	}
@@ -283,6 +281,6 @@
 	 * @return int
 	 */
-	public int readItemIndex() {
-		return Integer.parseInt(getString(buf));
+	public String readItemKey() {
+		return getString(buf);
 	}
 
@@ -299,5 +297,5 @@
 	 * @return void
 	 */
-	public void skipEndList(){
+	public void skipEndMap(){
 		skipEndObject();
 	}
Index: /java/io/trunk/src/org/red5/io/amf/Output.java
===================================================================
--- /java/io/trunk/src/org/red5/io/amf/Output.java (revision 260)
+++ /java/io/trunk/src/org/red5/io/amf/Output.java (revision 568)
@@ -60,5 +60,5 @@
 	
 	// DONE
-	public void markEndList() {
+	public void markEndMap() {
 		markEndObject();
 	}
@@ -129,5 +129,5 @@
 	}
 	
-	public void writeStartList(int length) {
+	public void writeStartMap(int length) {
 		buf.put(AMF.TYPE_MIXED_ARRAY);
 		buf.putInt(length);
@@ -138,8 +138,8 @@
 	}
 
-	public void writeItemIndex(int index) {
-		writePropertyName(Integer.toString(index));
+	public void writeItemKey(String key) {
+		writePropertyName(key);
 	}
-
+	
 	public void writeStartArray(int length) {
 		buf.put(AMF.TYPE_ARRAY);
Index: /java/io/trunk/src/org/red5/io/amf3/Input.java
===================================================================
--- /java/io/trunk/src/org/red5/io/amf3/Input.java (revision 398)
+++ /java/io/trunk/src/org/red5/io/amf3/Input.java (revision 568)
@@ -78,5 +78,5 @@
 			case AMF3.TYPE_ARRAY:
 				// should we map this to list or array?
-				coreType = DataTypes.CORE_LIST;
+				coreType = DataTypes.CORE_ARRAY;
 				break;
 								
@@ -200,5 +200,5 @@
 	 * @return int
 	 */	
-	public int readStartList() {
+	public int readStartMap() {
 		return buf.getInt();
 	}
@@ -216,6 +216,6 @@
 	 * @return int
 	 */
-	public int readItemIndex() {
-		return 0;
+	public String readItemKey() {
+		return "";
 	}
 
@@ -232,5 +232,5 @@
 	 * @return void
 	 */
-	public void skipEndList(){
+	public void skipEndMap(){
 		skipEndObject();
 	}
Index: /java/io/trunk/src/org/red5/io/object/Input.java
===================================================================
--- /java/io/trunk/src/org/red5/io/object/Input.java (revision 225)
+++ /java/io/trunk/src/org/red5/io/object/Input.java (revision 568)
@@ -49,9 +49,9 @@
 	void skipEndArray();
 	
-	int readStartList();
-	int readItemIndex();
+	int readStartMap();
+	String readItemKey();
 	void skipItemSeparator();
 	boolean hasMoreItems();
-	void skipEndList();
+	void skipEndMap();
 	
 	String readStartObject();
Index: /java/io/trunk/src/org/red5/io/object/Output.java
===================================================================
--- /java/io/trunk/src/org/red5/io/object/Output.java (revision 225)
+++ /java/io/trunk/src/org/red5/io/object/Output.java (revision 568)
@@ -48,8 +48,8 @@
 	void markEndArray();
 	
-	void writeStartList(int highestIndex);
-	void writeItemIndex(int index);
+	void writeStartMap(int size);
+	void writeItemKey(String key);
 	void markItemSeparator();
-	void markEndList();
+	void markEndMap();
 	
 	void writeStartObject(String classname);
Index: /java/io/trunk/src/org/red5/io/object/Deserializer.java
===================================================================
--- /java/io/trunk/src/org/red5/io/object/Deserializer.java (revision 225)
+++ /java/io/trunk/src/org/red5/io/object/Deserializer.java (revision 568)
@@ -87,6 +87,6 @@
 				result = readArray(in);
 				break;
-			case DataTypes.CORE_LIST:
-				result = readList(in);
+			case DataTypes.CORE_MAP:
+				result = readMap(in);
 				break;
 			case DataTypes.CORE_XML:
@@ -114,47 +114,44 @@
 	 * Reads the input and returns an array of Objects
 	 * @param in
-	 * @return Object
-	 */
-	protected Object readArray(Input in){
+	 * @return ArrayList
+	 */
+	protected ArrayList readArray(Input in){
 		if(log.isDebugEnabled()) {
 			log.debug("Read array");
 		}
 		final int arraySize = in.readStartArray();
-		Object[] array = new Object[arraySize];
-		in.storeReference(array);
+		ArrayList list = new ArrayList(arraySize);
+		in.storeReference(list);
 		for(int i=0; i<arraySize; i++){
-			array[i] = deserialize(in);
+			list.add(deserialize(in));
 			in.skipElementSeparator();
 		}
 		in.skipEndArray();
- 		return array;
-	}
-	
-	/**
-	 * Reads the input and returns a List
+ 		return list;
+	}
+	
+	/**
+	 * Reads the input and returns a Map
 	 * @param in
 	 * @return List
 	 */
-	protected List readList(Input in){
-		if(log.isDebugEnabled()) {
-			log.debug("read list");
-		}
-		
-		int highestIndex = in.readStartList();
-		
-		if(log.isDebugEnabled()) {
-			log.debug("Read start list: "+highestIndex);
-		}
-		
-		List list = new ArrayList(highestIndex);
-		for(int i=0; i<highestIndex; i++){
-			list.add(i, null); // fill with null
-		}
+	protected Map readMap(Input in){
+		if(log.isDebugEnabled()) {
+			log.debug("read map");
+		}
+		
+		int size = in.readStartMap();
+		
+		if(log.isDebugEnabled()) {
+			log.debug("Read start map: "+size);
+		}
+		
+		final HashMap map = new HashMap();
 			
-		in.storeReference(list);
+		in.storeReference(map);
 		while(in.hasMoreItems()){
-			int index = in.readItemIndex();
-			if(log.isDebugEnabled()) {
-				log.debug("index: "+index);
+			String key = in.readItemKey();
+			if(log.isDebugEnabled()) {
+				log.debug("key: "+key);
 			}
 			Object item = deserialize(in);
@@ -162,10 +159,10 @@
 				log.debug("item: "+item);
 			}
-			list.set(index, item);
+			map.put(key, item);
 			if(in.hasMoreItems()) 
 				in.skipItemSeparator();
 		}
-		in.skipEndList();
-		return list;
+		in.skipEndMap();
+		return map;
 	}
 	
@@ -206,5 +203,5 @@
 			} // else fall through
 		} 
-		return readMap(in);
+		return readSimpleObject(in);
 	}
 	
@@ -253,5 +250,5 @@
 	 * @return Map
 	 */
-	protected Map readMap(Input in){
+	protected Map readSimpleObject(Input in){
 		if(log.isDebugEnabled()) {
 			log.debug("read map");
Index: /java/io/trunk/src/org/red5/io/object/Serializer.java
===================================================================
--- /java/io/trunk/src/org/red5/io/object/Serializer.java (revision 228)
+++ /java/io/trunk/src/org/red5/io/object/Serializer.java (revision 568)
@@ -151,16 +151,40 @@
 	 */
 	protected void writeList(Output out, List list){
+		// if its a small list, write it as an array
+		if(list.size() < 100 ) writeListAsArray(out, list);
+		// else we should check for lots of null values,
+		// if there are over 80% then its probably best to do it as a map
 		int size = list.size();
-		out.writeStartList(size);
+		int nullCount = 0;
+		for(int i=0; i<size; i++) if(list.get(i)==null) nullCount++;
+		if(nullCount > (size * 0.8)) writeListAsMap(out, list);
+		else writeListAsArray(out, list);
+	}
+	
+	protected void writeListAsMap(Output out, List list){
+		int size = list.size();
+		out.writeStartMap(size);
 		for(int i=0; i<size; i++){
 			Object item = list.get(i);
 			if(item!=null){
-				out.writeItemIndex(i);
+				out.writeItemKey(Integer.toString(i));
 				serialize(out, item);
 				out.markItemSeparator();
 			}
 		}
-		out.markEndList();
-	}
+		out.markEndMap();
+	}
+	
+	protected void writeListAsArray(Output out, List list){
+		int size = list.size();
+		out.writeStartArray(size);
+		for(int i=0; i<size; i++){
+			if(i>0) out.markElementSeparator();
+			//log.info(i);
+			serialize(out, list.get(i));
+		}
+		out.markEndMap();
+	}
+	
 	
 	/**
@@ -309,6 +333,7 @@
 			log.debug("writeMap");
 		}
-		out.writeStartObject(null);
-		Set set = map.entrySet();
+		
+		final Set set = map.entrySet();
+		out.writeStartMap(set.size());
 		Iterator it = set.iterator();
 		boolean isBeanMap = (map instanceof BeanMap);
@@ -316,9 +341,9 @@
 			Map.Entry entry = (Map.Entry) it.next();
 			if(isBeanMap && ((String)entry.getKey()).equals("class")) continue;
-			out.writePropertyName(entry.getKey().toString());
+			out.writeItemKey(entry.getKey().toString());
 			serialize(out,entry.getValue());
 			if(it.hasNext()) out.markPropertySeparator();
 		}
-		out.markEndObject();
+		out.markEndMap();
 	}
 	
@@ -340,5 +365,4 @@
 			if(entry.getKey().toString().equals("class")) continue;
 			out.writePropertyName(entry.getKey().toString());
-			
 			//log.info(entry.getKey().toString()+" = "+entry.getValue());
 			serialize(out,entry.getValue());
Index: /java/io/trunk/src/org/red5/io/object/DataTypes.java
===================================================================
--- /java/io/trunk/src/org/red5/io/object/DataTypes.java (revision 225)
+++ /java/io/trunk/src/org/red5/io/object/DataTypes.java (revision 568)
@@ -44,6 +44,7 @@
 	// Basic stuctures
 	public final static byte CORE_ARRAY = 0x06; 
-	public final static byte CORE_LIST = 0x07;
+	public final static byte CORE_MAP = 0x07;
 	public final static byte CORE_XML = 0x08; 
+	
 	public final static byte CORE_OBJECT = 0x09;
 	
@@ -76,5 +77,5 @@
 			case CORE_DATE: return "Date";
 			case CORE_ARRAY: return "Array";
-			case CORE_LIST: return "List";
+			case CORE_MAP: return "List";
 			case CORE_XML: return "XML";
 			case CORE_OBJECT: return "Object";
Index: /java/io/trunk/src/org/red5/io/mock/Input.java
===================================================================
--- /java/io/trunk/src/org/red5/io/mock/Input.java (revision 225)
+++ /java/io/trunk/src/org/red5/io/mock/Input.java (revision 568)
@@ -72,16 +72,16 @@
 		if (! (next instanceof Byte)) return true;
 		Byte b = (Byte) next;
-		return (b.byteValue() != Mock.TYPE_END_OF_LIST);
+		return (b.byteValue() != Mock.TYPE_END_OF_MAP);
 	}
 
-	public int readItemIndex() {
+	public String readItemKey() {
+		return (String) getNext();
+	}
+
+	public int readStartMap() {
 		return ((Integer) getNext()).intValue();
 	}
 
-	public int readStartList() {
-		return ((Integer) getNext()).intValue();
-	}
-
-	public void skipEndList() {
+	public void skipEndMap() {
 		getNext();
 	}
Index: /java/io/trunk/src/org/red5/io/mock/Output.java
===================================================================
--- /java/io/trunk/src/org/red5/io/mock/Output.java (revision 225)
+++ /java/io/trunk/src/org/red5/io/mock/Output.java (revision 568)
@@ -40,6 +40,6 @@
 	}
 	
-	public void markEndList(){
-		list.add(new Byte(Mock.TYPE_END_OF_LIST));
+	public void markEndMap(){
+		list.add(new Byte(Mock.TYPE_END_OF_MAP));
 	}
 
@@ -90,6 +90,6 @@
 	}
 	
-	public void writeItemIndex(int index) {
-		list.add(new Integer(index));
+	public void writeItemKey(String key) {
+		list.add(key);
 	}
 
@@ -104,6 +104,6 @@
 	}
 	
-	public void writeStartList(int highestIndex) {
-		list.add(new Byte(DataTypes.CORE_LIST));
+	public void writeStartMap(int highestIndex) {
+		list.add(new Byte(DataTypes.CORE_MAP));
 		list.add(new Integer(highestIndex));
 	}
Index: /java/io/trunk/src/org/red5/io/mock/Mock.java
===================================================================
--- /java/io/trunk/src/org/red5/io/mock/Mock.java (revision 225)
+++ /java/io/trunk/src/org/red5/io/mock/Mock.java (revision 568)
@@ -13,5 +13,5 @@
 	public static final byte TYPE_PROPERTY_SEPARATOR = (byte) (DataTypes.CUSTOM_MOCK_MASK +0x04);
 	public static final byte TYPE_ITEM_SEPARATOR = (byte) (DataTypes.CUSTOM_MOCK_MASK +0x05);
-	public static final byte TYPE_END_OF_LIST = (byte) (DataTypes.CUSTOM_MOCK_MASK +0x06);
+	public static final byte TYPE_END_OF_MAP = (byte) (DataTypes.CUSTOM_MOCK_MASK +0x06);
 	
 	public static String toStringValue(byte dataType){



More information about the Red5commits mailing list