(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Web / System.Web.SessionState / SessionDictionary.cs
blobd1b193d90d594d4e36e73351fb7c15ac6afa34aa
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 //
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.
31 using System;
32 using System.IO;
33 using System.Collections;
34 using System.Collections.Specialized;
36 namespace System.Web.SessionState {
37 internal class SessionDictionary : NameObjectCollectionBase
39 bool _dirty;
41 public SessionDictionary ()
45 internal SessionDictionary Clone ()
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];
54 return sess;
57 internal void Clear ()
59 _dirty = true;
60 lock (this)
61 BaseClear ();
64 internal string GetKey (int index)
66 string value;
67 lock (this)
68 value = BaseGetKey (index);
70 return value;
73 internal static bool IsInmutable (object o)
75 Type t = o.GetType ();
76 return (t == typeof (string) || t.IsPrimitive);
79 internal void Remove (string s)
81 lock (this)
82 BaseRemove (s);
83 _dirty = true;
86 internal void RemoveAt (int index)
88 lock (this)
89 BaseRemoveAt (index);
90 _dirty = true;
93 internal void Serialize (BinaryWriter w)
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;
105 System.Web.Util.AltSerialization.SerializeByType (w, value);
110 internal static SessionDictionary Deserialize (BinaryReader r)
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);
122 return result;
125 internal bool Dirty
127 get { return _dirty; }
128 set { _dirty = value; }
131 internal object this [string s]
133 get {
134 object o;
135 lock (this)
136 o = BaseGet (s);
138 return o;
141 set {
142 lock (this)
144 object obj = BaseGet(s);
145 if ((obj == null) && (value == null))
146 return;
147 BaseSet (s, value);
150 _dirty = true;
154 public object this [int index]
156 get {
157 object o;
158 lock (this)
159 o = BaseGet (index);
161 return o;
163 set {
164 lock (this)
166 object obj = BaseGet(index);
167 if ((obj == null) && (value == null))
168 return;
169 BaseSet (index, value);
172 _dirty = true;
176 internal byte [] ToByteArray ()
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 ();
191 internal static SessionDictionary FromByteArray (byte [] data)
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 ();
204 return result;