AltSerialization.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // System.Web.Util.AltSerialization
  3. //
  4. // Author(s):
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Jackson Harper ([email protected])
  7. //
  8. // (C) 2003 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.IO;
  32. using System.Collections;
  33. using System.Runtime.Serialization.Formatters.Binary;
  34. namespace System.Web.Util {
  35. internal sealed class AltSerialization {
  36. private static ArrayList types;
  37. internal static readonly int NullIndex = 16;
  38. private AltSerialization () { }
  39. static AltSerialization ()
  40. {
  41. types = new ArrayList ();
  42. types.Add ("");
  43. types.Add (typeof (string));
  44. types.Add (typeof (int));
  45. types.Add (typeof (bool));
  46. types.Add (typeof (DateTime));
  47. types.Add (typeof (Decimal));
  48. types.Add (typeof (Byte));
  49. types.Add (typeof (Char));
  50. types.Add (typeof (Single));
  51. types.Add (typeof (Double));
  52. types.Add (typeof (short));
  53. types.Add (typeof (long));
  54. types.Add (typeof (ushort));
  55. types.Add (typeof (uint));
  56. types.Add (typeof (ulong));
  57. }
  58. internal static void SerializeByType (BinaryWriter w, object value)
  59. {
  60. Type type = value.GetType ();
  61. int i = types.IndexOf (type);
  62. if (i == -1) {
  63. w.Write (15); // types.Count
  64. #if TARGET_J2EE
  65. ((System.Web.J2EE.ObjectOutputStream)w.BaseStream).NativeStream.writeObject(value);
  66. #else
  67. BinaryFormatter bf = new BinaryFormatter ();
  68. bf.Serialize (w.BaseStream, value);
  69. #endif
  70. return;
  71. }
  72. w.Write (i);
  73. switch (i) {
  74. case 1:
  75. w.Write ((string) value);
  76. break;
  77. case 2:
  78. w.Write ((int) value);
  79. break;
  80. case 3:
  81. w.Write ((bool) value);
  82. break;
  83. case 4:
  84. w.Write (((DateTime) value).Ticks);
  85. break;
  86. case 5:
  87. w.Write ((decimal) value);
  88. break;
  89. case 6:
  90. w.Write ((byte) value);
  91. break;
  92. case 7:
  93. w.Write ((char) value);
  94. break;
  95. case 8:
  96. w.Write ((float) value);
  97. break;
  98. case 9:
  99. w.Write ((double) value);
  100. break;
  101. case 10:
  102. w.Write ((short) value);
  103. break;
  104. case 11:
  105. w.Write ((long) value);
  106. break;
  107. case 12:
  108. w.Write ((ushort) value);
  109. break;
  110. case 13:
  111. w.Write ((uint) value);
  112. break;
  113. case 14:
  114. w.Write ((ulong) value);
  115. break;
  116. }
  117. }
  118. internal static object DeserializeFromIndex (int index, BinaryReader r)
  119. {
  120. if (index == 15){
  121. #if TARGET_J2EE
  122. return ((System.Web.J2EE.ObjectInputStream)r.BaseStream).NativeStream.readObject();
  123. #else
  124. BinaryFormatter bf = new BinaryFormatter ();
  125. return bf.Deserialize (r.BaseStream);
  126. #endif
  127. }
  128. object value = null;
  129. switch (index) {
  130. case 1:
  131. value = r.ReadString ();
  132. break;
  133. case 2:
  134. value = r.ReadInt32 ();
  135. break;
  136. case 3:
  137. value = r.ReadBoolean ();
  138. break;
  139. case 4:
  140. value = new DateTime (r.ReadInt64 ());
  141. break;
  142. case 5:
  143. value = r.ReadDecimal ();
  144. break;
  145. case 6:
  146. value = r.ReadByte ();
  147. break;
  148. case 7:
  149. value = r.ReadChar ();
  150. break;
  151. case 8:
  152. value = r.ReadSingle ();
  153. break;
  154. case 9:
  155. value = r.ReadDouble ();
  156. break;
  157. case 10:
  158. value = r.ReadInt16 ();
  159. break;
  160. case 11:
  161. value = r.ReadInt64 ();
  162. break;
  163. case 12:
  164. value = r.ReadUInt16 ();
  165. break;
  166. case 13:
  167. value = r.ReadUInt32 ();
  168. break;
  169. case 14:
  170. value = r.ReadUInt64 ();
  171. break;
  172. }
  173. return value;
  174. }
  175. }
  176. }