1 package snow.utils.storage; 2 3 import java.io.*; 4 import java.util.Vector ; 5 import java.util.List ; 6 7 public final class VectorUtils { 8 9 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 32 @SuppressWarnings ("unchecked") 33 public static final void vectorToStream( final DataOutputStream out, final List <Object > theVector ) throws Exception 34 { 35 vectorToStreamRecurse(out, theVector); 36 } 37 38 39 40 41 42 43 47 @SuppressWarnings ("unchecked") 48 private static final void vectorToStreamRecurse( final DataOutputStream out , 49 final List <Object > theVector ) throws Exception 50 { 51 if (theVector == null) 52 { 53 System.out.println("VectorUtils vectorToStream: null vector received!"); 54 out.writeInt( IsVectorContent ); 55 Vector <Object > thisVector = new Vector <Object >(); 57 int thisSize = thisVector.size(); 58 out.writeInt(thisSize); 59 } 60 else 61 { 62 out.writeInt( IsVectorContent ); 63 int thisSize = theVector.size(); 65 out.writeInt(thisSize); 66 67 for( int i=0; i < thisSize; i++) 68 { 69 Object thisContent = theVector.get(i); 70 if( thisContent instanceof Integer ) 71 { 72 out.writeInt( IsIntegerContent ); 73 out.writeInt( (Integer ) thisContent ); 74 } 75 else if( thisContent instanceof Double ) 76 { 77 out.writeInt( IsDoubleContent ); 78 out.writeDouble( (Double ) thisContent ); 79 } 80 else if( thisContent instanceof Long ) 81 { 82 out.writeInt( IsLongContent ); 83 out.writeLong( (Long ) thisContent ); 84 } 85 else if( thisContent instanceof String ) 86 { 87 String str = (String ) thisContent; 90 int len = calculateUTFLength(str); 91 if(len<65535) 93 { 94 out.writeInt( IsStringContent ); 96 out.writeUTF( str ); 97 } 98 else 99 { 100 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 ) 111 { 112 out.writeInt( IsBooleanContent ); 113 out.writeBoolean( (Boolean ) 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 [] ) 156 { 157 String [] theArray = (String [])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 ) 166 { 167 vectorToStream( out , (Vector <Object >) thisContent ); 169 } 170 else if( thisContent == null ) 171 { 172 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 } } 189 } 191 192 193 198 public static void streamToVector( final DataInputStream in , final List <Object > theVector) throws Exception 199 { 200 firstError = true; 201 streamToVector(in, theVector, true); 202 } 203 204 public static void streamToVectorTest( final DataInputStream in , final List <Object > theVector) throws Exception 205 { 206 firstError = true; 207 streamToVector(in, theVector, true); 208 } 209 210 215 private static void streamToVector( final DataInputStream in , 216 final List <Object > theVector, 217 final boolean recursionStart ) throws Exception 218 { 219 222 if( recursionStart ) { 226 int dummy = in.readInt(); 227 } 228 int numberOfElements = in.readInt(); 230 232 for( int elementIndex = 0; elementIndex < numberOfElements; elementIndex++) 233 { 234 final int thisClassIndex = in.readInt(); 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 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 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 (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 [] thisValue = new String [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 Vector <Object > thisSubVector = new Vector <Object >(); 325 theVector.add(thisSubVector); 326 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 ().printStackTrace(); 336 firstError = false; 337 } 338 } 339 } } 342 private static void streamToVectorTest( final DataInputStream in , 343 final boolean recursionStart ) throws Exception 344 { 345 348 if( recursionStart ) { 352 int dummy = in.readInt(); 353 } 354 int numberOfElements = in.readInt(); 356 358 for( int elementIndex = 0; elementIndex < numberOfElements; elementIndex++) 359 { 360 final int thisClassIndex = in.readInt(); 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 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 byte[] thisValue = new byte[arraySize]; 395 for( int index = 0; index < arraySize; index++ ) 396 { 397 thisValue[index] = in.readByte(); 398 } 399 String s = new String (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 [] thisValue = new String [arraySize]; 432 for( int index = 0; index < arraySize; index++ ) 433 { 434 thisValue[index] = in.readUTF(); 435 } 436 } 437 else if( thisClassIndex == IsVectorContent ) 438 { 439 streamToVectorTest( in , false ); 441 } 442 else 443 { 444 throw new Exception ("unknown class type "+thisClassIndex ); 445 } 446 } } 449 static boolean firstError = true; 450 451 456 public static final int calculateUTFLength(String 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 493 public static List <Object > receiveVector( final DataInputStream in ) throws Exception 494 { 495 int vectorSize = 0; 496 vectorSize = in.readInt(); 498 final Vector <Object > theVector = new Vector <Object >(); 500 receiveVectorCoded( in,theVector,vectorSize ); return theVector; 502 } 503 504 505 506 507 508 509 512 private static void receiveVectorCoded( final DataInputStream in , 513 final List <Object > theVector, 514 final int numberOfElements ) throws Exception 515 { 516 for( int elementIndex = 0; 517 elementIndex < numberOfElements; 518 elementIndex++) 519 { 520 final int thisClassIndex = in.readInt(); if( thisClassIndex == IsStringContent ) 522 { 523 String 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 [] thisValue = new String [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 Vector <Object > thisSubVector = new Vector <Object >(); 601 theVector.add(thisSubVector); 602 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 ("FileUtilities.ReceiveVectorCoded error"); 612 } 613 } } 616 } | Popular Tags |