2004-05-05 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.SessionState / SessionDictionary.cs
1 //
2 // System.Web.SessionState.SessionDictionary
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 using System;
11 using System.IO;
12 using System.Collections;
13 using System.Collections.Specialized;
14
15 namespace System.Web.SessionState {
16 internal class SessionDictionary : NameObjectCollectionBase
17 {
18         bool _dirty;
19         
20         public SessionDictionary ()
21         {
22         }
23
24         internal SessionDictionary Clone ()
25         {
26                 SessionDictionary sess = new SessionDictionary ();
27                 int last = sess.Count;
28                 for (int i = 0; i < last; i++) {
29                         string key = GetKey (i);
30                         sess [key] = this [key];
31                 }
32
33                 return sess;
34         }
35         
36         internal void Clear ()
37         {
38                 _dirty = true;
39                 lock (this)
40                         BaseClear ();
41         }
42
43         internal string GetKey (int index)
44         {
45                 string value;
46                 lock (this)
47                         value = BaseGetKey (index);
48                         
49                 return value;
50         }
51
52         internal static bool IsInmutable (object o)
53         {
54                 Type t = o.GetType ();
55                 return (t == typeof (string) || t.IsPrimitive);
56         }
57
58         internal void Remove (string s)
59         {
60                 lock (this)
61                         BaseRemove (s);
62                 _dirty = true;
63         }
64
65         internal void RemoveAt (int index)
66         {
67                 lock (this)
68                         BaseRemoveAt (index);
69                 _dirty = true;
70         }
71
72         internal void Serialize (BinaryWriter w)
73         {
74                 lock (this) {
75                         w.Write (Count);
76                         foreach (string key in Keys) {
77                                 w.Write (key);
78                                 object value = BaseGet (key);
79                                 if (value == null) {
80                                         w.Write (System.Web.Util.AltSerialization.NullIndex);
81                                         continue;
82                                 }
83
84                                 System.Web.Util.AltSerialization.SerializeByType (w, value);
85                         }
86                 }
87         }
88
89         internal static SessionDictionary Deserialize (BinaryReader r)
90         {
91                 SessionDictionary result = new SessionDictionary ();
92                 for (int i = r.ReadInt32 (); i > 0; i--) {
93                         string key = r.ReadString ();
94                         int index = r.ReadInt32 ();
95                         if (index == System.Web.Util.AltSerialization.NullIndex)
96                                 result [key] = null;
97                         else
98                                 result [key] = System.Web.Util.AltSerialization.DeserializeFromIndex (index, r);
99                 }
100
101                 return result;
102         }
103
104         internal bool Dirty
105         {
106                 get { return _dirty; }
107                 set { _dirty = value; }
108         }
109
110         internal object this [string s]
111         {
112                 get {
113                         object o;
114                         lock (this)
115                                 o = BaseGet (s);
116
117                         return o;
118                 }
119
120                 set {
121                         lock (this)\r
122                         {                                \r
123                                 object obj = BaseGet(s);\r
124                                 if ((obj == null) && (value == null))\r
125                                         return; \r
126                                 BaseSet (s, value);
127                         }
128
129                         _dirty = true;
130                 }
131         }
132
133         public object this [int index]
134         {
135                 get {
136                         object o;
137                         lock (this)
138                                 o = BaseGet (index);
139
140                         return o;
141                 }
142                 set {
143                         lock (this)
144                         {                                \r
145                                 object obj = BaseGet(index);\r
146                                 if ((obj == null) && (value == null))\r
147                                         return; \r
148                                 BaseSet (index, value);
149                         }
150
151                         _dirty = true;
152                 }
153         }
154
155         internal byte [] ToByteArray ()
156         {
157                 MemoryStream stream = null;
158                 try {
159                         stream = new MemoryStream ();
160                         Serialize (new BinaryWriter (stream));
161                         return stream.GetBuffer ();
162                 } catch {
163                         throw;
164                 } finally {
165                         if (stream != null)
166                                 stream.Close ();
167                 }
168         }
169
170         internal static SessionDictionary FromByteArray (byte [] data)
171         {
172                 SessionDictionary result = null;
173                 MemoryStream stream = null;
174                 try {
175                         stream = new MemoryStream (data);
176                         result = Deserialize (new BinaryReader (stream));
177                 } catch {
178                         throw;
179                 } finally {
180                         if (stream != null)
181                                 stream.Close ();
182                 }
183                 return result;
184         }
185 }
186
187 }
188