2
0

SessionDictionary.cs 2.9 KB

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