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