SessionDictionary.cs 3.2 KB

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