SessionDictionary.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. BaseSet (s, value);
  94. _dirty = true;
  95. }
  96. }
  97. public object this [int index]
  98. {
  99. get {
  100. object o;
  101. lock (this)
  102. o = BaseGet (index);
  103. return o;
  104. }
  105. set {
  106. lock (this)
  107. BaseSet (index, value);
  108. _dirty = true;
  109. }
  110. }
  111. internal byte [] ToByteArray ()
  112. {
  113. MemoryStream stream = null;
  114. try {
  115. stream = new MemoryStream ();
  116. Serialize (new BinaryWriter (stream));
  117. return stream.GetBuffer ();
  118. } catch {
  119. throw;
  120. } finally {
  121. if (stream != null)
  122. stream.Close ();
  123. }
  124. }
  125. internal static SessionDictionary FromByteArray (byte [] data)
  126. {
  127. SessionDictionary result = null;
  128. MemoryStream stream = null;
  129. try {
  130. stream = new MemoryStream (data);
  131. result = Deserialize (new BinaryReader (stream));
  132. } catch {
  133. throw;
  134. } finally {
  135. if (stream != null)
  136. stream.Close ();
  137. }
  138. return result;
  139. }
  140. }
  141. }