KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
53 import java.util.Date JavaDoc;
54
55 /**
56  * Serializing an object for known object types.
57  */

58 public class BasicDeserializer extends AbstractDeserializer {
59   public static final int NULL = BasicSerializer.NULL;
60   public static final int BOOLEAN = BasicSerializer.BOOLEAN;
61   public static final int BYTE = BasicSerializer.BYTE;
62   public static final int SHORT = BasicSerializer.SHORT;
63   public static final int INTEGER = BasicSerializer.INTEGER;
64   public static final int LONG = BasicSerializer.LONG;
65   public static final int FLOAT = BasicSerializer.FLOAT;
66   public static final int DOUBLE = BasicSerializer.DOUBLE;
67   public static final int CHARACTER = BasicSerializer.CHARACTER;
68   public static final int STRING = BasicSerializer.STRING;
69   public static final int DATE = BasicSerializer.DATE;
70   public static final int NUMBER = BasicSerializer.NUMBER;
71   public static final int OBJECT = BasicSerializer.OBJECT;
72   
73   public static final int BOOLEAN_ARRAY = BasicSerializer.BOOLEAN_ARRAY;
74   public static final int BYTE_ARRAY = BasicSerializer.BYTE_ARRAY;
75   public static final int SHORT_ARRAY = BasicSerializer.SHORT_ARRAY;
76   public static final int INTEGER_ARRAY = BasicSerializer.INTEGER_ARRAY;
77   public static final int LONG_ARRAY = BasicSerializer.LONG_ARRAY;
78   public static final int FLOAT_ARRAY = BasicSerializer.FLOAT_ARRAY;
79   public static final int DOUBLE_ARRAY = BasicSerializer.DOUBLE_ARRAY;
80   public static final int CHARACTER_ARRAY = BasicSerializer.CHARACTER_ARRAY;
81   public static final int STRING_ARRAY = BasicSerializer.STRING_ARRAY;
82   public static final int OBJECT_ARRAY = BasicSerializer.OBJECT_ARRAY;
83
84   private int _code;
85
86   public BasicDeserializer(int code)
87   {
88     _code = code;
89   }
90
91   public Class JavaDoc getType()
92   {
93     switch (_code) {
94     case NULL:
95       return void.class;
96     case BOOLEAN:
97       return Boolean JavaDoc.class;
98     case BYTE:
99       return Byte JavaDoc.class;
100     case SHORT:
101       return Short JavaDoc.class;
102     case INTEGER:
103       return Integer JavaDoc.class;
104     case LONG:
105       return Long JavaDoc.class;
106     case FLOAT:
107       return Float JavaDoc.class;
108     case DOUBLE:
109       return Double JavaDoc.class;
110     case CHARACTER:
111       return Character JavaDoc.class;
112     case STRING:
113       return String JavaDoc.class;
114     case DATE:
115       return Date JavaDoc.class;
116     case NUMBER:
117       return Number JavaDoc.class;
118
119     case BOOLEAN_ARRAY:
120       return boolean[].class;
121     case BYTE_ARRAY:
122       return byte[].class;
123     case SHORT_ARRAY:
124       return short[].class;
125     case INTEGER_ARRAY:
126       return int[].class;
127     case LONG_ARRAY:
128       return long[].class;
129     case FLOAT_ARRAY:
130       return float[].class;
131     case DOUBLE_ARRAY:
132       return double[].class;
133     case CHARACTER_ARRAY:
134       return char[].class;
135     case STRING_ARRAY:
136       return String JavaDoc[].class;
137     case OBJECT_ARRAY:
138       return Object JavaDoc[].class;
139     default:
140       throw new UnsupportedOperationException JavaDoc();
141     }
142   }
143   
144   public Object JavaDoc readObject(AbstractHessianInput in)
145     throws IOException JavaDoc
146   {
147     switch (_code) {
148     case NULL:
149       // hessian/3490
150
in.readObject();
151       
152       return null;
153       
154     case BOOLEAN:
155       return new Boolean JavaDoc(in.readBoolean());
156       
157     case BYTE:
158       return new Byte JavaDoc((byte) in.readInt());
159       
160     case SHORT:
161       return new Short JavaDoc((short) in.readInt());
162       
163     case INTEGER:
164       return new Integer JavaDoc(in.readInt());
165
166     case LONG:
167       return new Long JavaDoc(in.readLong());
168
169     case FLOAT:
170       return new Float JavaDoc((float) in.readDouble());
171
172     case DOUBLE:
173       return new Double JavaDoc(in.readDouble());
174       
175     case STRING:
176       return in.readString();
177       
178     case OBJECT:
179       return in.readObject();
180       
181     case CHARACTER:
182       {
183     String JavaDoc s = in.readString();
184     if (s == null || s.equals(""))
185       return null;
186     else
187       return new Character JavaDoc(s.charAt(0));
188       }
189       
190     case DATE:
191       return new Date JavaDoc(in.readUTCDate());
192       
193     case NUMBER:
194       return in.readObject();
195
196     case BYTE_ARRAY:
197       return in.readBytes();
198
199     case CHARACTER_ARRAY:
200     {
201       String JavaDoc s = in.readString();
202
203       if (s == null)
204         return null;
205       else {
206         int len= s.length();
207         char []chars = new char[len];
208         s.getChars(0, len, chars, 0);
209         return chars;
210       }
211     }
212
213     case BOOLEAN_ARRAY:
214     case SHORT_ARRAY:
215     case INTEGER_ARRAY:
216     case LONG_ARRAY:
217     case FLOAT_ARRAY:
218     case DOUBLE_ARRAY:
219     case STRING_ARRAY:
220     {
221       int code = in.readListStart();
222
223       switch (code) {
224       case 'N':
225     return null;
226
227       case 0x10: case 0x11: case 0x12: case 0x13:
228       case 0x14: case 0x15: case 0x16: case 0x17:
229       case 0x18: case 0x19: case 0x1a: case 0x1b:
230       case 0x1c: case 0x1d: case 0x1e: case 0x1f:
231     int length = code - 0x10;
232     in.readInt();
233
234     return readLengthList(in, length);
235
236       default:
237     String JavaDoc type = in.readType();
238     length = in.readLength();
239
240     return readList(in, length);
241       }
242     }
243
244     default:
245       throw new UnsupportedOperationException JavaDoc();
246     }
247   }
248   
249   public Object JavaDoc readList(AbstractHessianInput in, int length)
250     throws IOException JavaDoc
251   {
252     switch (_code) {
253     case BOOLEAN_ARRAY: {
254       if (length >= 0) {
255         boolean []data = new boolean[length];
256
257         in.addRef(data);
258
259         for (int i = 0; i < data.length; i++)
260           data[i] = in.readBoolean();
261     
262         in.readEnd();
263
264         return data;
265       }
266       else {
267         ArrayList JavaDoc list = new ArrayList JavaDoc();
268
269         while (! in.isEnd())
270           list.add(new Boolean JavaDoc(in.readBoolean()));
271     
272         in.readEnd();
273
274         boolean []data = new boolean[list.size()];
275         
276         in.addRef(data);
277         
278         for (int i = 0; i < data.length; i++)
279           data[i] = ((Boolean JavaDoc) list.get(i)).booleanValue();
280
281         return data;
282       }
283     }
284     
285     case SHORT_ARRAY: {
286       if (length >= 0) {
287         short []data = new short[length];
288
289         in.addRef(data);
290         
291         for (int i = 0; i < data.length; i++)
292           data[i] = (short) in.readInt();
293     
294         in.readEnd();
295
296         return data;
297       }
298       else {
299         ArrayList JavaDoc list = new ArrayList JavaDoc();
300
301         while (! in.isEnd())
302           list.add(new Short JavaDoc((short) in.readInt()));
303
304         in.readEnd();
305
306         short []data = new short[list.size()];
307         for (int i = 0; i < data.length; i++)
308           data[i] = ((Short JavaDoc) list.get(i)).shortValue();
309
310         in.addRef(data);
311         
312         return data;
313       }
314     }
315     
316     case INTEGER_ARRAY: {
317       if (length >= 0) {
318         int []data = new int[length];
319
320         in.addRef(data);
321         
322         for (int i = 0; i < data.length; i++)
323           data[i] = in.readInt();
324
325         in.readEnd();
326     
327         return data;
328       }
329       else {
330         ArrayList JavaDoc list = new ArrayList JavaDoc();
331
332         while (! in.isEnd())
333           list.add(new Integer JavaDoc(in.readInt()));
334
335
336         in.readEnd();
337     
338         int []data = new int[list.size()];
339         for (int i = 0; i < data.length; i++)
340           data[i] = ((Integer JavaDoc) list.get(i)).intValue();
341         
342         in.addRef(data);
343
344         return data;
345       }
346     }
347     
348     case LONG_ARRAY: {
349       if (length >= 0) {
350         long []data = new long[length];
351
352         in.addRef(data);
353
354         for (int i = 0; i < data.length; i++)
355           data[i] = in.readLong();
356
357         in.readEnd();
358
359         return data;
360       }
361       else {
362         ArrayList JavaDoc list = new ArrayList JavaDoc();
363
364         while (! in.isEnd())
365           list.add(new Long JavaDoc(in.readLong()));
366
367         in.readEnd();
368         
369         long []data = new long[list.size()];
370         for (int i = 0; i < data.length; i++)
371           data[i] = ((Long JavaDoc) list.get(i)).longValue();
372         
373         in.addRef(data);
374
375         return data;
376       }
377     }
378     
379     case FLOAT_ARRAY: {
380       if (length >= 0) {
381         float []data = new float[length];
382         in.addRef(data);
383
384         for (int i = 0; i < data.length; i++)
385           data[i] = (float) in.readDouble();
386
387         in.readEnd();
388
389         return data;
390       }
391       else {
392         ArrayList JavaDoc list = new ArrayList JavaDoc();
393
394         while (! in.isEnd())
395           list.add(new Float JavaDoc(in.readDouble()));
396
397         in.readEnd();
398         
399         float []data = new float[list.size()];
400         for (int i = 0; i < data.length; i++)
401           data[i] = ((Float JavaDoc) list.get(i)).floatValue();
402         
403         in.addRef(data);
404
405         return data;
406       }
407     }
408     
409     case DOUBLE_ARRAY: {
410       if (length >= 0) {
411         double []data = new double[length];
412         in.addRef(data);
413
414         for (int i = 0; i < data.length; i++)
415           data[i] = in.readDouble();
416
417         in.readEnd();
418
419         return data;
420       }
421       else {
422         ArrayList JavaDoc list = new ArrayList JavaDoc();
423
424         while (! in.isEnd())
425           list.add(new Double JavaDoc(in.readDouble()));
426
427         in.readEnd();
428         
429         double []data = new double[list.size()];
430         in.addRef(data);
431         for (int i = 0; i < data.length; i++)
432           data[i] = ((Double JavaDoc) list.get(i)).intValue();
433
434         in.readEnd();
435
436         return data;
437       }
438     }
439     
440     case STRING_ARRAY: {
441       if (length >= 0) {
442         String JavaDoc []data = new String JavaDoc[length];
443         in.addRef(data);
444
445         for (int i = 0; i < data.length; i++)
446           data[i] = in.readString();
447
448         in.readEnd();
449
450         return data;
451       }
452       else {
453         ArrayList JavaDoc list = new ArrayList JavaDoc();
454
455         while (! in.isEnd())
456           list.add(in.readString());
457
458         in.readEnd();
459         
460         String JavaDoc []data = new String JavaDoc[list.size()];
461         in.addRef(data);
462         for (int i = 0; i < data.length; i++)
463           data[i] = (String JavaDoc) list.get(i);
464
465         in.readEnd();
466
467         return data;
468       }
469     }
470     
471     case OBJECT_ARRAY: {
472       if (length >= 0) {
473         Object JavaDoc []data = new Object JavaDoc[length];
474         in.addRef(data);
475
476         for (int i = 0; i < data.length; i++)
477           data[i] = in.readObject();
478
479         in.readEnd();
480
481         return data;
482       }
483       else {
484         ArrayList JavaDoc list = new ArrayList JavaDoc();
485         
486         in.addRef(list); // XXX: potential issues here
487

488         while (! in.isEnd())
489           list.add(in.readObject());
490
491         in.readEnd();
492         
493         Object JavaDoc []data = new Object JavaDoc[list.size()];
494         for (int i = 0; i < data.length; i++)
495           data[i] = (Object JavaDoc) list.get(i);
496
497         return data;
498       }
499     }
500     
501     default:
502       throw new UnsupportedOperationException JavaDoc(String.valueOf(this));
503     }
504   }
505   
506   public Object JavaDoc readLengthList(AbstractHessianInput in, int length)
507     throws IOException JavaDoc
508   {
509     switch (_code) {
510     case BOOLEAN_ARRAY: {
511       boolean []data = new boolean[length];
512
513       in.addRef(data);
514
515       for (int i = 0; i < data.length; i++)
516     data[i] = in.readBoolean();
517
518       return data;
519     }
520     
521     case SHORT_ARRAY: {
522       short []data = new short[length];
523
524       in.addRef(data);
525         
526       for (int i = 0; i < data.length; i++)
527     data[i] = (short) in.readInt();
528
529       return data;
530     }
531     
532     case INTEGER_ARRAY: {
533       int []data = new int[length];
534
535       in.addRef(data);
536         
537       for (int i = 0; i < data.length; i++)
538     data[i] = in.readInt();
539
540       return data;
541     }
542     
543     case FLOAT_ARRAY: {
544       float []data = new float[length];
545       in.addRef(data);
546
547       for (int i = 0; i < data.length; i++)
548     data[i] = (float) in.readDouble();
549
550       return data;
551     }
552     
553     case DOUBLE_ARRAY: {
554       double []data = new double[length];
555       in.addRef(data);
556
557       for (int i = 0; i < data.length; i++)
558     data[i] = in.readDouble();
559
560       return data;
561     }
562     
563     case STRING_ARRAY: {
564       String JavaDoc []data = new String JavaDoc[length];
565       in.addRef(data);
566
567       for (int i = 0; i < data.length; i++)
568     data[i] = in.readString();
569
570       return data;
571     }
572     
573     case OBJECT_ARRAY: {
574       Object JavaDoc []data = new Object JavaDoc[length];
575       in.addRef(data);
576
577       for (int i = 0; i < data.length; i++)
578     data[i] = in.readObject();
579
580       return data;
581     }
582     
583     default:
584       throw new UnsupportedOperationException JavaDoc(String.valueOf(this));
585     }
586   }
587 }
588
Popular Tags