refactoring for Serialize/Deserialize functionality
authorKonstantin Triger <kostat@mono-cvs.ximian.com>
Wed, 21 Mar 2007 14:09:20 +0000 (14:09 -0000)
committerKonstantin Triger <kostat@mono-cvs.ximian.com>
Wed, 21 Mar 2007 14:09:20 +0000 (14:09 -0000)
svn path=/trunk/mcs/; revision=74735

mcs/class/System.Web/System.Web.SessionState/SessionDictionary.cs
mcs/class/System.Web/System.Web.SessionState_2.0/SessionStateItemCollection.cs
mcs/class/System.Web/System.Web.Util/AltSerialization.cs
mcs/class/System.Web/System.Web.Util/ChangeLog
mcs/class/System.Web/System.Web/HttpStaticObjectsCollection.cs

index 9b709a48238f83f0ecd02cfd0cc77931c4df8c03..f62315432558dbf0d835a66bca15be1c6fe06d38 100644 (file)
@@ -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)
+       {\r
+               writer.Write (Count);\r
+               foreach (string key in base.Keys) {\r
+                       writer.Write (key);\r
+                       System.Web.Util.AltSerialization.Serialize (writer, BaseGet (key));\r
                }
        }
 
        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 ();\r
+               for (int i = r.ReadInt32(); i > 0; i--)\r
+                       result [r.ReadString ()] =\r
+                               System.Web.Util.AltSerialization.Deserialize (r);
 
                return result;
        }
index b5e946364febcda2127769a6213adedd667a4801..f738f4e1cbb1eeb4f19275363b42ce0541802613 100644 (file)
@@ -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;
+               {\r
+                       if (Count > 0) {\r
+                               BaseClear ();\r
+                               is_dirty = true;\r
+                       }
                }
 
                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);\r
+                       for (; i > 0; i--)\r
+                               ret [reader.ReadString ()] =\r
+                                       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;
+               }\r
+\r
+               public void Serialize (BinaryWriter writer) {\r
+                       writer.Write (Count);\r
+                       foreach (string key in base.Keys) {\r
+                               writer.Write (key);\r
+                               System.Web.Util.AltSerialization.Serialize (writer, BaseGet (key));\r
+                       }\r
                }
                
                // Todo: why override this?
index 182d3cc0bd5b49828ca84fa4ea7fb65f2620b520..500459d875160e5880ba156ec2202620ce99cf57 100644 (file)
@@ -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;
                }
        }
 }
index 224a463ed62d94e73fae2d9f596c8afbd2e3bd4d..b430f6e52ac988d9b3096377e90c8fba2946a5e2 100644 (file)
@@ -1,3 +1,7 @@
+2007-03-21  Konstantin Triger <kostat@mainsoft.com>
+
+       AltSerialization.cs: refactoring for Serialize/Deserialize functionality.
+
 2007-03-18  Marek Habersack  <mhabersack@novell.com>
 
        * UrlUtils.cs: GetDirectory always returns a path with trailing
index d54068df35720c1cfa85714b908d4c854cfbbd24..7dbf8789fd726cd5394729cfec15c7d9d16020d7 100644 (file)
@@ -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;
                }