KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > common > SerUtils


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.common;
13
14 import com.versant.core.metadata.MDStatics;
15 import com.versant.core.metadata.FieldMetaData;
16 import com.versant.core.util.OIDObjectInput;
17 import com.versant.core.util.OIDObjectOutput;
18
19 import java.io.*;
20 import java.util.Date JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.math.BigDecimal JavaDoc;
23 import java.math.BigInteger JavaDoc;
24
25 /**
26  * Utility methods to help with Serialization.
27  */

28 public final class SerUtils {
29
30     public static Object JavaDoc readArrayField(FieldMetaData fmd,
31             OIDObjectInput is)
32             throws IOException, ClassNotFoundException JavaDoc {
33         if (fmd.elementTypeMetaData != null) {
34             return SerUtils.readOIDArray(is);
35         } else {
36             return SerUtils.readArrayField(fmd.componentTypeCode, is);
37         }
38     }
39
40     public static void writeArrayField(FieldMetaData fmd, OIDObjectOutput os,
41                                        Object JavaDoc data) throws IOException {
42         if (fmd.elementTypeMetaData != null) {
43             SerUtils.writeOIDArray((OID[]) data, os);
44         } else {
45             SerUtils.writeArrayField(fmd.componentTypeCode, os, data);
46         }
47     }
48
49     public static final void writeOIDArray(OID[] oids, OIDObjectOutput out) throws IOException {
50         if (oids == null) {
51             out.writeInt(0);
52         } else {
53             int length = oids.length;
54             out.writeInt(length);
55             for (int i = 0; i < length; i++) {
56                 out.write(oids[i]);
57             }
58         }
59     }
60
61     public static final OID[] readOIDArray(OIDObjectInput in) throws IOException,
62             ClassNotFoundException JavaDoc {
63         int size = in.readInt();
64         if (size > 0) {
65             OID[] oids = new OID[size];
66             for (int i = 0; i < size; i++) {
67                 oids[i] = in.readOID();
68             }
69             return oids;
70         } else {
71             return null;
72         }
73     }
74
75     public static Object JavaDoc readArrayField(int type, OIDObjectInput is)
76             throws IOException, ClassNotFoundException JavaDoc {
77         switch (type) {
78             case MDStatics.INTW:
79                 return SerUtils.readIntegerArray(is);
80             case MDStatics.INT:
81                 return SerUtils.readIntArray(is);
82             case MDStatics.SHORTW:
83                 return SerUtils.readShortWArray(is);
84             case MDStatics.SHORT:
85                 return SerUtils.readShortArray(is);
86             case MDStatics.CHARW:
87                 return SerUtils.readCharWArray(is);
88             case MDStatics.CHAR:
89                 return SerUtils.readCharArray(is);
90             case MDStatics.BOOLEANW:
91                 return SerUtils.readBooleanWArray(is);
92             case MDStatics.BOOLEAN:
93                 return SerUtils.readBooleanArray(is);
94             case MDStatics.BYTEW:
95                 return SerUtils.readByteWArray(is);
96             case MDStatics.BYTE:
97                 return SerUtils.readByteArray(is);
98             case MDStatics.DOUBLEW:
99                 return SerUtils.readDoubleWArray(is);
100             case MDStatics.DOUBLE:
101                 return SerUtils.readDoubleArray(is);
102             case MDStatics.FLOATW:
103                 return SerUtils.readFloatWArray(is);
104             case MDStatics.FLOAT:
105                 return SerUtils.readFloatArray(is);
106             case MDStatics.LONGW:
107                 return SerUtils.readLongWArray(is);
108             case MDStatics.LONG:
109                 return SerUtils.readLongArray(is);
110             case MDStatics.STRING:
111                 return SerUtils.readStringArray(is);
112             case MDStatics.LOCALE:
113                 return SerUtils.readLocaleArray(is);
114             case MDStatics.DATE:
115                 return SerUtils.readDateArray(is);
116             case MDStatics.OID:
117                 return SerUtils.readOIDArray(is);
118             default:
119                 return SerUtils.readObjectArray(is);
120         }
121     }
122
123     public static void writeArrayField(int type, OIDObjectOutput os,
124             Object JavaDoc toWrite) throws IOException {
125         switch (type) {
126             case MDStatics.INTW:
127                 SerUtils.writeIntegerArray((Integer JavaDoc[]) toWrite, os);
128                 break;
129             case MDStatics.INT:
130                 SerUtils.writeIntArray((int[])toWrite, os);
131                 break;
132             case MDStatics.SHORTW:
133                 SerUtils.writeShortWArray((Short JavaDoc[]) toWrite, os);
134                 break;
135             case MDStatics.SHORT:
136                 SerUtils.writeShortArray((short[])toWrite, os);
137                 break;
138             case MDStatics.CHARW:
139                 SerUtils.writeCharWArray((Character JavaDoc[]) toWrite, os);
140                 break;
141             case MDStatics.CHAR:
142                 SerUtils.writeCharArray((char[])toWrite, os);
143                 break;
144             case MDStatics.BOOLEANW:
145                 SerUtils.writeBooleanWArray((Boolean JavaDoc[]) toWrite, os);
146                 break;
147             case MDStatics.BOOLEAN:
148                 SerUtils.writeBooleanArray((boolean[])toWrite, os);
149                 break;
150             case MDStatics.BYTEW:
151                 SerUtils.writeByteWArray((Byte JavaDoc[]) toWrite, os);
152                 break;
153             case MDStatics.BYTE:
154                 SerUtils.writeByteArray((byte[])toWrite, os);
155                 break;
156             case MDStatics.DOUBLEW:
157                 SerUtils.writeDoubleWArray((Double JavaDoc[]) toWrite, os);
158                 break;
159             case MDStatics.DOUBLE:
160                 SerUtils.writeDoubleArray((double[])toWrite, os);
161                 break;
162             case MDStatics.FLOATW:
163                 SerUtils.writeFloatWArray((Float JavaDoc[]) toWrite, os);
164                 break;
165             case MDStatics.FLOAT:
166                 SerUtils.writeFloatArray((float[])toWrite, os);
167                 break;
168             case MDStatics.LONGW:
169                 SerUtils.writeLongWArray((Long JavaDoc[]) toWrite, os);
170                 break;
171             case MDStatics.LONG:
172                 SerUtils.writeLongArray((long[])toWrite, os);
173                 break;
174             case MDStatics.STRING:
175                 SerUtils.writeStringArray((String JavaDoc[])toWrite, os);
176                 break;
177             case MDStatics.LOCALE:
178                 SerUtils.writeLocaleArray((Locale JavaDoc[])toWrite, os);
179                 break;
180             case MDStatics.DATE:
181                 SerUtils.writeDateArray((Date JavaDoc[])toWrite, os);
182                 break;
183             case MDStatics.OID:
184                 SerUtils.writeOIDArray((OID[])toWrite, os);
185                 break;
186             default:
187                 writeObjectArray((Object JavaDoc[]) toWrite, os);
188         }
189     }
190
191     public static void writeLongWArray(Long JavaDoc[] array, ObjectOutput out) throws IOException {
192         if (array == null) {
193             out.writeBoolean(true);
194         } else {
195             out.writeBoolean(false);
196             int n = array.length;
197             out.writeInt(n);
198             for (int i = 0; i < n; i++) {
199                 if (array[i] == null) {
200                     out.writeByte(0);
201                 } else {
202                     out.writeByte(0);
203                     out.writeLong(array[i].longValue());
204                 }
205             }
206         }
207     }
208
209     public static Long JavaDoc[] readLongWArray(ObjectInput in) throws IOException {
210         if (in.readBoolean()) return null;
211         final int n = in.readInt();
212         final Long JavaDoc[] array = new Long JavaDoc[n];
213         for (int i = 0; i < n; i++) {
214             if (in.readByte() == 1) array[i] = new Long JavaDoc(in.readLong());
215         }
216         return array;
217     }
218
219     public static void writeLongArray(long[] array, ObjectOutput out) throws IOException {
220         if (array == null) {
221             out.writeBoolean(true);
222             return;
223         } else {
224             out.writeBoolean(false);
225         }
226         final int n = array.length;
227         out.writeInt(n);
228         for (int i = 0; i < n; i++) {
229             out.writeLong(array[i]);
230         }
231     }
232
233     public static long[] readLongArray(ObjectInput in) throws IOException {
234         if (in.readBoolean()) return null;
235         final int n = in.readInt();
236         final long[] array = new long[n];
237         for (int i = 0; i < n; i++) {
238             array[i] = in.readLong();
239         }
240         return array;
241     }
242
243     public static void writeBooleanWArray(Boolean JavaDoc[] array, ObjectOutput out) throws IOException {
244         if (array == null) {
245             out.writeBoolean(true);
246             return;
247         } else {
248             out.writeBoolean(false);
249         }
250         final int n = array.length;
251         out.writeInt(n);
252         for (int i = 0; i < n; i++) {
253             if (array[i] == null) {
254                 out.writeByte(0);
255             } else {
256                 out.writeByte(0);
257                 out.writeByte(array[i].booleanValue() == true ? 1 : 0);
258             }
259         }
260     }
261
262     public static Boolean JavaDoc[] readBooleanWArray(ObjectInput in) throws IOException {
263         if (in.readBoolean()) return null;
264         final int n = in.readInt();
265         final Boolean JavaDoc[] array = new Boolean JavaDoc[n];
266         for (int i = 0; i < n; i++) {
267             if (in.readByte() == 1) array[i] = new Boolean JavaDoc(in.readByte() == 1 ? true : false);
268         }
269         return array;
270     }
271
272     public static void writeBooleanArray(boolean[] array, ObjectOutput out) throws IOException {
273         if (array == null) {
274             out.writeBoolean(true);
275             return;
276         } else {
277             out.writeBoolean(false);
278         }
279         final int n = array.length;
280         out.writeInt(n);
281         for (int i = 0; i < n; i++) {
282             out.writeBoolean(array[i]);
283         }
284     }
285
286     public static boolean[] readBooleanArray(ObjectInput in) throws IOException {
287         if (in.readBoolean()) return null;
288         final int n = in.readInt();
289         final boolean[] array = new boolean[n];
290         for (int i = 0; i < n; i++) {
291             array[i] = in.readBoolean();
292         }
293         return array;
294     }
295
296     public static void writeIntegerArray(Integer JavaDoc[] array, ObjectOutput out) throws IOException {
297         if (array == null) {
298             out.writeBoolean(true);
299             return;
300         } else {
301             out.writeBoolean(false);
302         }
303         final int n = array.length;
304         out.writeInt(n);
305         for (int i = 0; i < n; i++) {
306             if (array[i] == null) {
307                 out.writeByte(0);
308             } else {
309                 out.writeByte(1);
310                 out.writeInt(array[i].intValue());
311             }
312         }
313     }
314
315     public static Integer JavaDoc[] readIntegerArray(ObjectInput in) throws IOException {
316         if (in.readBoolean()) return null;
317         final int n = in.readInt();
318         final Integer JavaDoc[] array = new Integer JavaDoc[n];
319         for (int i = 0; i < n; i++) {
320             if (in.readByte() == 1) {
321                 array[i] = new Integer JavaDoc(in.readInt());
322             }
323         }
324         return array;
325     }
326
327     public static void writeIntArray(int[] array, ObjectOutput out) throws IOException {
328         if (array == null) {
329             out.writeBoolean(true);
330             return;
331         } else {
332             out.writeBoolean(false);
333         }
334         final int n = array.length;
335         out.writeInt(n);
336         for (int i = 0; i < n; i++) {
337             out.writeInt(array[i]);
338         }
339     }
340
341     public static int[] readIntArray(ObjectInput in) throws IOException {
342         if (in.readBoolean()) {
343             return null;
344         }
345         int n = in.readInt();
346         int[] array = new int[n];
347         for (int i = 0; i < n; i++) {
348             array[i] = in.readInt();
349         }
350         return array;
351     }
352
353     public static void writeByteWArray(Byte JavaDoc[] array, ObjectOutput out) throws IOException {
354         if (array == null) {
355             out.writeBoolean(true);
356             return;
357         } else {
358             out.writeBoolean(false);
359         }
360         final int n = array.length;
361         out.writeInt(n);
362         for (int i = 0; i < n; i++) {
363             if (array[i] == null) {
364                 out.writeByte(0);
365             } else {
366                 out.writeByte(1);
367                 out.writeByte(array[i].byteValue());
368             }
369         }
370     }
371
372     public static Byte JavaDoc[] readByteWArray(ObjectInput in) throws IOException {
373         if (in.readBoolean()) return null;
374         final int n = in.readInt();
375         final Byte JavaDoc[] array = new Byte JavaDoc[n];
376         for (int i = 0; i < n; i++) {
377             if (in.readByte() == 1) array[i] = new Byte JavaDoc(in.readByte());
378         }
379         return array;
380     }
381
382     public static void writeByteArray(byte[] array, ObjectOutput out) throws IOException {
383         if (array == null) {
384             out.writeBoolean(true);
385             return;
386         } else {
387             out.writeBoolean(false);
388         }
389         final int n = array.length;
390         out.writeInt(n);
391         for (int i = 0; i < n; i++) {
392             out.writeByte(array[i]);
393         }
394     }
395
396     public static byte[] readByteArray(ObjectInput in) throws IOException {
397         if (in.readBoolean()) return null;
398         final int n = in.readInt();
399         final byte[] array = new byte[n];
400         for (int i = 0; i < n; i++) {
401             array[i] = in.readByte();
402         }
403         return array;
404     }
405
406     public static void writeShortWArray(Short JavaDoc[] array, ObjectOutput out) throws IOException {
407         if (array == null) {
408             out.writeBoolean(true);
409             return;
410         } else {
411             out.writeBoolean(false);
412         }
413         final int n = array.length;
414         out.writeInt(n);
415         for (int i = 0; i < n; i++) {
416             if (array[i] == null) {
417                 out.writeByte(0);
418             } else {
419                 out.writeByte(0);
420                 out.writeShort(array[i].shortValue());
421             }
422         }
423     }
424
425     public static Short JavaDoc[] readShortWArray(ObjectInput in) throws IOException {
426         if (in.readBoolean()) return null;
427         final int n = in.readInt();
428         final Short JavaDoc[] array = new Short JavaDoc[n];
429         for (int i = 0; i < n; i++) {
430             if (in.readByte() == 1) array[i] = new Short JavaDoc(in.readShort());
431         }
432         return array;
433     }
434
435     public static void writeShortArray(short[] array, ObjectOutput out) throws IOException {
436         if (array == null) {
437             out.writeBoolean(true);
438             return;
439         } else {
440             out.writeBoolean(false);
441         }
442         final int n = array.length;
443         out.writeInt(n);
444         for (int i = 0; i < n; i++) {
445             out.writeShort(array[i]);
446         }
447     }
448
449     public static short[] readShortArray(ObjectInput in) throws IOException {
450         if (in.readBoolean()) return null;
451         final int n = in.readInt();
452         final short[] array = new short[n];
453         for (int i = 0; i < n; i++) {
454             array[i] = in.readShort();
455         }
456         return array;
457     }
458
459     public static void writeCharWArray(Character JavaDoc[] array, ObjectOutput out) throws IOException {
460         if (array == null) {
461             out.writeBoolean(true);
462             return;
463         } else {
464             out.writeBoolean(false);
465         }
466         final int n = array.length;
467         out.writeInt(n);
468         for (int i = 0; i < n; i++) {
469             if (array[i] == null) {
470                 out.writeByte(0);
471             } else {
472                 out.writeByte(0);
473                 out.writeChar(array[i].charValue());
474             }
475         }
476     }
477
478     public static Character JavaDoc[] readCharWArray(ObjectInput in) throws IOException {
479         if (in.readBoolean()) return null;
480         final int n = in.readInt();
481         final Character JavaDoc[] array = new Character JavaDoc[n];
482         for (int i = 0; i < n; i++) {
483             if (in.readByte() == 1) array[i] = new Character JavaDoc(in.readChar());
484         }
485         return array;
486     }
487
488     public static void writeCharArray(char[] array, ObjectOutput out) throws IOException {
489         if (array == null) {
490             out.writeBoolean(true);
491             return;
492         } else {
493             out.writeBoolean(false);
494         }
495         final int n = array.length;
496         out.writeInt(n);
497         for (int i = 0; i < n; i++) {
498             out.writeChar(array[i]);
499         }
500     }
501
502     public static char[] readCharArray(ObjectInput in) throws IOException {
503         if (in.readBoolean()) return null;
504         final int n = in.readInt();
505         final char[] array = new char[n];
506         for (int i = 0; i < n; i++) {
507             array[i] = in.readChar();
508         }
509         return array;
510     }
511
512     public static void writeFloatWArray(Float JavaDoc[] array, ObjectOutput out) throws IOException {
513         if (array == null) {
514             out.writeBoolean(true);
515             return;
516         } else {
517             out.writeBoolean(false);
518         }
519         final int n = array.length;
520         out.writeInt(n);
521         for (int i = 0; i < n; i++) {
522             if (array[i] == null) {
523                 out.writeByte(0);
524             } else {
525                 out.writeByte(0);
526                 out.writeFloat(array[i].floatValue());
527             }
528         }
529     }
530
531     public static Float JavaDoc[] readFloatWArray(ObjectInput in) throws IOException {
532         if (in.readBoolean()) return null;
533         final int n = in.readInt();
534         final Float JavaDoc[] array = new Float JavaDoc[n];
535         for (int i = 0; i < n; i++) {
536             if (in.readByte() == 1) array[i] = new Float JavaDoc(in.readFloat());
537         }
538         return array;
539     }
540
541     public static void writeFloatArray(float[] array, ObjectOutput out) throws IOException {
542         if (array == null) {
543             out.writeBoolean(true);
544             return;
545         } else {
546             out.writeBoolean(false);
547         }
548         final int n = array.length;
549         out.writeInt(n);
550         for (int i = 0; i < n; i++) {
551             out.writeFloat(array[i]);
552         }
553     }
554
555     public static float[] readFloatArray(ObjectInput in) throws IOException {
556         if (in.readBoolean()) return null;
557         final int n = in.readInt();
558         final float[] array = new float[n];
559         for (int i = 0; i < n; i++) {
560             array[i] = in.readFloat();
561         }
562         return array;
563     }
564
565     public static void writeDoubleWArray(Double JavaDoc[] array, ObjectOutput out) throws IOException {
566         if (array == null) {
567             out.writeBoolean(true);
568             return;
569         } else {
570             out.writeBoolean(false);
571         }
572         final int n = array.length;
573         out.writeInt(n);
574         for (int i = 0; i < n; i++) {
575             if (array[i] == null) {
576                 out.writeByte(0);
577             } else {
578                 out.writeByte(0);
579                 out.writeDouble(array[i].doubleValue());
580             }
581         }
582     }
583
584     public static Double JavaDoc[] readDoubleWArray(ObjectInput in) throws IOException {
585         if (in.readBoolean()) return null;
586         final int n = in.readInt();
587         final Double JavaDoc[] array = new Double JavaDoc[n];
588         for (int i = 0; i < n; i++) {
589             if (in.readByte() == 1) array[i] = new Double JavaDoc(in.readDouble());
590         }
591         return array;
592     }
593
594     public static void writeDoubleArray(double[] array, ObjectOutput out) throws IOException {
595         if (array == null) {
596             out.writeBoolean(true);
597             return;
598         } else {
599             out.writeBoolean(false);
600         }
601         final int n = array.length;
602         out.writeInt(n);
603         for (int i = 0; i < n; i++) {
604             out.writeDouble(array[i]);
605         }
606     }
607
608     public static double[] readDoubleArray(ObjectInput in) throws IOException {
609         if (in.readBoolean()) return null;
610         final int n = in.readInt();
611         final double[] array = new double[n];
612         for (int i = 0; i < n; i++) {
613             array[i] = in.readDouble();
614         }
615         return array;
616     }
617
618     public static void writeStringArray(String JavaDoc[] array, ObjectOutput out) throws IOException {
619         if (array == null) {
620             out.writeBoolean(true);
621             return;
622         } else {
623             out.writeBoolean(false);
624         }
625         final int n = array.length;
626         out.writeInt(n);
627         for (int i = 0; i < n; i++) {
628             if (array[i] == null) {
629                 out.writeByte(0);
630             } else {
631                 out.writeByte(1);
632                 out.writeUTF(array[i]);
633             }
634         }
635     }
636
637     public static String JavaDoc[] readStringArray(ObjectInput in) throws IOException {
638         if (in.readBoolean()) return null;
639         final int n = in.readInt();
640         final String JavaDoc[] array = new String JavaDoc[n];
641         for (int i = 0; i < n; i++) {
642             if (in.readByte() == 1) {
643                 array[i] = in.readUTF();
644             }
645         }
646         return array;
647     }
648
649     public static void writeDateArray(Date JavaDoc[] array, ObjectOutput out) throws IOException {
650         if (array == null) {
651             out.writeBoolean(true);
652             return;
653         } else {
654             out.writeBoolean(false);
655         }
656         final int n = array.length;
657         out.writeInt(n);
658         for (int i = 0; i < n; i++) {
659             if (array[i] == null) {
660                 out.writeByte(0);
661             } else {
662                 out.writeByte(1);
663                 out.writeLong(array[i].getTime());
664             }
665         }
666     }
667
668     public static Date JavaDoc[] readDateArray(ObjectInput in) throws IOException {
669         if (in.readBoolean()) return null;
670         final int n = in.readInt();
671         final Date JavaDoc[] array = new Date JavaDoc[n];
672         for (int i = 0; i < n; i++) {
673             if (in.readByte() == 1) {
674                 array[i] = new Date JavaDoc(in.readLong());
675             }
676         }
677         return array;
678     }
679
680     public static void writeObjectArray(Object JavaDoc[] array, ObjectOutput out) throws IOException {
681         out.writeObject(array);
682     }
683
684     public static Object JavaDoc[] readObjectArray(ObjectInput in) throws IOException,
685             ClassNotFoundException JavaDoc {
686         return (Object JavaDoc[])in.readObject();
687     }
688
689     public static void writeLocaleArray(Locale JavaDoc[] array, ObjectOutput out) throws IOException {
690         if (array == null) {
691             out.writeBoolean(true);
692             return;
693         } else {
694             out.writeBoolean(false);
695         }
696         final int n = array.length;
697         out.writeInt(n);
698         for (int i = 0; i < n; i++) {
699             if (array[i] == null) {
700                 out.writeByte(0);
701             } else {
702                 out.writeByte(1);
703                 Locale JavaDoc l = (Locale JavaDoc)array[i];
704                 out.writeUTF(l.getLanguage());
705                 out.writeUTF(l.getCountry());
706                 out.writeUTF(l.getVariant());
707             }
708         }
709     }
710
711     public static Locale JavaDoc[] readLocaleArray(ObjectInput in) throws IOException {
712         if (in.readBoolean()) return null;
713         final int n = in.readInt();
714         final Locale JavaDoc[] array = new Locale JavaDoc[n];
715         for (int i = 0; i < n; i++) {
716             if (in.readByte() == 1) {
717                 array[i] = new Locale JavaDoc(in.readUTF(), in.readUTF(), in.readUTF());
718             }
719         }
720         return array;
721     }
722
723     public static void writeSimpleField(FieldMetaData fmd, ObjectOutput os,
724             Object JavaDoc toWrite) throws IOException {
725         writeSimpleField(fmd.typeCode, os, toWrite);
726     }
727
728     public static void writeSimpleField(int typeCode, ObjectOutput os, Object JavaDoc toWrite) throws IOException {
729         if (toWrite == null) {
730             os.writeByte(0);
731             return;
732         } else {
733             os.writeByte(1);
734             switch (typeCode) {
735                 case MDStatics.INTW:
736                 case MDStatics.INT:
737                     os.writeInt(((Integer JavaDoc)toWrite).intValue());
738                     break;
739                 case MDStatics.CHARW:
740                 case MDStatics.CHAR:
741                     os.writeChar(((Character JavaDoc)toWrite).charValue());
742                     break;
743                 case MDStatics.SHORTW:
744                 case MDStatics.SHORT:
745                     os.writeShort(((Short JavaDoc)toWrite).shortValue());
746                     break;
747                 case MDStatics.STRING:
748                     Utils.writeLongUTF8((String JavaDoc) toWrite, os);
749                     break;
750                 case MDStatics.BOOLEANW:
751                 case MDStatics.BOOLEAN:
752                     os.writeBoolean(((Boolean JavaDoc)toWrite).booleanValue());
753                     break;
754                 case MDStatics.BYTEW:
755                 case MDStatics.BYTE:
756                     os.writeByte(((Byte JavaDoc)toWrite).byteValue());
757                     break;
758                 case MDStatics.DOUBLEW:
759                 case MDStatics.DOUBLE:
760                     os.writeDouble(((Double JavaDoc)toWrite).doubleValue());
761                     break;
762                 case MDStatics.FLOATW:
763                 case MDStatics.FLOAT:
764                     os.writeFloat(((Float JavaDoc)toWrite).floatValue());
765                     break;
766                 case MDStatics.LONGW:
767                 case MDStatics.LONG:
768                     os.writeLong(((Long JavaDoc)toWrite).longValue());
769                     break;
770                 case MDStatics.DATE:
771                     os.writeLong(((Date JavaDoc)toWrite).getTime());
772                     break;
773                 case MDStatics.LOCALE:
774                     final Locale JavaDoc l = (Locale JavaDoc)toWrite;
775                     os.writeUTF(l.getLanguage());
776                     os.writeUTF(l.getCountry());
777                     os.writeUTF(l.getVariant());
778                     break;
779                 case MDStatics.BIGDECIMAL:
780                     os.writeUTF(toWrite.toString());
781                     break;
782                 case MDStatics.BIGINTEGER:
783                     os.writeUTF(toWrite.toString());
784                     break;
785                 default:
786                     os.writeObject(toWrite);
787                     break;
788             }
789         }
790     }
791
792     public static void writeObject(Object JavaDoc o, ObjectOutput dos) throws IOException {
793         dos.writeObject(o);
794     }
795
796     public static Object JavaDoc readSimpleField(FieldMetaData fmd, ObjectInput is)
797             throws IOException, ClassNotFoundException JavaDoc {
798         return readSimpleField(fmd.typeCode, is);
799     }
800
801     public static Object JavaDoc readSimpleField(int typeCode, ObjectInput is)
802             throws IOException, ClassNotFoundException JavaDoc {
803         if (is.readByte() == 0) {
804             return null;
805         } else {
806             switch (typeCode) {
807                 case MDStatics.INTW:
808                 case MDStatics.INT:
809                     return new Integer JavaDoc(is.readInt());
810                 case MDStatics.CHARW:
811                 case MDStatics.CHAR:
812                     return new Character JavaDoc(is.readChar());
813                 case MDStatics.SHORTW:
814                 case MDStatics.SHORT:
815                     return new Short JavaDoc(is.readShort());
816                 case MDStatics.STRING:
817                     return Utils.readLongUTF8(is);
818                 case MDStatics.BOOLEANW:
819                 case MDStatics.BOOLEAN:
820                     return new Boolean JavaDoc(is.readBoolean());
821                 case MDStatics.BYTEW:
822                 case MDStatics.BYTE:
823                     return new Byte JavaDoc(is.readByte());
824                 case MDStatics.DOUBLEW:
825                 case MDStatics.DOUBLE:
826                     return new Double JavaDoc(is.readDouble());
827                 case MDStatics.FLOATW:
828                 case MDStatics.FLOAT:
829                     return new Float JavaDoc(is.readFloat());
830                 case MDStatics.LONGW:
831                 case MDStatics.LONG:
832                     return new Long JavaDoc(is.readLong());
833                 case MDStatics.DATE:
834                     return new Date JavaDoc(is.readLong());
835                 case MDStatics.LOCALE:
836                     return new Locale JavaDoc(is.readUTF(), is.readUTF(), is.readUTF());
837                 case MDStatics.BIGDECIMAL:
838                     return new BigDecimal JavaDoc(is.readUTF());
839                 case MDStatics.BIGINTEGER:
840                     return new BigInteger JavaDoc(is.readUTF());
841                 default:
842                     return is.readObject();
843             }
844         }
845     }
846
847     public static Object JavaDoc readObject(ObjectInput dis) throws IOException,
848             ClassNotFoundException JavaDoc {
849         return dis.readObject();
850     }
851
852     public static Object JavaDoc[] readCollectionArray(FieldMetaData fmd,
853             OIDObjectInput in)
854             throws IOException, ClassNotFoundException JavaDoc {
855         if (in.readBoolean()) return null;
856         if (fmd.elementTypeMetaData != null) {
857             return readOIDObjectArray(in);
858         } else {
859             return readColObjectArray(fmd.elementTypeCode, in);
860         }
861     }
862
863     public static void writeCollectionArray(FieldMetaData fmd, Object JavaDoc data,
864             OIDObjectOutput out) throws IOException {
865         if (data == null) {
866             out.writeBoolean(true);
867             return;
868         } else {
869             out.writeBoolean(false);
870         }
871         if (fmd.elementTypeMetaData != null) {
872             writeOIDObjectArray((Object JavaDoc[])data, out);
873         } else {
874             writeColObjectArray((Object JavaDoc[])data, out, fmd.elementTypeCode);
875         }
876     }
877
878     /**
879      * This writes the Object[] of oids that is send as part of a collection read
880      * from the store.
881      */

882     public static void writeOIDObjectArray(Object JavaDoc[] oids,
883             OIDObjectOutput out) throws IOException {
884         int n = oids.length;
885         out.writeInt(n);
886         for (int j = 0; j < n; j++) {
887             out.write((OID)oids[j]);
888         }
889     }
890
891     public static Object JavaDoc[] readOIDObjectArray(OIDObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
892         OID[] oids = new OID[in.readInt()];
893         for (int j = 0; j < oids.length; j++) {
894             oids[j] = in.readOID();
895         }
896         return oids;
897     }
898
899     private static void writeColObjectArray(Object JavaDoc[] ar, ObjectOutput out,
900             int typeCode) throws IOException {
901         int n = ar.length;
902         out.writeInt(n);
903         switch (typeCode) {
904             case MDStatics.INTW:
905             case MDStatics.INT:
906                 for (int i = 0; i < n; i++) {
907                     if (ar[i] == null) {
908                         out.writeByte(0);
909                         continue;
910                     } else {
911                         out.writeByte(1);
912                     }
913                     out.writeInt(((Integer JavaDoc)ar[i]).intValue());
914                 }
915                 break;
916             case MDStatics.CHARW:
917             case MDStatics.CHAR:
918                 for (int i = 0; i < n; i++) {
919                     if (ar[i] == null) {
920                         out.writeByte(0);
921                         continue;
922                     } else {
923                         out.writeByte(1);
924                     }
925                     out.writeChar(((Character JavaDoc)ar[i]).charValue());
926                 }
927                 break;
928             case MDStatics.SHORTW:
929             case MDStatics.SHORT:
930                 for (int i = 0; i < n; i++) {
931                     if (ar[i] == null) {
932                         out.writeByte(0);
933                         continue;
934                     } else {
935                         out.writeByte(1);
936                     }
937                     out.writeShort(((Short JavaDoc)ar[i]).shortValue());
938                 }
939                 break;
940             case MDStatics.STRING:
941                 for (int i = 0; i < n; i++) {
942                     if (ar[i] == null) {
943                         out.writeByte(0);
944                         continue;
945                     } else {
946                         out.writeByte(1);
947                     }
948                     out.writeUTF((String JavaDoc)ar[i]);
949                 }
950                 break;
951             case MDStatics.BOOLEANW:
952             case MDStatics.BOOLEAN:
953                 for (int i = 0; i < n; i++) {
954                     if (ar[i] == null) {
955                         out.writeByte(0);
956                         continue;
957                     } else {
958                         out.writeByte(1);
959                     }
960                     out.writeBoolean(((Boolean JavaDoc)ar[i]).booleanValue());
961                 }
962                 break;
963             case MDStatics.BYTEW:
964             case MDStatics.BYTE:
965                 for (int i = 0; i < n; i++) {
966                     if (ar[i] == null) {
967                         out.writeByte(0);
968                         continue;
969                     } else {
970                         out.writeByte(1);
971                     }
972                     out.writeByte(((Byte JavaDoc)ar[i]).byteValue());
973                 }
974                 break;
975             case MDStatics.DOUBLEW:
976             case MDStatics.DOUBLE:
977                 for (int i = 0; i < n; i++) {
978                     if (ar[i] == null) {
979                         out.writeByte(0);
980                         continue;
981                     } else {
982                         out.writeByte(1);
983                     }
984                     out.writeDouble(((Double JavaDoc)ar[i]).doubleValue());
985                 }
986                 break;
987             case MDStatics.FLOATW:
988             case MDStatics.FLOAT:
989                 for (int i = 0; i < n; i++) {
990                     if (ar[i] == null) {
991                         out.writeByte(0);
992                         continue;
993                     } else {
994                         out.writeByte(1);
995                     }
996                     out.writeFloat(((Float JavaDoc)ar[i]).floatValue());
997                 }
998                 break;
999             case MDStatics.LONGW:
1000            case MDStatics.LONG:
1001                for (int i = 0; i < n; i++) {
1002                    if (ar[i] == null) {
1003                        out.writeByte(0);
1004                        continue;
1005                    } else {
1006                        out.writeByte(1);
1007                    }
1008                    out.writeLong(((Long JavaDoc)ar[i]).longValue());
1009                }
1010                break;
1011            case MDStatics.DATE:
1012                for (int i = 0; i < n; i++) {
1013                    if (ar[i] == null) {
1014                        out.writeByte(0);
1015                        continue;
1016                    } else {
1017                        out.writeByte(1);
1018                    }
1019                    out.writeLong(((Long JavaDoc)ar[i]).longValue());
1020                }
1021                break;
1022            case MDStatics.LOCALE:
1023                break;
1024            case MDStatics.BIGDECIMAL:
1025                break;
1026            case MDStatics.BIGINTEGER:
1027                break;
1028            default:
1029                throw BindingSupportImpl.getInstance().internal(
1030                        "writeColObjectArray for " + typeCode + " is not supported");
1031        }
1032    }
1033
1034    private static Object JavaDoc[] readColObjectArray(int typeCode,
1035            ObjectInput in) throws IOException {
1036        final int n = in.readInt();
1037        final Object JavaDoc[] ar = new Object JavaDoc[n];
1038        switch (typeCode) {
1039            case MDStatics.INTW:
1040            case MDStatics.INT:
1041                for (int i = 0; i < n; i++) {
1042                    if (in.readByte() == 0) continue;
1043                    ar[i] = new Integer JavaDoc(in.readInt());
1044                }
1045                break;
1046            case MDStatics.CHARW:
1047            case MDStatics.CHAR:
1048                for (int i = 0; i < n; i++) {
1049                    if (in.readByte() == 0) continue;
1050                    ar[i] = new Character JavaDoc(in.readChar());
1051                }
1052                break;
1053            case MDStatics.SHORTW:
1054            case MDStatics.SHORT:
1055                for (int i = 0; i < n; i++) {
1056                    if (in.readByte() == 0) continue;
1057                    ar[i] = new Short JavaDoc(in.readShort());
1058                }
1059                break;
1060            case MDStatics.STRING:
1061                for (int i = 0; i < n; i++) {
1062                    if (in.readByte() == 0) continue;
1063                    ar[i] = in.readUTF();
1064                }
1065                return ar;
1066            case MDStatics.BOOLEANW:
1067            case MDStatics.BOOLEAN:
1068                for (int i = 0; i < n; i++) {
1069                    if (in.readByte() == 0) continue;
1070                    ar[i] = new Boolean JavaDoc(in.readBoolean());
1071                }
1072                break;
1073            case MDStatics.BYTEW:
1074            case MDStatics.BYTE:
1075                for (int i = 0; i < n; i++) {
1076                    if (in.readByte() == 0) continue;
1077                    ar[i] = new Byte JavaDoc(in.readByte());
1078                }
1079                break;
1080            case MDStatics.DOUBLEW:
1081            case MDStatics.DOUBLE:
1082                for (int i = 0; i < n; i++) {
1083                    if (in.readByte() == 0) continue;
1084                    ar[i] = new Double JavaDoc(in.readDouble());
1085                }
1086                break;
1087            case MDStatics.FLOATW:
1088            case MDStatics.FLOAT:
1089                for (int i = 0; i < n; i++) {
1090                    if (in.readByte() == 0) continue;
1091                    ar[i] = new Float JavaDoc(in.readFloat());
1092                }
1093                break;
1094            case MDStatics.LONGW:
1095            case MDStatics.LONG:
1096                for (int i = 0; i < n; i++) {
1097                    if (in.readByte() == 0) continue;
1098                    ar[i] = new Long JavaDoc(in.readLong());
1099                }
1100                break;
1101            case MDStatics.DATE:
1102                for (int i = 0; i < n; i++) {
1103                    if (in.readByte() == 0) continue;
1104                    ar[i] = new Date JavaDoc(in.readLong());
1105                }
1106                break;
1107            case MDStatics.LOCALE:
1108                break;
1109            case MDStatics.BIGDECIMAL:
1110                break;
1111            case MDStatics.BIGINTEGER:
1112                break;
1113            default:
1114                throw BindingSupportImpl.getInstance().internal(
1115                        "readColObjectArray for " + typeCode + " is not supported");
1116        }
1117        return ar;
1118    }
1119
1120    public static void writeMapEntries(FieldMetaData fmd, OIDObjectOutput out,
1121            MapEntries data) throws IOException {
1122        if (data == null) {
1123            out.writeBoolean(true);
1124            return;
1125        } else {
1126            out.writeBoolean(false);
1127        }
1128        if (fmd.keyTypeMetaData != null) {
1129            writeOIDObjectArray(data.keys, out);
1130        } else {
1131            writeColObjectArray(data.keys, out, fmd.keyTypeCode);
1132        }
1133        if (fmd.elementTypeMetaData != null) {
1134            writeOIDObjectArray(data.values, out);
1135        } else {
1136            writeColObjectArray(data.values, out, fmd.elementTypeCode);
1137        }
1138    }
1139
1140    public static MapEntries readMapEntries(FieldMetaData fmd,
1141            OIDObjectInput in)
1142            throws IOException, ClassNotFoundException JavaDoc {
1143        if (in.readBoolean() == true) {
1144            return null;
1145        }
1146        final MapEntries mapEntries = new MapEntries();
1147        if (fmd.keyTypeMetaData != null) {
1148            mapEntries.keys = readOIDObjectArray(in);
1149        } else {
1150            mapEntries.keys = readColObjectArray(fmd.keyTypeCode, in);
1151        }
1152        if (fmd.elementTypeMetaData != null) {
1153            mapEntries.values = readOIDObjectArray(in);
1154        } else {
1155            mapEntries.values = readColObjectArray(fmd.elementTypeCode, in);
1156        }
1157        return mapEntries;
1158    }
1159
1160    private static final int COL_NULL = 0;
1161    private static final int COL_ARRAY = 1;
1162    private static final int COL_MAP_ENTRIES = 2;
1163    private static final int COL_DIFF_ORDERED = 3;
1164    private static final int COL_DIFF_UNORDERED = 4;
1165    private static final int COL_DIFF_MAP = 5;
1166
1167    /**
1168     * Read the value of a collection or map field from the stream.
1169     * @see #writeCollectionOrMapField
1170     */

1171    public static Object JavaDoc readCollectionOrMapField(OIDObjectInput in,
1172            FieldMetaData fmd)
1173            throws IOException, ClassNotFoundException JavaDoc {
1174        CollectionDiff d;
1175        int code = in.readByte();
1176        switch (code) {
1177            case COL_NULL:
1178                return null;
1179            case COL_ARRAY:
1180                return SerUtils.readCollectionArray(fmd, in);
1181            case COL_MAP_ENTRIES:
1182                return SerUtils.readMapEntries(fmd, in);
1183            case COL_DIFF_UNORDERED:
1184                d = new UnorderedCollectionDiff(fmd);
1185                d.readExternal(in);
1186                return d;
1187            case COL_DIFF_ORDERED:
1188                d = new OrderedCollectionDiff(fmd);
1189                d.readExternal(in);
1190                return d;
1191            case COL_DIFF_MAP:
1192                d = new MapDiff(fmd);
1193                d.readExternal(in);
1194                return d;
1195        }
1196        throw BindingSupportImpl.getInstance().internal(
1197                "Unknown collection field code: " + code);
1198    }
1199
1200    /**
1201     * Write the value of a collection or map field to the stream. This
1202     * can handle raw collection or map data as well as diffs.
1203     * @see #readCollectionOrMapField
1204     */

1205    public static void writeCollectionOrMapField(OIDObjectOutput out,
1206            FieldMetaData fmd, Object JavaDoc data) throws IOException {
1207        if (data instanceof CollectionDiff) {
1208            int code;
1209            if (data instanceof UnorderedCollectionDiff) {
1210                code = COL_DIFF_UNORDERED;
1211            } else if (data instanceof OrderedCollectionDiff) {
1212                code = COL_DIFF_ORDERED;
1213            } else if (data instanceof MapDiff) {
1214                code = COL_DIFF_MAP;
1215            } else {
1216                throw BindingSupportImpl.getInstance().internal(
1217                        "Unknown CollectionDiff class: " +
1218                        data.getClass().getName());
1219            }
1220            out.writeByte(code);
1221            ((CollectionDiff)data).writeExternal(out);
1222        } else if (data == null) {
1223            out.writeByte(COL_NULL);
1224        } else if (data instanceof MapEntries) {
1225            out.writeByte(COL_MAP_ENTRIES);
1226            SerUtils.writeMapEntries(fmd, out, (MapEntries)data);
1227        } else {
1228            out.writeByte(COL_ARRAY);
1229            SerUtils.writeCollectionArray(fmd, data, out);
1230        }
1231    }
1232
1233}
1234
Popular Tags