1 18 package org.apache.activemq.openwire.v1; 19 20 import java.io.DataInput ; 21 import java.io.DataOutput ; 22 import java.io.IOException ; 23 import java.lang.reflect.Constructor ; 24 25 import org.apache.activemq.command.DataStructure; 26 import org.apache.activemq.openwire.BooleanStream; 27 import org.apache.activemq.openwire.DataStreamMarshaller; 28 import org.apache.activemq.openwire.OpenWireFormat; 29 import org.apache.activemq.util.ByteSequence; 30 import org.apache.activemq.util.ClassLoading; 31 32 abstract public class BaseDataStreamMarshaller implements DataStreamMarshaller { 33 34 static final public Constructor STACK_TRACE_ELEMENT_CONSTRUCTOR; 35 36 static { 37 Constructor constructor=null; 38 try { 39 constructor = StackTraceElement .class.getConstructor(new Class []{String .class, String .class, String .class, int.class}); 40 } catch (Throwable e) { 41 } 42 STACK_TRACE_ELEMENT_CONSTRUCTOR = constructor; 43 } 44 45 46 abstract public byte getDataStructureType(); 47 abstract public DataStructure createObject(); 48 49 public int tightMarshal1(OpenWireFormat wireFormat, Object o, BooleanStream bs) throws IOException { 50 return 0; 51 } 52 public void tightMarshal2(OpenWireFormat wireFormat, Object o, DataOutput dataOut, BooleanStream bs) throws IOException { 53 } 54 public void tightUnmarshal(OpenWireFormat wireFormat, Object o, DataInput dataIn, BooleanStream bs) throws IOException { 55 } 56 57 public int tightMarshalLong1(OpenWireFormat wireFormat, long o, BooleanStream bs) throws IOException { 58 if( o == 0 ) { 59 bs.writeBoolean(false); 60 bs.writeBoolean(false); 61 return 0; 62 } else if ( (o & 0xFFFFFFFFFFFF0000l ) == 0 ) { 63 bs.writeBoolean(false); 64 bs.writeBoolean(true); 65 return 2; 66 } else if ( (o & 0xFFFFFFFF00000000l ) == 0) { 67 bs.writeBoolean(true); 68 bs.writeBoolean(false); 69 return 4; 70 } else { 71 bs.writeBoolean(true); 72 bs.writeBoolean(true); 73 return 8; 74 } 75 } 76 public void tightMarshalLong2(OpenWireFormat wireFormat, long o, DataOutput dataOut, BooleanStream bs) throws IOException { 77 if( bs.readBoolean() ) { 78 if( bs.readBoolean() ) { 79 dataOut.writeLong(o); 80 } else { 81 dataOut.writeInt((int) o); 82 } 83 } else { 84 if( bs.readBoolean() ) { 85 dataOut.writeShort((int) o); 86 } 87 } 88 } 89 public long tightUnmarshalLong(OpenWireFormat wireFormat, DataInput dataIn, BooleanStream bs) throws IOException { 90 if( bs.readBoolean() ) { 91 if( bs.readBoolean() ) { 92 return dataIn.readLong(); 93 } else { 94 return toLong(dataIn.readInt()); 95 } 96 } else { 97 if( bs.readBoolean() ) { 98 return toLong(dataIn.readShort()); 99 } else { 100 return 0; 101 } 102 } 103 } 104 105 protected long toLong(short value) { 106 long answer = value; 108 return answer & 0xffffL; 109 } 110 111 protected long toLong(int value) { 112 long answer = value; 114 return answer & 0xffffffffL; 115 } 116 117 protected DataStructure tightUnmarsalNestedObject(OpenWireFormat wireFormat, DataInput dataIn, BooleanStream bs) throws IOException { 118 return wireFormat.tightUnmarshalNestedObject(dataIn, bs); 119 } 120 protected int tightMarshalNestedObject1(OpenWireFormat wireFormat, DataStructure o, BooleanStream bs) throws IOException { 121 return wireFormat.tightMarshalNestedObject1(o, bs); 122 } 123 124 protected void tightMarshalNestedObject2(OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut, BooleanStream bs) throws IOException { 125 wireFormat.tightMarshalNestedObject2(o, dataOut, bs); 126 } 127 128 protected DataStructure tightUnmarsalCachedObject(OpenWireFormat wireFormat, DataInput dataIn, BooleanStream bs) throws IOException { 129 if( wireFormat.isCacheEnabled() ) { 130 if( bs.readBoolean() ) { 131 short index = dataIn.readShort(); 132 DataStructure object = wireFormat.tightUnmarshalNestedObject(dataIn, bs); 133 wireFormat.setInUnmarshallCache(index, object); 134 return object; 135 } else { 136 short index = dataIn.readShort(); 137 return wireFormat.getFromUnmarshallCache(index); 138 } 139 } else { 140 return wireFormat.tightUnmarshalNestedObject(dataIn, bs); 141 } 142 } 143 protected int tightMarshalCachedObject1(OpenWireFormat wireFormat, DataStructure o, BooleanStream bs) throws IOException { 144 if( wireFormat.isCacheEnabled() ) { 145 Short index = wireFormat.getMarshallCacheIndex(o); 146 bs.writeBoolean(index == null); 147 if( index == null ) { 148 int rc = wireFormat.tightMarshalNestedObject1(o, bs); 149 wireFormat.addToMarshallCache(o); 150 return 2+rc; 151 } else { 152 return 2; 153 } 154 } else { 155 return wireFormat.tightMarshalNestedObject1(o, bs); 156 } 157 } 158 protected void tightMarshalCachedObject2(OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut, BooleanStream bs) throws IOException { 159 if( wireFormat.isCacheEnabled() ) { 160 Short index = wireFormat.getMarshallCacheIndex(o); 161 if( bs.readBoolean() ) { 162 dataOut.writeShort(index.shortValue()); 163 wireFormat.tightMarshalNestedObject2(o, dataOut, bs); 164 } else { 165 dataOut.writeShort(index.shortValue()); 166 } 167 } else { 168 wireFormat.tightMarshalNestedObject2(o, dataOut, bs); 169 } 170 } 171 172 protected Throwable tightUnmarsalThrowable(OpenWireFormat wireFormat, DataInput dataIn, BooleanStream bs) throws IOException { 173 if( bs.readBoolean() ) { 174 String clazz = tightUnmarshalString(dataIn, bs); 175 String message = tightUnmarshalString(dataIn, bs); 176 Throwable o = createThrowable(clazz, message); 177 if( wireFormat.isStackTraceEnabled() ) { 178 if( STACK_TRACE_ELEMENT_CONSTRUCTOR!=null) { 179 StackTraceElement ss[] = new StackTraceElement [dataIn.readShort()]; 180 for (int i = 0; i < ss.length; i++) { 181 try { 182 ss[i] = (StackTraceElement ) STACK_TRACE_ELEMENT_CONSTRUCTOR.newInstance(new Object []{ 183 tightUnmarshalString(dataIn, bs), 184 tightUnmarshalString(dataIn, bs), 185 tightUnmarshalString(dataIn, bs), 186 new Integer (dataIn.readInt()) 187 }); 188 } catch (IOException e) { 189 throw e; 190 } catch (Throwable e) { 191 } 192 } 193 o.setStackTrace(ss); 194 } else { 195 short size = dataIn.readShort(); 196 for (int i = 0; i < size; i++) { 197 tightUnmarshalString(dataIn, bs); 198 tightUnmarshalString(dataIn, bs); 199 tightUnmarshalString(dataIn, bs); 200 dataIn.readInt(); 201 } 202 } 203 o.initCause(tightUnmarsalThrowable(wireFormat, dataIn, bs)); 204 205 } 206 return o; 207 } else { 208 return null; 209 } 210 } 211 212 private Throwable createThrowable(String className, String message) { 213 try { 214 Class clazz = ClassLoading.loadClass(className, BaseDataStreamMarshaller.class.getClassLoader()); 215 Constructor constructor = clazz.getConstructor(new Class []{String .class}); 216 return (Throwable ) constructor.newInstance(new Object []{message}); 217 } catch (Throwable e) { 218 return new Throwable (className+": "+message); 219 } 220 } 221 222 protected int tightMarshalThrowable1(OpenWireFormat wireFormat, Throwable o, BooleanStream bs) throws IOException { 223 if( o==null ) { 224 bs.writeBoolean(false); 225 return 0; 226 } else { 227 int rc=0; 228 bs.writeBoolean(true); 229 rc += tightMarshalString1(o.getClass().getName(), bs); 230 rc += tightMarshalString1(o.getMessage(), bs); 231 if( wireFormat.isStackTraceEnabled() ) { 232 rc += 2; 233 StackTraceElement [] stackTrace = o.getStackTrace(); 234 for (int i = 0; i < stackTrace.length; i++) { 235 StackTraceElement element = stackTrace[i]; 236 rc += tightMarshalString1(element.getClassName(), bs); 237 rc += tightMarshalString1(element.getMethodName(), bs); 238 rc += tightMarshalString1(element.getFileName(), bs); 239 rc += 4; 240 } 241 rc += tightMarshalThrowable1(wireFormat, o.getCause(), bs); 242 } 243 return rc; 244 } 245 } 246 247 protected void tightMarshalThrowable2(OpenWireFormat wireFormat, Throwable o, DataOutput dataOut, BooleanStream bs) throws IOException { 248 if( bs.readBoolean() ) { 249 tightMarshalString2(o.getClass().getName(), dataOut, bs); 250 tightMarshalString2(o.getMessage(), dataOut, bs); 251 if( wireFormat.isStackTraceEnabled() ) { 252 StackTraceElement [] stackTrace = o.getStackTrace(); 253 dataOut.writeShort(stackTrace.length); 254 for (int i = 0; i < stackTrace.length; i++) { 255 StackTraceElement element = stackTrace[i]; 256 tightMarshalString2(element.getClassName(), dataOut, bs); 257 tightMarshalString2(element.getMethodName(), dataOut, bs); 258 tightMarshalString2(element.getFileName(), dataOut, bs); 259 dataOut.writeInt(element.getLineNumber()); 260 } 261 tightMarshalThrowable2(wireFormat, o.getCause(), dataOut, bs); 262 } 263 } 264 } 265 266 protected String tightUnmarshalString(DataInput dataIn, BooleanStream bs) throws IOException { 267 if( bs.readBoolean() ) { 268 if( bs.readBoolean() ) { 269 int size = dataIn.readShort(); 270 byte data[] = new byte[size]; 271 dataIn.readFully(data); 272 return new String (data,0); } else { 274 return dataIn.readUTF(); 275 } 276 } else { 277 return null; 278 } 279 } 280 281 protected int tightMarshalString1(String value, BooleanStream bs) throws IOException { 282 bs.writeBoolean(value!=null); 283 if( value!=null ) { 284 285 int strlen = value.length(); 286 int utflen = 0; 287 char[] charr = new char[strlen]; 288 int c, count = 0; 289 boolean isOnlyAscii=true; 290 291 value.getChars(0, strlen, charr, 0); 292 293 for (int i = 0; i < strlen; i++) { 294 c = charr[i]; 295 if ((c >= 0x0001) && (c <= 0x007F)) { 296 utflen++; 297 } else if (c > 0x07FF) { 298 utflen += 3; 299 isOnlyAscii=false; 300 } else { 301 isOnlyAscii=false; 302 utflen += 2; 303 } 304 } 305 306 if( utflen >= Short.MAX_VALUE ) 307 throw new IOException ("Encountered a String value that is too long to encode."); 308 309 bs.writeBoolean(isOnlyAscii); 310 return utflen+2; 311 312 } else { 313 return 0; 314 } 315 } 316 317 protected void tightMarshalString2(String value, DataOutput dataOut, BooleanStream bs) throws IOException { 318 if( bs.readBoolean() ) { 319 if( bs.readBoolean() ) { 321 dataOut.writeShort(value.length()); 322 dataOut.writeBytes(value); 323 } else { 324 dataOut.writeUTF(value); 325 } 326 } 327 } 328 329 protected int tightMarshalObjectArray1(OpenWireFormat wireFormat, DataStructure[] objects, BooleanStream bs) throws IOException { 330 if( objects != null ) { 331 int rc=0; 332 bs.writeBoolean(true); 333 rc += 2; 334 for( int i=0; i < objects.length; i++ ) { 335 rc += tightMarshalNestedObject1(wireFormat,objects[i], bs); 336 } 337 return rc; 338 } else { 339 bs.writeBoolean(false); 340 return 0; 341 } 342 } 343 344 protected void tightMarshalObjectArray2(OpenWireFormat wireFormat, DataStructure[] objects, DataOutput dataOut, BooleanStream bs) throws IOException { 345 if( bs.readBoolean() ) { 346 dataOut.writeShort(objects.length); 347 for( int i=0; i < objects.length; i++ ) { 348 tightMarshalNestedObject2(wireFormat,objects[i], dataOut, bs); 349 } 350 } 351 } 352 353 protected int tightMarshalConstByteArray1(byte[] data, BooleanStream bs, int i) throws IOException { 354 return i; 355 } 356 protected void tightMarshalConstByteArray2(byte[] data, DataOutput dataOut, BooleanStream bs, int i) throws IOException { 357 dataOut.write(data, 0, i); 358 } 359 360 protected byte[] tightUnmarshalConstByteArray(DataInput dataIn, BooleanStream bs, int i) throws IOException { 361 byte data[] = new byte[i]; 362 dataIn.readFully(data); 363 return data; 364 } 365 366 protected int tightMarshalByteArray1(byte[] data, BooleanStream bs) throws IOException { 367 bs.writeBoolean(data!=null); 368 if( data!=null ){ 369 return data.length+4; 370 } else { 371 return 0; 372 } 373 } 374 375 protected void tightMarshalByteArray2(byte[] data, DataOutput dataOut, BooleanStream bs) throws IOException { 376 if( bs.readBoolean() ){ 377 dataOut.writeInt(data.length); 378 dataOut.write(data); 379 } 380 } 381 382 protected byte[] tightUnmarshalByteArray(DataInput dataIn, BooleanStream bs) throws IOException { 383 byte rc[]=null; 384 if( bs.readBoolean() ) { 385 int size = dataIn.readInt(); 386 rc = new byte[size]; 387 dataIn.readFully(rc); 388 } 389 return rc; 390 } 391 392 protected int tightMarshalByteSequence1(ByteSequence data, BooleanStream bs) throws IOException { 393 bs.writeBoolean(data!=null); 394 if( data!=null ){ 395 return data.getLength()+4; 396 } else { 397 return 0; 398 } 399 } 400 401 protected void tightMarshalByteSequence2(ByteSequence data, DataOutput dataOut, BooleanStream bs) throws IOException { 402 if( bs.readBoolean() ){ 403 dataOut.writeInt(data.getLength()); 404 dataOut.write(data.getData(), data.getOffset(), data.getLength()); 405 } 406 } 407 408 protected ByteSequence tightUnmarshalByteSequence(DataInput dataIn, BooleanStream bs) throws IOException { 409 ByteSequence rc=null; 410 if( bs.readBoolean() ) { 411 int size = dataIn.readInt(); 412 byte[] t = new byte[size]; 413 dataIn.readFully(t); 414 return new ByteSequence(t, 0, size); 415 } 416 return rc; 417 } 418 419 423 public void looseMarshal(OpenWireFormat wireFormat, Object o, DataOutput dataOut) throws IOException { 424 } 425 public void looseUnmarshal(OpenWireFormat wireFormat, Object o, DataInput dataIn) throws IOException { 426 } 427 428 public void looseMarshalLong(OpenWireFormat wireFormat, long o, DataOutput dataOut) throws IOException { 429 dataOut.writeLong(o); 430 } 431 public long looseUnmarshalLong(OpenWireFormat wireFormat, DataInput dataIn) throws IOException { 432 return dataIn.readLong(); 433 } 434 435 protected DataStructure looseUnmarsalNestedObject(OpenWireFormat wireFormat, DataInput dataIn) throws IOException { 436 return wireFormat.looseUnmarshalNestedObject(dataIn); 437 } 438 protected void looseMarshalNestedObject(OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut) throws IOException { 439 wireFormat.looseMarshalNestedObject(o, dataOut); 440 } 441 442 protected DataStructure looseUnmarsalCachedObject(OpenWireFormat wireFormat, DataInput dataIn) throws IOException { 443 if( wireFormat.isCacheEnabled() ) { 444 if( dataIn.readBoolean() ) { 445 short index = dataIn.readShort(); 446 DataStructure object = wireFormat.looseUnmarshalNestedObject(dataIn); 447 wireFormat.setInUnmarshallCache(index, object); 448 return object; 449 } else { 450 short index = dataIn.readShort(); 451 return wireFormat.getFromUnmarshallCache(index); 452 } 453 } else { 454 return wireFormat.looseUnmarshalNestedObject(dataIn); 455 } 456 } 457 protected void looseMarshalCachedObject(OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut) throws IOException { 458 if( wireFormat.isCacheEnabled() ) { 459 Short index = wireFormat.getMarshallCacheIndex(o); 460 dataOut.writeBoolean(index == null); 461 if( index == null ) { 462 index = wireFormat.addToMarshallCache(o); 463 dataOut.writeShort(index.shortValue()); 464 wireFormat.looseMarshalNestedObject(o, dataOut); 465 } else { 466 dataOut.writeShort(index.shortValue()); 467 } 468 } else { 469 wireFormat.looseMarshalNestedObject(o, dataOut); 470 } 471 } 472 473 protected Throwable looseUnmarsalThrowable(OpenWireFormat wireFormat, DataInput dataIn) throws IOException { 474 if( dataIn.readBoolean() ) { 475 String clazz = looseUnmarshalString(dataIn); 476 String message = looseUnmarshalString(dataIn); 477 Throwable o = createThrowable(clazz, message); 478 if( wireFormat.isStackTraceEnabled() ) { 479 if( STACK_TRACE_ELEMENT_CONSTRUCTOR!=null) { 480 StackTraceElement ss[] = new StackTraceElement [dataIn.readShort()]; 481 for (int i = 0; i < ss.length; i++) { 482 try { 483 ss[i] = (StackTraceElement ) STACK_TRACE_ELEMENT_CONSTRUCTOR.newInstance(new Object []{ 484 looseUnmarshalString(dataIn), 485 looseUnmarshalString(dataIn), 486 looseUnmarshalString(dataIn), 487 new Integer (dataIn.readInt()) 488 }); 489 } catch (IOException e) { 490 throw e; 491 } catch (Throwable e) { 492 } 493 } 494 o.setStackTrace(ss); 495 } else { 496 short size = dataIn.readShort(); 497 for (int i = 0; i < size; i++) { 498 looseUnmarshalString(dataIn); 499 looseUnmarshalString(dataIn); 500 looseUnmarshalString(dataIn); 501 dataIn.readInt(); 502 } 503 } 504 o.initCause(looseUnmarsalThrowable(wireFormat, dataIn)); 505 506 } 507 return o; 508 } else { 509 return null; 510 } 511 } 512 513 514 protected void looseMarshalThrowable(OpenWireFormat wireFormat, Throwable o, DataOutput dataOut) throws IOException { 515 dataOut.writeBoolean(o!=null); 516 if( o!=null ) { 517 looseMarshalString(o.getClass().getName(), dataOut); 518 looseMarshalString(o.getMessage(), dataOut); 519 if( wireFormat.isStackTraceEnabled() ) { 520 StackTraceElement [] stackTrace = o.getStackTrace(); 521 dataOut.writeShort(stackTrace.length); 522 for (int i = 0; i < stackTrace.length; i++) { 523 StackTraceElement element = stackTrace[i]; 524 looseMarshalString(element.getClassName(), dataOut); 525 looseMarshalString(element.getMethodName(), dataOut); 526 looseMarshalString(element.getFileName(), dataOut); 527 dataOut.writeInt(element.getLineNumber()); 528 } 529 looseMarshalThrowable(wireFormat, o.getCause(), dataOut); 530 } 531 } 532 } 533 534 protected String looseUnmarshalString(DataInput dataIn) throws IOException { 535 if( dataIn.readBoolean() ) { 536 return dataIn.readUTF(); 537 } else { 538 return null; 539 } 540 } 541 542 protected void looseMarshalString(String value, DataOutput dataOut) throws IOException { 543 dataOut.writeBoolean(value!=null); 544 if( value!=null ) { 545 dataOut.writeUTF(value); 546 } 547 } 548 549 protected void looseMarshalObjectArray(OpenWireFormat wireFormat, DataStructure[] objects, DataOutput dataOut) throws IOException { 550 dataOut.writeBoolean(objects!=null); 551 if( objects!=null ) { 552 dataOut.writeShort(objects.length); 553 for( int i=0; i < objects.length; i++ ) { 554 looseMarshalNestedObject(wireFormat,objects[i], dataOut); 555 } 556 } 557 } 558 559 protected void looseMarshalConstByteArray(OpenWireFormat wireFormat, byte[] data, DataOutput dataOut, int i) throws IOException { 560 dataOut.write(data, 0, i); 561 } 562 563 protected byte[] looseUnmarshalConstByteArray(DataInput dataIn, int i) throws IOException { 564 byte data[] = new byte[i]; 565 dataIn.readFully(data); 566 return data; 567 } 568 569 protected void looseMarshalByteArray(OpenWireFormat wireFormat, byte[] data, DataOutput dataOut) throws IOException { 570 dataOut.writeBoolean(data!=null); 571 if( data!=null ){ 572 dataOut.writeInt(data.length); 573 dataOut.write(data); 574 } 575 } 576 577 protected byte[] looseUnmarshalByteArray(DataInput dataIn) throws IOException { 578 byte rc[]=null; 579 if( dataIn.readBoolean() ) { 580 int size = dataIn.readInt(); 581 rc = new byte[size]; 582 dataIn.readFully(rc); 583 } 584 return rc; 585 } 586 587 protected void looseMarshalByteSequence(OpenWireFormat wireFormat, ByteSequence data, DataOutput dataOut) throws IOException { 588 dataOut.writeBoolean(data!=null); 589 if( data!=null ){ 590 dataOut.writeInt(data.getLength()); 591 dataOut.write(data.getData(), data.getOffset(), data.getLength()); 592 } 593 } 594 595 protected ByteSequence looseUnmarshalByteSequence(DataInput dataIn) throws IOException { 596 ByteSequence rc=null; 597 if( dataIn.readBoolean() ) { 598 int size = dataIn.readInt(); 599 byte[] t = new byte[size]; 600 dataIn.readFully(t); 601 rc = new ByteSequence(t, 0, size); 602 } 603 return rc; 604 } 605 } 606 | Popular Tags |