Copied remotely
[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 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.IO;
33 using System.Collections;
34 using System.Collections.Specialized;
35
36 namespace System.Web.SessionState {
37 internal class SessionDictionary : NameObjectCollectionBase
38 {
39         bool _dirty;
40         
41         public SessionDictionary ()
42         {
43         }
44
45         internal SessionDictionary Clone ()
46         {
47                 SessionDictionary sess = new SessionDictionary ();
48                 int last = sess.Count;
49                 for (int i = 0; i < last; i++) {
50                         string key = GetKey (i);
51                         sess [key] = this [key];
52                 }
53
54                 return sess;
55         }
56         
57         internal void Clear ()
58         {
59                 _dirty = true;
60                 lock (this)
61                         BaseClear ();
62         }
63
64         internal string GetKey (int index)
65         {
66                 string value;
67                 lock (this)
68                         value = BaseGetKey (index);
69                         
70                 return value;
71         }
72
73         internal static bool IsInmutable (object o)
74         {
75                 Type t = o.GetType ();
76                 return (t == typeof (string) || t.IsPrimitive);
77         }
78
79         internal void Remove (string s)
80         {
81                 lock (this)
82                         BaseRemove (s);
83                 _dirty = true;
84         }
85
86         internal void RemoveAt (int index)
87         {
88                 lock (this)
89                         BaseRemoveAt (index);
90                 _dirty = true;
91         }
92
93         internal void Serialize (BinaryWriter w)
94         {
95                 lock (this) {
96                         w.Write (Count);
97                         foreach (string key in Keys) {
98                                 w.Write (key);
99                                 object value = BaseGet (key);
100                                 if (value == null) {
101                                         w.Write (System.Web.Util.AltSerialization.NullIndex);
102                                         continue;
103                                 }
104
105                                 System.Web.Util.AltSerialization.SerializeByType (w, value);
106                         }
107                 }
108         }
109
110         internal static SessionDictionary Deserialize (BinaryReader r)
111         {
112                 SessionDictionary result = new SessionDictionary ();
113                 for (int i = r.ReadInt32 (); i > 0; i--) {
114                         string key = r.ReadString ();
115                         int index = r.ReadInt32 ();
116                         if (index == System.Web.Util.AltSerialization.NullIndex)
117                                 result [key] = null;
118                         else
119                                 result [key] = System.Web.Util.AltSerialization.DeserializeFromIndex (index, r);
120                 }
121
122                 return result;
123         }
124
125         internal bool Dirty
126         {
127                 get { return _dirty; }
128                 set { _dirty = value; }
129         }
130
131         internal object this [string s]
132         {
133                 get {
134                         object o;
135                         lock (this)
136                                 o = BaseGet (s);
137
138                         return o;
139                 }
140
141                 set {
142                         lock (this)\r
143                         {                                \r
144                                 object obj = BaseGet(s);\r
145                                 if ((obj == null) && (value == null))\r
146                                         return; \r
147                                 BaseSet (s, value);
148                         }
149
150                         _dirty = true;
151                 }
152         }
153
154         public object this [int index]
155         {
156                 get {
157                         object o;
158                         lock (this)
159                                 o = BaseGet (index);
160
161                         return o;
162                 }
163                 set {
164                         lock (this)
165                         {                                \r
166                                 object obj = BaseGet(index);\r
167                                 if ((obj == null) && (value == null))\r
168                                         return; \r
169                                 BaseSet (index, value);
170                         }
171
172                         _dirty = true;
173                 }
174         }
175
176         internal byte [] ToByteArray ()
177         {
178                 MemoryStream stream = null;
179                 try {
180                         stream = new MemoryStream ();
181                         Serialize (new BinaryWriter (stream));
182                         return stream.GetBuffer ();
183                 } catch {
184                         throw;
185                 } finally {
186                         if (stream != null)
187                                 stream.Close ();
188                 }
189         }
190
191         internal static SessionDictionary FromByteArray (byte [] data)
192         {
193                 SessionDictionary result = null;
194                 MemoryStream stream = null;
195                 try {
196                         stream = new MemoryStream (data);
197                         result = Deserialize (new BinaryReader (stream));
198                 } catch {
199                         throw;
200                 } finally {
201                         if (stream != null)
202                                 stream.Close ();
203                 }
204                 return result;
205         }
206 }
207
208 }
209