SessionDictionary.cs 3.6 KB

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