SessionDictionary.cs 4.1 KB

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