SessionDictionary.cs 4.2 KB

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