SessionDictionary.cs 3.7 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. // 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. object this_lock = new object ();
  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. lock (this_lock)
  52. BaseClear ();
  53. }
  54. internal string GetKey (int index)
  55. {
  56. string value;
  57. lock (this_lock)
  58. value = BaseGetKey (index);
  59. return value;
  60. }
  61. internal void Remove (string s)
  62. {
  63. lock (this_lock)
  64. BaseRemove (s);
  65. }
  66. internal void RemoveAt (int index)
  67. {
  68. lock (this_lock)
  69. BaseRemoveAt (index);
  70. }
  71. internal void Serialize (BinaryWriter writer)
  72. {
  73. writer.Write (Count);
  74. foreach (string key in base.Keys) {
  75. writer.Write (key);
  76. System.Web.Util.AltSerialization.Serialize (writer, BaseGet (key));
  77. }
  78. }
  79. internal static SessionDictionary Deserialize (BinaryReader r)
  80. {
  81. SessionDictionary result = new SessionDictionary ();
  82. for (int i = r.ReadInt32(); i > 0; i--)
  83. result [r.ReadString ()] =
  84. System.Web.Util.AltSerialization.Deserialize (r);
  85. return result;
  86. }
  87. internal object this [string s]
  88. {
  89. get {
  90. object o;
  91. lock (this_lock)
  92. o = BaseGet (s);
  93. return o;
  94. }
  95. set {
  96. lock (this_lock)
  97. {
  98. object obj = BaseGet(s);
  99. if ((obj == null) && (value == null))
  100. return;
  101. BaseSet (s, value);
  102. }
  103. }
  104. }
  105. public object this [int index]
  106. {
  107. get {
  108. object o;
  109. lock (this_lock)
  110. o = BaseGet (index);
  111. return o;
  112. }
  113. set {
  114. lock (this_lock)
  115. {
  116. object obj = BaseGet(index);
  117. if ((obj == null) && (value == null))
  118. return;
  119. BaseSet (index, value);
  120. }
  121. }
  122. }
  123. internal byte [] ToByteArray ()
  124. {
  125. MemoryStream stream = null;
  126. try {
  127. stream = new MemoryStream ();
  128. Serialize (new BinaryWriter (stream));
  129. return stream.GetBuffer ();
  130. } catch {
  131. throw;
  132. } finally {
  133. if (stream != null)
  134. stream.Close ();
  135. }
  136. }
  137. internal static SessionDictionary FromByteArray (byte [] data)
  138. {
  139. SessionDictionary result = null;
  140. MemoryStream stream = null;
  141. try {
  142. stream = new MemoryStream (data);
  143. result = Deserialize (new BinaryReader (stream));
  144. } catch {
  145. throw;
  146. } finally {
  147. if (stream != null)
  148. stream.Close ();
  149. }
  150. return result;
  151. }
  152. }
  153. }