Browse Source

refactoring for Serialize/Deserialize functionality

svn path=/trunk/mcs/; revision=74735
Konstantin Triger 19 years ago
parent
commit
121f197bb2

+ 10 - 23
mcs/class/System.Web/System.Web.SessionState/SessionDictionary.cs

@@ -78,34 +78,21 @@ internal class SessionDictionary : NameObjectCollectionBase
 			BaseRemoveAt (index);
 	}
 
-	internal void Serialize (BinaryWriter w)
-	{
-		lock (this) {
-			w.Write (Count);
-			foreach (string key in Keys) {
-				w.Write (key);
-				object value = BaseGet (key);
-				if (value == null) {
-					w.Write (System.Web.Util.AltSerialization.NullIndex);
-					continue;
-				}
-
-				System.Web.Util.AltSerialization.SerializeByType (w, value);
-			}
+	internal void Serialize (BinaryWriter writer)
+	{
+		writer.Write (Count);
+		foreach (string key in base.Keys) {
+			writer.Write (key);
+			System.Web.Util.AltSerialization.Serialize (writer, BaseGet (key));
 		}
 	}
 
 	internal static SessionDictionary Deserialize (BinaryReader r)
 	{
-		SessionDictionary result = new SessionDictionary ();
-		for (int i = r.ReadInt32 (); i > 0; i--) {
-			string key = r.ReadString ();
-			int index = r.ReadInt32 ();
-			if (index == System.Web.Util.AltSerialization.NullIndex)
-				result [key] = null;
-			else
-				result [key] = System.Web.Util.AltSerialization.DeserializeFromIndex (index, r);
-		}
+		SessionDictionary result = new SessionDictionary ();
+		for (int i = r.ReadInt32(); i > 0; i--)
+			result [r.ReadString ()] =
+				System.Web.Util.AltSerialization.Deserialize (r);
 
 		return result;
 	}

+ 21 - 65
mcs/class/System.Web/System.Web.SessionState_2.0/SessionStateItemCollection.cs

@@ -38,45 +38,11 @@ namespace System.Web.SessionState
 {
 	public sealed class SessionStateItemCollection : NameObjectCollectionBase, ISessionStateItemCollection, ICollection, IEnumerable
 	{
-		static Dictionary <Type, bool> immutables;
 		bool is_dirty;
-		
-		static SessionStateItemCollection ()
-		{
-			immutables = new Dictionary <Type, bool> (14);
-			Type type = typeof(string);
-			immutables.Add (type, true);
-			type = typeof(int);
-			immutables.Add (type, true);
-			type = typeof(bool);
-			immutables.Add (type, true);
-			type = typeof(decimal);
-			immutables.Add (type, true);
-			type = typeof(byte);
-			immutables.Add (type, true);
-			type = typeof(char);
-			immutables.Add (type, true);
-			type = typeof(float);
-			immutables.Add (type, true);
-			type = typeof(double);
-			immutables.Add (type, true);
-			type = typeof(sbyte);
-			immutables.Add (type, true);
-			type = typeof(short);
-			immutables.Add (type, true);
-			type = typeof(long);
-			immutables.Add (type, true);
-			type = typeof(ushort);
-			immutables.Add (type, true);
-			type = typeof(uint);
-			immutables.Add (type, true);
-			type = typeof(ulong);
-			immutables.Add (type, true);
-		}
 
 		static bool IsMutable (object o)
 		{
-			return (immutables.ContainsKey (o.GetType ()) == false);
+			return (o != null && Type.GetTypeCode(o.GetType()) == TypeCode.Object);
 		}
 		
 		public SessionStateItemCollection ()
@@ -96,7 +62,7 @@ namespace System.Web.SessionState
 		public object this [int index] {
 			get {
 				object o = BaseGet (index);
-				if (o != null && IsMutable (o))
+				if (IsMutable (o))
 					is_dirty = true;
 				return o;
 			}
@@ -110,7 +76,7 @@ namespace System.Web.SessionState
                 public object this [string name] {
 			get {
 				object o = BaseGet (name);
-				if (o != null && IsMutable (o))
+				if (IsMutable (o))
 					is_dirty = true;
 				return o;
 			}
@@ -127,40 +93,30 @@ namespace System.Web.SessionState
 		}
 
 		public void Clear ()
-		{
-			BaseClear ();
-			is_dirty = true;
+		{
+			if (Count > 0) {
+				BaseClear ();
+				is_dirty = true;
+			}
 		}
 
 		public static SessionStateItemCollection Deserialize (BinaryReader reader)
 		{
 			int i = reader.ReadInt32 ();
-			SessionStateItemCollection ret = new SessionStateItemCollection (i);
-			while (i > 0) {
-				i--;
-				string key = reader.ReadString ();
-				int index = reader.ReadInt32 ();
-				if (index == AltSerialization.NullIndex)
-					ret [key] = null;
-				else
-					ret [key] = AltSerialization.DeserializeFromIndex (index, reader);
-			}
-			return ret;
-		}
+			SessionStateItemCollection ret = new SessionStateItemCollection (i);
+			for (; i > 0; i--)
+				ret [reader.ReadString ()] =
+					System.Web.Util.AltSerialization.Deserialize (reader);
 
-		public void Serialize (BinaryWriter writer)
-		{
-			lock (this) {
-				writer.Write (Count);
-				foreach (string key in base.Keys) {
-					object val = BaseGet (key);
-					if (val == null) {
-						writer.Write (AltSerialization.NullIndex);
-						continue;
-					}
-					AltSerialization.SerializeByType (writer, val);
-				}
-			}
+			return ret;
+		}
+
+		public void Serialize (BinaryWriter writer) {
+			writer.Write (Count);
+			foreach (string key in base.Keys) {
+				writer.Write (key);
+				System.Web.Util.AltSerialization.Serialize (writer, BaseGet (key));
+			}
 		}
 		
 		// Todo: why override this?

+ 87 - 116
mcs/class/System.Web/System.Web.Util/AltSerialization.cs

@@ -38,155 +38,126 @@ namespace System.Web.Util {
 
 	internal sealed class AltSerialization {
 
-                private static ArrayList types;
-
-                internal static readonly int NullIndex = 16;
                 
 		private AltSerialization () { }
-                
-                
-                static AltSerialization ()
-                {
-                        types = new ArrayList ();
-                        types.Add ("");
-                        types.Add (typeof (string));
-                        types.Add (typeof (int));
-                        types.Add (typeof (bool));
-                        types.Add (typeof (DateTime));
-                        types.Add (typeof (Decimal));
-                        types.Add (typeof (Byte));
-                        types.Add (typeof (Char));
-                        types.Add (typeof (Single));
-                        types.Add (typeof (Double));
-                        types.Add (typeof (short));
-                        types.Add (typeof (long));
-                        types.Add (typeof (ushort));
-                        types.Add (typeof (uint));
-                        types.Add (typeof (ulong));
-                }
-                
-		internal static void SerializeByType (BinaryWriter w, object value)
+  
+		internal static void Serialize (BinaryWriter w, object value)
 		{
-			Type type = value.GetType ();
-			int i = types.IndexOf (type);
-			if (i == -1) {
-				w.Write (15); // types.Count
-#if TARGET_J2EE
-				if (w.BaseStream is java.io.ObjectOutput) {
-					((java.io.ObjectOutput) w.BaseStream).writeObject (value);
-					return;
-				}
-#endif
-				BinaryFormatter bf = new BinaryFormatter ();
-				bf.Serialize (w.BaseStream, value);
-				return;
-			}
-			
-			w.Write (i);
-			switch (i) {
-			case 1:
-				w.Write ((string) value);
-				break;
-			case 2:
-				w.Write ((int) value);
-				break;
-			case 3:
+			TypeCode typeCode = value != null ? Type.GetTypeCode (value.GetType ()) : TypeCode.Empty;
+			w.Write ((byte)typeCode);
+
+			switch (typeCode) {
+			case TypeCode.Boolean:
 				w.Write ((bool) value);
 				break;
-			case 4:
-				w.Write (((DateTime) value).Ticks);
-				break;
-			case 5:
-				w.Write ((decimal) value);
-				break;
-			case 6:
+			case TypeCode.Byte:
 				w.Write ((byte) value);
 				break;
-			case 7:
+			case TypeCode.Char:
 				w.Write ((char) value);
 				break;
-			case 8:
-				w.Write ((float) value);
+			case TypeCode.DateTime:
+				w.Write (((DateTime) value).Ticks);
+				break;
+			case TypeCode.DBNull:
+				break;
+			case TypeCode.Decimal:
+				w.Write ((decimal) value);
 				break;
-			case 9:
+			case TypeCode.Double:
 				w.Write ((double) value);
 				break;
-			case 10:
+			case TypeCode.Empty:
+				break;
+			case TypeCode.Int16:
 				w.Write ((short) value);
 				break;
-			case 11:
+			case TypeCode.Int32:
+				w.Write ((int) value);
+				break;
+			case TypeCode.Int64:
 				w.Write ((long) value);
 				break;
-			case 12:
+			case TypeCode.Object:
+#if TARGET_J2EE
+				if (w.BaseStream is java.io.ObjectOutput) {
+					((java.io.ObjectOutput) w.BaseStream).writeObject (value);
+					return;
+				}
+#endif
+				BinaryFormatter bf = new BinaryFormatter ();
+				bf.Serialize (w.BaseStream, value);
+				break;
+			case TypeCode.SByte:
+				w.Write ((sbyte) value);
+				break;
+			case TypeCode.Single:
+				w.Write ((float) value);
+				break;
+			case TypeCode.String:
+				w.Write ((string) value);
+				break;
+			case TypeCode.UInt16:
 				w.Write ((ushort) value);
 				break;
-			case 13:
+			case TypeCode.UInt32:
 				w.Write ((uint) value);
 				break;
-			case 14:
+			case TypeCode.UInt64:
 				w.Write ((ulong) value);
 				break;
+
 			}
 		}
 
-		internal static object DeserializeFromIndex (int index, BinaryReader r)
+		internal static object Deserialize (BinaryReader r)
 		{
-			if (index == 15){
+			TypeCode typeCode = (TypeCode)r.ReadByte();
+			switch (typeCode) {
+			case TypeCode.Boolean:
+				return r.ReadBoolean ();
+			case TypeCode.Byte:
+				return r.ReadByte ();
+			case TypeCode.Char:
+				return r.ReadChar ();
+			case TypeCode.DateTime:
+				return new DateTime (r.ReadInt64 ());
+			case TypeCode.DBNull:
+				return DBNull.Value;
+			case TypeCode.Decimal:
+				return r.ReadDecimal ();
+			case TypeCode.Double:
+				return r.ReadDouble ();
+			case TypeCode.Empty:
+				return null;
+			case TypeCode.Int16:
+				return r.ReadInt16 ();
+			case TypeCode.Int32:
+				return r.ReadInt32 ();
+			case TypeCode.Int64:
+				return r.ReadInt64 ();
+			case TypeCode.Object:
 #if TARGET_J2EE
 				if (r.BaseStream is java.io.ObjectInput)
 					return ((java.io.ObjectInput) r.BaseStream).readObject ();
 #endif
 				BinaryFormatter bf = new BinaryFormatter ();
 				return bf.Deserialize (r.BaseStream);
+			case TypeCode.SByte:
+				return r.ReadSByte ();
+			case TypeCode.Single:
+				return r.ReadSingle ();
+			case TypeCode.String:
+				return r.ReadString ();
+			case TypeCode.UInt16:
+				return r.ReadUInt16 ();
+			case TypeCode.UInt32:
+				return r.ReadUInt32 ();
+			case TypeCode.UInt64:
+				return r.ReadUInt64 ();
+			default:
+				throw new ArgumentOutOfRangeException ("TypeCode:" + typeCode);
 			}
-			
-			object value = null;
-			switch (index) {
-			case 1:
-				value = r.ReadString ();
-				break;
-			case 2:
-				value = r.ReadInt32 ();
-				break;
-			case 3:
-				value = r.ReadBoolean ();
-				break;
-			case 4:
-				value = new DateTime (r.ReadInt64 ());
-				break;
-			case 5:
-				value = r.ReadDecimal ();
-				break;
-			case 6:
-				value = r.ReadByte ();
-				break;
-			case 7:
-				value = r.ReadChar ();
-				break;
-			case 8:
-				value = r.ReadSingle ();
-				break;
-			case 9:
-				value = r.ReadDouble ();
-				break;
-			case 10:
-				value = r.ReadInt16 ();
-				break;
-			case 11:
-				value = r.ReadInt64 ();
-				break;
-			case 12:
-				value = r.ReadUInt16 ();
-				break;
-			case 13:
-				value = r.ReadUInt32 ();
-				break;
-			case 14:
-				value = r.ReadUInt64 ();
-				break;
-			}
-			
-			return value;
 		}
 	}
 }

+ 4 - 0
mcs/class/System.Web/System.Web.Util/ChangeLog

@@ -1,3 +1,7 @@
+2007-03-21  Konstantin Triger <[email protected]>
+
+	AltSerialization.cs: refactoring for Serialize/Deserialize functionality.
+
 2007-03-18  Marek Habersack  <[email protected]>
 
 	* UrlUtils.cs: GetDirectory always returns a path with trailing

+ 7 - 20
mcs/class/System.Web/System.Web/HttpStaticObjectsCollection.cs

@@ -154,18 +154,10 @@ namespace System.Web {
 		internal void Serialize (BinaryWriter writer)
 #endif
 		{
-			lock (_Objects) {
-				writer.Write (Count);
-				foreach (string key in _Objects.Keys) {
-					writer.Write (key);
-					object value = _Objects [key];
-					if (value == null) {
-						writer.Write (System.Web.Util.AltSerialization.NullIndex);
-						continue;
-					}
-
-					System.Web.Util.AltSerialization.SerializeByType (writer, value);
-				}
+			writer.Write (_Objects.Count);
+			foreach (string key in _Objects.Keys) {
+				writer.Write (key);
+				System.Web.Util.AltSerialization.Serialize (writer, _Objects [key]);
 			}
 		}
 
@@ -176,14 +168,9 @@ namespace System.Web {
 #endif
 		{
 			HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
-			for (int i = reader.ReadInt32 (); i > 0; i--) {
-				string key = reader.ReadString ();
-				int index = reader.ReadInt32 ();
-				if (index == System.Web.Util.AltSerialization.NullIndex)
-					result.Set (key, null);
-				else
-					result.Set (key, System.Web.Util.AltSerialization.DeserializeFromIndex (index, reader));
-			}
+			for (int i = reader.ReadInt32 (); i > 0; i--)
+				result.Set (reader.ReadString (),
+					System.Web.Util.AltSerialization.Deserialize (reader));
 
 			return result;
 		}