KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > storage > VectorUtils


1 package snow.utils.storage;
2
3 import java.io.*;
4 import java.util.Vector JavaDoc;
5 import java.util.List JavaDoc;
6
7 public final class VectorUtils {
8
9   // Constants used to map classnames into integers
10
// used by the vectorrepresentation methods :
11
// DO NOT CHANGE - or all designfiles saved cannot be read in anymore.
12
public static final int IsVectorContent = 1;
13   public static final int IsIntegerContent = 2;
14   public static final int IsBooleanContent = 3;
15   public static final int IsDoubleContent = 4;
16   public static final int IsStringContent = 5;
17   public static final int IsbyteArrayContent = 10;
18   public static final int IsintArrayContent = 11;
19   public static final int IsfloatArrayContent = 12;
20   public static final int IsdoubleArrayContent = 13;
21   public static final int IsStringArrayContent = 14;
22
23   public static final int IsLongStringContent = 20;
24   public static final int IsLongContent = 21;
25
26
27
28  /**
29   * writes the vector (consisting of elementary types)
30   * inverse method is streamToVector
31   */

32 @SuppressWarnings JavaDoc("unchecked")
33   public static final void vectorToStream( final DataOutputStream out, final List JavaDoc<Object JavaDoc> theVector ) throws Exception JavaDoc
34   {
35     vectorToStreamRecurse(out, theVector);
36   }
37
38
39
40
41
42
43  /**
44   * Writes the vector (consisting of elementary types)
45   * inverse method is streamToVector
46   */

47 @SuppressWarnings JavaDoc("unchecked")
48   private static final void vectorToStreamRecurse( final DataOutputStream out ,
49                                              final List JavaDoc<Object JavaDoc> theVector ) throws Exception JavaDoc
50   {
51       if (theVector == null)
52       {
53         System.out.println("VectorUtils vectorToStream: null vector received!");
54         out.writeInt( IsVectorContent );
55         // and send its size right after this :
56
Vector JavaDoc<Object JavaDoc> thisVector = new Vector JavaDoc<Object JavaDoc>();
57         int thisSize = thisVector.size();
58         out.writeInt(thisSize);
59       }
60       else
61       {
62         out.writeInt( IsVectorContent );
63         // and send its size right after this :
64
int thisSize = theVector.size();
65         out.writeInt(thisSize);
66
67         for( int i=0; i < thisSize; i++)
68         {
69           Object JavaDoc thisContent = theVector.get(i);
70           if( thisContent instanceof Integer JavaDoc )
71           {
72             out.writeInt( IsIntegerContent );
73             out.writeInt( (Integer JavaDoc) thisContent );
74           }
75           else if( thisContent instanceof Double JavaDoc )
76           {
77             out.writeInt( IsDoubleContent );
78             out.writeDouble( (Double JavaDoc) thisContent );
79           }
80           else if( thisContent instanceof Long JavaDoc )
81           {
82             out.writeInt( IsLongContent );
83             out.writeLong( (Long JavaDoc) thisContent );
84           }
85           else if( thisContent instanceof String JavaDoc )
86           {
87             // UTF is limited to 65535 bytes !, we must check
88
//
89
String JavaDoc str = (String JavaDoc) thisContent;
90             int len = calculateUTFLength(str);
91             //System.out.println("len="+len);
92
if(len<65535)
93             {
94               //if(len>10000) System.out.println("Writing a "+len+" String in utf-8");
95
out.writeInt( IsStringContent );
96               out.writeUTF( str );
97             }
98             else
99             {
100               //System.out.println("Writing a "+len+" String in utf-16");
101
out.writeInt( IsLongStringContent );
102               byte[] theArray = str.getBytes("UTF-16");
103               out.writeInt( theArray.length );
104               for( int index=0; index < theArray.length; index++ )
105               {
106                  out.writeByte(theArray[index]);
107               }
108             }
109           }
110           else if( thisContent instanceof Boolean JavaDoc )
111           {
112             out.writeInt( IsBooleanContent );
113             out.writeBoolean( (Boolean JavaDoc) thisContent );
114           }
115           else if( thisContent instanceof byte[] )
116           {
117             byte[] theArray = (byte[])thisContent;
118             out.writeInt( IsbyteArrayContent );
119             out.writeInt( theArray.length );
120             for( int index=0; index < theArray.length; index++ )
121              {
122               out.writeByte(theArray[index]);
123              }
124           }
125           else if( thisContent instanceof int[] )
126           {
127             int[] theArray = (int[])thisContent;
128             out.writeInt( IsintArrayContent );
129             out.writeInt( theArray.length );
130             for( int index=0; index < theArray.length; index++ )
131              {
132               out.writeInt(theArray[index]);
133              }
134           }
135           else if( thisContent instanceof double[] )
136           {
137             double[] theArray = (double[])thisContent;
138             out.writeInt( IsdoubleArrayContent );
139             out.writeInt( theArray.length );
140             for( int index=0; index < theArray.length; index++ )
141              {
142               out.writeDouble(theArray[index]);
143              }
144           }
145           else if( thisContent instanceof float[] )
146           {
147             float[] theArray = (float[])thisContent;
148             out.writeInt( IsfloatArrayContent );
149             out.writeInt( theArray.length );
150             for( int index=0; index < theArray.length; index++ )
151              {
152               out.writeFloat(theArray[index]);
153              }
154           }
155           else if( thisContent instanceof String JavaDoc[] )
156           {
157             String JavaDoc[] theArray = (String JavaDoc[])thisContent;
158             out.writeInt( IsStringArrayContent );
159             out.writeInt( theArray.length );
160             for( int index=0; index < theArray.length; index++ )
161              {
162               out.writeUTF(theArray[index]);
163              }
164           }
165           else if( thisContent instanceof Vector JavaDoc )
166           {
167             // now dive one recursion level deeper and sent its elements :
168
vectorToStream( out , (Vector JavaDoc<Object JavaDoc>) thisContent );
169           }
170           else if( thisContent == null )
171           {
172             // caution : this will be processed, if there are
173
// null values in the frontend database. But this
174
// can give troubles, when the same mechanism is
175
// reversed and tries to write back a String, where
176
// in the database there isnt defined anything.
177
// One has to correct the database in this case.
178
out.writeInt( IsStringContent );
179             out.writeUTF( "<null value!>" );
180           }
181          else
182           {
183             System.out.println("**vectorToStream error: Only Integer, Double, Boolean, String, byte[], int[], double[], float[] and Vector types supported.");
184             System.out.println("**The data had value :" + thisContent.toString());
185             System.out.println("**The data had class : " + thisContent.getClass().toString() );
186           }
187         } // for
188
}
189   } // vectorToStream
190

191
192
193  /**
194   * Reads the stream, which originates from a vectorToStream process
195   * and rebuilds theVector. theVector has initially to be a vector with zero length.
196   * Inverse method is vectorToStream.
197   */

198   public static void streamToVector( final DataInputStream in , final List JavaDoc<Object JavaDoc> theVector) throws Exception JavaDoc
199   {
200      firstError = true;
201      streamToVector(in, theVector, true);
202   }
203
204   public static void streamToVectorTest( final DataInputStream in , final List JavaDoc<Object JavaDoc> theVector) throws Exception JavaDoc
205   {
206      firstError = true;
207      streamToVector(in, theVector, true);
208   }
209   
210  /**
211   * Reads the stream, which originates from a vectorToStream process
212   * and rebuilds theVector. theVector has initially to be a vector with zero length.
213   * This method uses recursion.
214   */

215   private static void streamToVector( final DataInputStream in ,
216                                      final List JavaDoc<Object JavaDoc> theVector,
217                                      final boolean recursionStart ) throws Exception JavaDoc
218   {
219       // Do NOT catch exceptions here. They must be propagated back to the callers,
220
// for giving them a chance to react on error situations.
221

222       // At the first run, we have to read the string "vector once",
223
// but not in recursion :
224
if( recursionStart ) // read the keynumber for "vector" only on the first call
225
{
226         int dummy = in.readInt();
227        }
228       // read the size :
229
int numberOfElements = in.readInt();
230       // System.out.println("Vector contains "+numberOfElements+" elements");
231

232       for( int elementIndex = 0; elementIndex < numberOfElements; elementIndex++)
233       {
234         final int thisClassIndex = in.readInt(); // the data type keyword string
235
if( thisClassIndex == IsIntegerContent )
236         {
237           int thisValue = in.readInt();
238           theVector.add( thisValue );
239         }
240         else if( thisClassIndex == IsDoubleContent )
241         {
242           double thisValue = in.readDouble();
243           theVector.add( thisValue );
244         }
245         else if( thisClassIndex == IsLongContent )
246         {
247           long thisValue = in.readLong();
248           theVector.add( thisValue );
249         }
250         else if( thisClassIndex == IsStringContent )
251         {
252           String JavaDoc thisValue = in.readUTF();
253           theVector.add( thisValue );
254         }
255         else if( thisClassIndex == IsBooleanContent )
256         {
257           boolean thisValue = in.readBoolean();
258           theVector.add( thisValue );
259         }
260         else if( thisClassIndex == IsbyteArrayContent )
261         {
262           int arraySize = in.readInt();
263           byte[] thisValue = new byte[arraySize];
264           for( int index = 0; index < arraySize; index++ )
265            {
266             thisValue[index] = in.readByte();
267            }
268           theVector.add( thisValue );
269         }
270         else if( thisClassIndex == IsLongStringContent )
271         {
272           int arraySize = in.readInt();
273           //System.out.println("Reading a "+arraySize+" String in utf-16");
274
byte[] thisValue = new byte[arraySize];
275           for( int index = 0; index < arraySize; index++ )
276           {
277             thisValue[index] = in.readByte();
278           }
279           theVector.add( new String JavaDoc(thisValue, "UTF-16") );
280         }
281         else if( thisClassIndex == IsintArrayContent )
282         {
283           int arraySize = in.readInt();
284           int[] thisValue = new int[arraySize];
285           for( int index = 0; index < arraySize; index++ )
286           {
287             thisValue[index] = in.readInt();
288           }
289           theVector.add( thisValue );
290         }
291         else if( thisClassIndex == IsdoubleArrayContent )
292         {
293           int arraySize = in.readInt();
294           double[] thisValue = new double[arraySize];
295           for( int index = 0; index < arraySize; index++ )
296            {
297              thisValue[index] = in.readDouble();
298            }
299           theVector.add( thisValue );
300         }
301         else if( thisClassIndex == IsfloatArrayContent )
302         {
303           int arraySize = in.readInt();
304           float[] thisValue = new float[arraySize];
305           for( int index = 0; index < arraySize; index++ )
306            {
307              thisValue[index] = in.readFloat();
308            }
309           theVector.add( thisValue );
310         }
311         else if( thisClassIndex == IsStringArrayContent )
312         {
313           int arraySize = in.readInt();
314           String JavaDoc[] thisValue = new String JavaDoc[arraySize];
315           for( int index = 0; index < arraySize; index++ )
316            {
317              thisValue[index] = in.readUTF();
318            }
319           theVector.add( thisValue );
320         }
321         else if( thisClassIndex == IsVectorContent )
322         {
323           // generate a new Vector and add it :
324
Vector JavaDoc<Object JavaDoc> thisSubVector = new Vector JavaDoc<Object JavaDoc>();
325           theVector.add(thisSubVector);
326           // dive one recursion level deeper :
327
streamToVector( in , thisSubVector, false );
328         }
329         else
330         {
331           if(firstError)
332           {
333             System.out.println("streamToVector error: Only Integer, Double Boolean and String types supported.");
334             System.out.println("This class identifier index was : " + thisClassIndex );
335             new Throwable JavaDoc().printStackTrace();
336             firstError = false;
337           }
338         }
339       } // for
340
} // streamToVector
341

342   private static void streamToVectorTest( final DataInputStream in ,
343                                      final boolean recursionStart ) throws Exception JavaDoc
344   {
345       // Do NOT catch exceptions here. They must be propagated back to the callers,
346
// for giving them a chance to react on error situations.
347

348       // At the first run, we have to read the string "vector once",
349
// but not in recursion :
350
if( recursionStart ) // read the keynumber for "vector" only on the first call
351
{
352         int dummy = in.readInt();
353        }
354       // read the size :
355
int numberOfElements = in.readInt();
356       // System.out.println("Vector contains "+numberOfElements+" elements");
357

358       for( int elementIndex = 0; elementIndex < numberOfElements; elementIndex++)
359       {
360         final int thisClassIndex = in.readInt(); // the data type keyword string
361
if( thisClassIndex == IsIntegerContent )
362         {
363           int thisValue = in.readInt();
364         }
365         else if( thisClassIndex == IsDoubleContent )
366         {
367           double thisValue = in.readDouble();
368         }
369         else if( thisClassIndex == IsLongContent )
370         {
371           long thisValue = in.readLong();
372         }
373         else if( thisClassIndex == IsStringContent )
374         {
375           String JavaDoc thisValue = in.readUTF();
376         }
377         else if( thisClassIndex == IsBooleanContent )
378         {
379           boolean thisValue = in.readBoolean();
380         }
381         else if( thisClassIndex == IsbyteArrayContent )
382         {
383           int arraySize = in.readInt();
384           byte[] thisValue = new byte[arraySize];
385           for( int index = 0; index < arraySize; index++ )
386            {
387             thisValue[index] = in.readByte();
388            }
389         }
390         else if( thisClassIndex == IsLongStringContent )
391         {
392           int arraySize = in.readInt();
393           //System.out.println("Reading a "+arraySize+" String in utf-16");
394
byte[] thisValue = new byte[arraySize];
395           for( int index = 0; index < arraySize; index++ )
396           {
397             thisValue[index] = in.readByte();
398           }
399           String JavaDoc s = new String JavaDoc(thisValue, "UTF-16");
400         }
401         else if( thisClassIndex == IsintArrayContent )
402         {
403           int arraySize = in.readInt();
404           int[] thisValue = new int[arraySize];
405           for( int index = 0; index < arraySize; index++ )
406           {
407             thisValue[index] = in.readInt();
408           }
409         }
410         else if( thisClassIndex == IsdoubleArrayContent )
411         {
412           int arraySize = in.readInt();
413           double[] thisValue = new double[arraySize];
414           for( int index = 0; index < arraySize; index++ )
415            {
416              thisValue[index] = in.readDouble();
417            }
418         }
419         else if( thisClassIndex == IsfloatArrayContent )
420         {
421           int arraySize = in.readInt();
422           float[] thisValue = new float[arraySize];
423           for( int index = 0; index < arraySize; index++ )
424            {
425              thisValue[index] = in.readFloat();
426            }
427         }
428         else if( thisClassIndex == IsStringArrayContent )
429         {
430           int arraySize = in.readInt();
431           String JavaDoc[] thisValue = new String JavaDoc[arraySize];
432           for( int index = 0; index < arraySize; index++ )
433            {
434              thisValue[index] = in.readUTF();
435            }
436         }
437         else if( thisClassIndex == IsVectorContent )
438         {
439           // dive one recursion level deeper :
440
streamToVectorTest( in , false );
441         }
442         else
443         {
444             throw new Exception JavaDoc("unknown class type "+thisClassIndex );
445         }
446       } // for
447
} // streamToVectorTest
448

449   static boolean firstError = true;
450
451   /** an utf string has a 65535 bytes limited length.
452     chars < 127 take one byte, greater chars take 2 or 3 bytes
453     this routine returns the exact bytes that the string will take
454     copied from dataoutputstream
455   */

456   public static final int calculateUTFLength(String JavaDoc str)
457   {
458     int strlen = str.length();
459     int utflen = 0;
460     char[] charr = new char[strlen];
461     int c, count = 0;
462
463     str.getChars(0, strlen, charr, 0);
464
465     for (int i = 0; i < strlen; i++)
466     {
467        c = charr[i];
468        if ((c >= 0x0001) && (c <= 0x007F))
469        {
470           utflen++;
471          }
472        else if (c > 0x07FF)
473        {
474           utflen += 3;
475        }
476        else
477        {
478           utflen += 2;
479        }
480     }
481     return utflen;
482   }
483
484
485
486
487
488
489  /** FROM SCHMORTOPF
490   * Reads a Vector, which was written using SendVector() from
491   * the inputstream.
492   */

493   public static List JavaDoc<Object JavaDoc> receiveVector( final DataInputStream in ) throws Exception JavaDoc
494   {
495     int vectorSize = 0;
496     // First read its size :
497
vectorSize = in.readInt();
498     // Then use the recursive worker method for the rest :
499
final Vector JavaDoc<Object JavaDoc> theVector = new Vector JavaDoc<Object JavaDoc>();
500     receiveVectorCoded( in,theVector,vectorSize ); // works on theVector
501
return theVector;
502   }
503
504
505
506
507
508
509  /** FROM SCHMORTOPF
510   * Recursive worker method for ReceiveVector()
511   */

512   private static void receiveVectorCoded( final DataInputStream in ,
513                                           final List JavaDoc<Object JavaDoc> theVector,
514                                           final int numberOfElements ) throws Exception JavaDoc
515   {
516      for( int elementIndex = 0;
517           elementIndex < numberOfElements;
518           elementIndex++)
519       {
520        final int thisClassIndex = in.readInt(); // the data type keyword string
521
if( thisClassIndex == IsStringContent )
522         {
523          String JavaDoc thisValue = in.readUTF();
524          theVector.add( thisValue );
525         }
526        else if( thisClassIndex == IsIntegerContent )
527         {
528          int thisValue = in.readInt();
529          theVector.add( thisValue );
530         }
531        else if( thisClassIndex == IsLongContent )
532         {
533          long thisValue = in.readLong();
534          theVector.add( thisValue );
535         }
536        else if( thisClassIndex == IsDoubleContent )
537         {
538          double thisValue = in.readDouble();
539          theVector.add( thisValue );
540         }
541        else if( thisClassIndex == IsBooleanContent )
542         {
543           boolean thisValue = in.readBoolean();
544           theVector.add( thisValue );
545         }
546        else if( thisClassIndex == IsbyteArrayContent )
547         {
548           int arraySize = in.readInt();
549           byte[] thisValue = new byte[arraySize];
550           for( int index = 0; index < arraySize; index++ )
551            {
552             thisValue[index] = in.readByte();
553            }
554           theVector.add( thisValue );
555         }
556        else if( thisClassIndex == IsintArrayContent )
557         {
558           int arraySize = in.readInt();
559           int[] thisValue = new int[arraySize];
560           for( int index = 0; index < arraySize; index++ )
561            {
562             thisValue[index] = in.readInt();
563            }
564           theVector.add( thisValue );
565         }
566        else if( thisClassIndex == IsdoubleArrayContent )
567         {
568           int arraySize = in.readInt();
569           double[] thisValue = new double[arraySize];
570           for( int index = 0; index < arraySize; index++ )
571            {
572             thisValue[index] = in.readDouble();
573            }
574           theVector.add( thisValue );
575         }
576        else if( thisClassIndex == IsfloatArrayContent )
577         {
578           int arraySize = in.readInt();
579           float[] thisValue = new float[arraySize];
580           for( int index = 0; index < arraySize; index++ )
581            {
582             thisValue[index] = in.readFloat();
583            }
584           theVector.add( thisValue );
585         }
586        else if( thisClassIndex == IsStringArrayContent )
587         {
588           int arraySize = in.readInt();
589           String JavaDoc[] thisValue = new String JavaDoc[arraySize];
590           for( int index = 0; index < arraySize; index++ )
591            {
592             thisValue[index] = in.readUTF();
593            }
594           theVector.add( thisValue );
595         }
596        else if( thisClassIndex == IsVectorContent )
597         {
598          int thisVectorSize = in.readInt();
599          // generate a new Vector and add it :
600
Vector JavaDoc<Object JavaDoc> thisSubVector = new Vector JavaDoc<Object JavaDoc>();
601          theVector.add(thisSubVector);
602          // dive one recursion level deeper :
603
receiveVectorCoded( in ,
604                              thisSubVector,
605                              thisVectorSize );
606         }
607        else
608         {
609          System.out.println("*** FileUtilities.ReceiveVectorCoded error: Only Integer, Double and String types supported.");
610          System.out.println("*** The data had classindex = " + thisClassIndex );
611          throw new Exception JavaDoc("FileUtilities.ReceiveVectorCoded error");
612         }
613       } // for
614
} // ReceiveVectorCoded
615

616 }
Popular Tags