KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > hessian > io > BasicSerializer


1 /*
2  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Burlap", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Scott Ferguson
47  */

48
49 package com.caucho.hessian.io;
50
51 import java.io.IOException JavaDoc;
52 import java.util.Date JavaDoc;
53
54 /**
55  * Serializing an object for known object types.
56  */

57 public class BasicSerializer extends AbstractSerializer {
58   public static final int NULL = 0;
59   public static final int BOOLEAN = NULL + 1;
60   public static final int BYTE = BOOLEAN + 1;
61   public static final int SHORT = BYTE + 1;
62   public static final int INTEGER = SHORT + 1;
63   public static final int LONG = INTEGER + 1;
64   public static final int FLOAT = LONG + 1;
65   public static final int DOUBLE = FLOAT + 1;
66   public static final int CHARACTER = DOUBLE + 1;
67   public static final int STRING = CHARACTER + 1;
68   public static final int DATE = STRING + 1;
69   public static final int NUMBER = DATE + 1;
70   public static final int OBJECT = NUMBER + 1;
71   
72   public static final int BOOLEAN_ARRAY = OBJECT + 1;
73   public static final int BYTE_ARRAY = BOOLEAN_ARRAY + 1;
74   public static final int SHORT_ARRAY = BYTE_ARRAY + 1;
75   public static final int INTEGER_ARRAY = SHORT_ARRAY + 1;
76   public static final int LONG_ARRAY = INTEGER_ARRAY + 1;
77   public static final int FLOAT_ARRAY = LONG_ARRAY + 1;
78   public static final int DOUBLE_ARRAY = FLOAT_ARRAY + 1;
79   public static final int CHARACTER_ARRAY = DOUBLE_ARRAY + 1;
80   public static final int STRING_ARRAY = CHARACTER_ARRAY + 1;
81   public static final int OBJECT_ARRAY = STRING_ARRAY + 1;
82
83   private int code;
84
85   public BasicSerializer(int code)
86   {
87     this.code = code;
88   }
89   
90   public void writeObject(Object JavaDoc obj, AbstractHessianOutput out)
91     throws IOException JavaDoc
92   {
93     switch (code) {
94     case BOOLEAN:
95       out.writeBoolean(((Boolean JavaDoc) obj).booleanValue());
96       break;
97       
98     case BYTE:
99     case SHORT:
100     case INTEGER:
101       out.writeInt(((Number JavaDoc) obj).intValue());
102       break;
103
104     case LONG:
105       out.writeLong(((Number JavaDoc) obj).longValue());
106       break;
107
108     case FLOAT:
109     case DOUBLE:
110       out.writeDouble(((Number JavaDoc) obj).doubleValue());
111       break;
112       
113     case CHARACTER:
114       out.writeString(String.valueOf(obj));
115       break;
116       
117     case STRING:
118       out.writeString((String JavaDoc) obj);
119       break;
120       
121     case DATE:
122       out.writeUTCDate(((Date JavaDoc) obj).getTime());
123       break;
124       
125     case BOOLEAN_ARRAY:
126     {
127       if (out.addRef(obj))
128         return;
129       
130       boolean []data = (boolean []) obj;
131       boolean hasEnd = out.writeListBegin(data.length, "[boolean");
132       for (int i = 0; i < data.length; i++)
133         out.writeBoolean(data[i]);
134
135       if (hasEnd)
136     out.writeListEnd();
137       
138       break;
139     }
140
141     case BYTE_ARRAY:
142     {
143       byte []data = (byte []) obj;
144       out.writeBytes(data, 0, data.length);
145       break;
146     }
147
148     case SHORT_ARRAY:
149     {
150       if (out.addRef(obj))
151         return;
152       
153       short []data = (short []) obj;
154       boolean hasEnd = out.writeListBegin(data.length, "[short");
155       
156       for (int i = 0; i < data.length; i++)
157         out.writeInt(data[i]);
158
159       if (hasEnd)
160     out.writeListEnd();
161       break;
162     }
163
164     case INTEGER_ARRAY:
165     {
166       if (out.addRef(obj))
167         return;
168       
169       int []data = (int []) obj;
170       
171       boolean hasEnd = out.writeListBegin(data.length, "[int");
172       
173       for (int i = 0; i < data.length; i++)
174         out.writeInt(data[i]);
175
176       if (hasEnd)
177     out.writeListEnd();
178       
179       break;
180     }
181
182     case LONG_ARRAY:
183     {
184       if (out.addRef(obj))
185         return;
186       
187       long []data = (long []) obj;
188       
189       boolean hasEnd = out.writeListBegin(data.length, "[long");
190       
191       for (int i = 0; i < data.length; i++)
192         out.writeLong(data[i]);
193
194       if (hasEnd)
195     out.writeListEnd();
196       break;
197     }
198
199     case FLOAT_ARRAY:
200     {
201       if (out.addRef(obj))
202         return;
203       
204       float []data = (float []) obj;
205       
206       boolean hasEnd = out.writeListBegin(data.length, "[float");
207       
208       for (int i = 0; i < data.length; i++)
209         out.writeDouble(data[i]);
210
211       if (hasEnd)
212     out.writeListEnd();
213       break;
214     }
215
216     case DOUBLE_ARRAY:
217     {
218       if (out.addRef(obj))
219         return;
220       
221       double []data = (double []) obj;
222       boolean hasEnd = out.writeListBegin(data.length, "[double");
223       
224       for (int i = 0; i < data.length; i++)
225         out.writeDouble(data[i]);
226
227       if (hasEnd)
228     out.writeListEnd();
229       break;
230     }
231
232     case STRING_ARRAY:
233     {
234       if (out.addRef(obj))
235         return;
236       
237       String JavaDoc []data = (String JavaDoc []) obj;
238       
239       boolean hasEnd = out.writeListBegin(data.length, "[string");
240       
241       for (int i = 0; i < data.length; i++) {
242         out.writeString(data[i]);
243       }
244
245       if (hasEnd)
246     out.writeListEnd();
247       break;
248     }
249
250     case CHARACTER_ARRAY:
251     {
252       char []data = (char []) obj;
253       out.writeString(data, 0, data.length);
254       break;
255     }
256
257     case OBJECT_ARRAY:
258     {
259       if (out.addRef(obj))
260         return;
261       
262       Object JavaDoc []data = (Object JavaDoc []) obj;
263       
264       boolean hasEnd = out.writeListBegin(data.length, "[object");
265       
266       for (int i = 0; i < data.length; i++) {
267         out.writeObject(data[i]);
268       }
269
270       if (hasEnd)
271     out.writeListEnd();
272       break;
273     }
274     
275     case NULL:
276       out.writeNull();
277       break;
278
279     default:
280       throw new RuntimeException JavaDoc(code + " " + String.valueOf(obj.getClass()));
281     }
282   }
283 }
284
Popular Tags