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