AltSerialization.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. if (w.BaseStream is java.io.ObjectOutput) {
  66. ((java.io.ObjectOutput) w.BaseStream).writeObject (value);
  67. return;
  68. }
  69. #endif
  70. BinaryFormatter bf = new BinaryFormatter ();
  71. bf.Serialize (w.BaseStream, value);
  72. return;
  73. }
  74. w.Write (i);
  75. switch (i) {
  76. case 1:
  77. w.Write ((string) value);
  78. break;
  79. case 2:
  80. w.Write ((int) value);
  81. break;
  82. case 3:
  83. w.Write ((bool) value);
  84. break;
  85. case 4:
  86. w.Write (((DateTime) value).Ticks);
  87. break;
  88. case 5:
  89. w.Write ((decimal) value);
  90. break;
  91. case 6:
  92. w.Write ((byte) value);
  93. break;
  94. case 7:
  95. w.Write ((char) value);
  96. break;
  97. case 8:
  98. w.Write ((float) value);
  99. break;
  100. case 9:
  101. w.Write ((double) value);
  102. break;
  103. case 10:
  104. w.Write ((short) value);
  105. break;
  106. case 11:
  107. w.Write ((long) value);
  108. break;
  109. case 12:
  110. w.Write ((ushort) value);
  111. break;
  112. case 13:
  113. w.Write ((uint) value);
  114. break;
  115. case 14:
  116. w.Write ((ulong) value);
  117. break;
  118. }
  119. }
  120. internal static object DeserializeFromIndex (int index, BinaryReader r)
  121. {
  122. if (index == 15){
  123. #if TARGET_J2EE
  124. if (r.BaseStream is java.io.ObjectInput)
  125. return ((java.io.ObjectInput) r.BaseStream).readObject ();
  126. #endif
  127. BinaryFormatter bf = new BinaryFormatter ();
  128. return bf.Deserialize (r.BaseStream);
  129. }
  130. object value = null;
  131. switch (index) {
  132. case 1:
  133. value = r.ReadString ();
  134. break;
  135. case 2:
  136. value = r.ReadInt32 ();
  137. break;
  138. case 3:
  139. value = r.ReadBoolean ();
  140. break;
  141. case 4:
  142. value = new DateTime (r.ReadInt64 ());
  143. break;
  144. case 5:
  145. value = r.ReadDecimal ();
  146. break;
  147. case 6:
  148. value = r.ReadByte ();
  149. break;
  150. case 7:
  151. value = r.ReadChar ();
  152. break;
  153. case 8:
  154. value = r.ReadSingle ();
  155. break;
  156. case 9:
  157. value = r.ReadDouble ();
  158. break;
  159. case 10:
  160. value = r.ReadInt16 ();
  161. break;
  162. case 11:
  163. value = r.ReadInt64 ();
  164. break;
  165. case 12:
  166. value = r.ReadUInt16 ();
  167. break;
  168. case 13:
  169. value = r.ReadUInt32 ();
  170. break;
  171. case 14:
  172. value = r.ReadUInt64 ();
  173. break;
  174. }
  175. return value;
  176. }
  177. }
  178. }