1 7 package com.sun.corba.se.impl.encoding; 8 9 import java.io.IOException ; 10 import java.io.ObjectOutputStream ; 11 import java.io.ByteArrayOutputStream ; 12 13 import java.nio.ByteBuffer ; 14 15 import com.sun.corba.se.spi.orb.ORB; 16 import com.sun.corba.se.spi.ior.IOR; 17 import com.sun.corba.se.spi.ior.IORFactories; 18 import com.sun.corba.se.spi.ior.iiop.GIOPVersion; 19 import com.sun.corba.se.spi.logging.CORBALogDomains; 20 import com.sun.corba.se.spi.presentation.rmi.StubAdapter; 21 22 import com.sun.corba.se.impl.util.Utility; 23 import com.sun.corba.se.impl.orbutil.ORBConstants; 24 import com.sun.corba.se.impl.orbutil.ORBUtility; 25 import com.sun.corba.se.impl.corba.TypeCodeImpl; 26 import com.sun.corba.se.impl.logging.ORBUtilSystemException; 27 import com.sun.corba.se.impl.protocol.giopmsgheaders.Message; 28 29 import org.omg.CORBA.Any ; 30 import org.omg.CORBA.TypeCode ; 31 import org.omg.CORBA.Principal ; 32 import org.omg.CORBA.CompletionStatus ; 33 34 51 public class IDLJavaSerializationOutputStream extends CDROutputStreamBase { 52 53 private ORB orb; 54 private byte encodingVersion; 55 private ObjectOutputStream os; 56 private _ByteArrayOutputStream bos; 57 private BufferManagerWrite bufferManager; 58 59 private final int directWriteLength = Message.GIOPMessageHeaderLength + 4; 61 62 protected ORBUtilSystemException wrapper; 63 64 class _ByteArrayOutputStream extends ByteArrayOutputStream { 65 66 _ByteArrayOutputStream(int initialSize) { 67 super(initialSize); 68 } 69 70 byte[] getByteArray() { 71 return this.buf; 72 } 73 } 74 75 class MarshalObjectOutputStream extends ObjectOutputStream { 76 77 ORB orb; 78 79 MarshalObjectOutputStream(java.io.OutputStream out, ORB orb) 80 throws IOException { 81 82 super(out); 83 this.orb = orb; 84 java.security.AccessController.doPrivileged( 85 new java.security.PrivilegedAction () { 86 public Object run() { 87 enableReplaceObject(true); 89 return null; 90 } 91 } 92 ); 93 } 94 95 99 protected final Object replaceObject(Object obj) throws IOException { 100 try { 101 if ((obj instanceof java.rmi.Remote ) && 102 !(StubAdapter.isStub(obj))) { 103 return Utility.autoConnect(obj, orb, true); 104 } 105 } catch (Exception e) { 106 IOException ie = new IOException ("replaceObject failed"); 107 ie.initCause(e); 108 throw ie; 109 } 110 return obj; 111 } 112 } 113 114 public IDLJavaSerializationOutputStream(byte encodingVersion) { 115 super(); 116 this.encodingVersion = encodingVersion; 117 } 118 119 public void init(org.omg.CORBA.ORB orb, boolean littleEndian, 120 BufferManagerWrite bufferManager, 121 byte streamFormatVersion, 122 boolean usePooledByteBuffers) { 123 this.orb = (ORB) orb; 124 this.bufferManager = bufferManager; 125 wrapper = ORBUtilSystemException.get((ORB) orb, 126 CORBALogDomains.RPC_ENCODING); 127 bos = 128 new _ByteArrayOutputStream(ORBConstants.GIOP_DEFAULT_BUFFER_SIZE); 129 } 130 131 private void initObjectOutputStream() { 133 if (os != null) { 135 throw wrapper.javaStreamInitFailed(); 136 } 137 try { 138 os = new MarshalObjectOutputStream(bos, orb); 139 } catch (Exception e) { 140 throw wrapper.javaStreamInitFailed(e); 141 } 142 } 143 144 146 148 public final void write_boolean(boolean value) { 149 try { 150 os.writeBoolean(value); 151 } catch (Exception e) { 152 throw wrapper.javaSerializationException(e, "write_boolean"); 153 } 154 } 155 156 public final void write_char(char value) { 157 try { 158 os.writeChar(value); 159 } catch (Exception e) { 160 throw wrapper.javaSerializationException(e, "write_char"); 161 } 162 } 163 164 public final void write_wchar(char value) { 165 this.write_char(value); 166 } 167 168 public final void write_octet(byte value) { 169 170 if (bos.size() < directWriteLength) { 172 bos.write(value); if (bos.size() == directWriteLength) { 174 initObjectOutputStream(); 175 } 176 return; 177 } 178 179 try { 180 os.writeByte(value); 181 } catch (Exception e) { 182 throw wrapper.javaSerializationException(e, "write_octet"); 183 } 184 } 185 186 public final void write_short(short value) { 187 try { 188 os.writeShort(value); 189 } catch (Exception e) { 190 throw wrapper.javaSerializationException(e, "write_short"); 191 } 192 } 193 194 public final void write_ushort(short value) { 195 this.write_short(value); 196 } 197 198 public final void write_long(int value) { 199 200 if (bos.size() < directWriteLength) { 202 203 bos.write((byte)((value >>> 24) & 0xFF)); 206 bos.write((byte)((value >>> 16) & 0xFF)); 207 bos.write((byte)((value >>> 8) & 0xFF)); 208 bos.write((byte)((value >>> 0) & 0xFF)); 209 210 if (bos.size() == directWriteLength) { 211 initObjectOutputStream(); 212 } else if (bos.size() > directWriteLength) { 213 wrapper.javaSerializationException("write_long"); 216 } 217 return; 218 } 219 220 try { 221 os.writeInt(value); 222 } catch (Exception e) { 223 throw wrapper.javaSerializationException(e, "write_long"); 224 } 225 } 226 227 public final void write_ulong(int value) { 228 this.write_long(value); 229 } 230 231 public final void write_longlong(long value) { 232 try { 233 os.writeLong(value); 234 } catch (Exception e) { 235 throw wrapper.javaSerializationException(e, "write_longlong"); 236 } 237 } 238 239 public final void write_ulonglong(long value) { 240 this.write_longlong(value); 241 } 242 243 public final void write_float(float value) { 244 try { 245 os.writeFloat(value); 246 } catch (Exception e) { 247 throw wrapper.javaSerializationException(e, "write_float"); 248 } 249 } 250 251 public final void write_double(double value) { 252 try { 253 os.writeDouble(value); 254 } catch (Exception e) { 255 throw wrapper.javaSerializationException(e, "write_double"); 256 } 257 } 258 259 261 public final void write_string(String value) { 262 try { 263 os.writeUTF(value); 264 } catch (Exception e) { 265 throw wrapper.javaSerializationException(e, "write_string"); 266 } 267 } 268 269 public final void write_wstring(String value) { 270 try { 271 os.writeObject(value); 272 } catch (Exception e) { 273 throw wrapper.javaSerializationException(e, "write_wstring"); 274 } 275 } 276 277 279 public final void write_boolean_array(boolean[] value, 280 int offset, int length) { 281 for (int i = 0; i < length; i++) { 282 write_boolean(value[offset + i]); 283 } 284 } 285 286 public final void write_char_array(char[] value, int offset, int length) { 287 for (int i = 0; i < length; i++) { 288 write_char(value[offset + i]); 289 } 290 } 291 292 public final void write_wchar_array(char[] value, int offset, int length) { 293 write_char_array(value, offset, length); 294 } 295 296 public final void write_octet_array(byte[] value, int offset, int length) { 297 try { 298 os.write(value, offset, length); 299 } catch (Exception e) { 300 throw wrapper.javaSerializationException(e, "write_octet_array"); 301 } 302 } 303 304 public final void write_short_array(short[] value, 305 int offset, int length) { 306 for (int i = 0; i < length; i++) { 307 write_short(value[offset + i]); 308 } 309 } 310 311 public final void write_ushort_array(short[] value, 312 int offset, int length){ 313 write_short_array(value, offset, length); 314 } 315 316 public final void write_long_array(int[] value, int offset, int length) { 317 for (int i = 0; i < length; i++) { 318 write_long(value[offset + i]); 319 } 320 } 321 322 public final void write_ulong_array(int[] value, int offset, int length) { 323 write_long_array(value, offset, length); 324 } 325 326 public final void write_longlong_array(long[] value, 327 int offset, int length) { 328 for (int i = 0; i < length; i++) { 329 write_longlong(value[offset + i]); 330 } 331 } 332 333 public final void write_ulonglong_array(long[] value, 334 int offset,int length) { 335 write_longlong_array(value, offset, length); 336 } 337 338 public final void write_float_array(float[] value, 339 int offset, int length) { 340 for (int i = 0; i < length; i++) { 341 write_float(value[offset + i]); 342 } 343 } 344 345 public final void write_double_array(double[] value, 346 int offset, int length) { 347 for (int i = 0; i < length; i++) { 348 write_double(value[offset + i]); 349 } 350 } 351 352 354 public final void write_Object(org.omg.CORBA.Object value) { 355 if (value == null) { 356 IOR nullIOR = IORFactories.makeIOR(orb); 357 nullIOR.write(parent); 358 return; 359 } 360 if (value instanceof org.omg.CORBA.LocalObject ) { 362 throw wrapper.writeLocalObject(CompletionStatus.COMPLETED_MAYBE); 363 } 364 IOR ior = ORBUtility.connectAndGetIOR(orb, value); 365 ior.write(parent); 366 return; 367 } 368 369 public final void write_TypeCode(TypeCode tc) { 370 if (tc == null) { 371 throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); 372 } 373 TypeCodeImpl tci; 374 if (tc instanceof TypeCodeImpl) { 375 tci = (TypeCodeImpl) tc; 376 } else { 377 tci = new TypeCodeImpl(orb, tc); 378 } 379 tci.write_value((org.omg.CORBA_2_3.portable.OutputStream ) parent); 380 } 381 382 public final void write_any(Any any) { 383 if (any == null) { 384 throw wrapper.nullParam(CompletionStatus.COMPLETED_MAYBE); 385 } 386 write_TypeCode(any.type()); 387 any.write_value(parent); 388 } 389 390 public final void write_Principal(Principal p) { 391 write_long(p.name().length); 394 write_octet_array(p.name(), 0, p.name().length); 395 } 396 397 public final void write_fixed(java.math.BigDecimal bigDecimal) { 398 this.write_fixed(bigDecimal.toString(), bigDecimal.signum()); 400 } 401 402 private void write_fixed(String string, int signum) { 404 405 int stringLength = string.length(); 406 407 byte doubleDigit = 0; 409 char ch; 410 byte digit; 411 412 int numDigits = 0; 414 for (int i=0; i<stringLength; i++) { 415 ch = string.charAt(i); 416 if (ch == '-' || ch == '+' || ch == '.') 417 continue; 418 numDigits++; 419 } 420 421 for (int i=0; i<stringLength; i++) { 422 ch = string.charAt(i); 423 if (ch == '-' || ch == '+' || ch == '.') 424 continue; 425 digit = (byte)Character.digit(ch, 10); 426 if (digit == -1) { 427 throw wrapper.badDigitInFixed( 428 CompletionStatus.COMPLETED_MAYBE); 429 } 430 if (numDigits % 2 == 0) { 435 doubleDigit |= digit; 436 this.write_octet(doubleDigit); 437 doubleDigit = 0; 438 } else { 439 doubleDigit |= (digit << 4); 440 } 441 numDigits--; 442 } 443 444 if (signum == -1) { 447 doubleDigit |= 0xd; 448 } else { 449 doubleDigit |= 0xc; 450 } 451 this.write_octet(doubleDigit); 452 } 453 454 public final org.omg.CORBA.ORB orb() { 455 return this.orb; 456 } 457 458 460 public final void write_value(java.io.Serializable value) { 461 write_value(value, (String ) null); 462 } 463 464 public final void write_value(java.io.Serializable value, 465 java.lang.Class clz) { 466 write_value(value); 467 } 468 469 public final void write_value(java.io.Serializable value, 470 String repository_id) { 471 try { 472 os.writeObject(value); 473 } catch (Exception e) { 474 throw wrapper.javaSerializationException(e, "write_value"); 475 } 476 } 477 478 public final void write_value(java.io.Serializable value, 479 org.omg.CORBA.portable.BoxedValueHelper factory) { 480 this.write_value(value, (String ) null); 481 } 482 483 public final void write_abstract_interface(java.lang.Object obj) { 484 485 boolean isCorbaObject = false; org.omg.CORBA.Object theCorbaObject = null; 487 488 if (obj != null && obj instanceof org.omg.CORBA.Object ) { 490 theCorbaObject = (org.omg.CORBA.Object )obj; 491 isCorbaObject = true; 492 } 493 494 this.write_boolean(isCorbaObject); 496 497 if (isCorbaObject) { 499 write_Object(theCorbaObject); 500 } else { 501 try { 502 write_value((java.io.Serializable )obj); 503 } catch(ClassCastException cce) { 504 if (obj instanceof java.io.Serializable ) { 505 throw cce; 506 } else { 507 ORBUtility.throwNotSerializableForCorba( 508 obj.getClass().getName()); 509 } 510 } 511 } 512 } 513 514 516 public final void start_block() { 517 throw wrapper.giopVersionError(); 518 } 519 520 public final void end_block() { 521 throw wrapper.giopVersionError(); 522 } 523 524 public final void putEndian() { 525 throw wrapper.giopVersionError(); 526 } 527 528 public void writeTo(java.io.OutputStream s) throws IOException { 529 try { 530 os.flush(); 531 bos.writeTo(s); 532 } catch (Exception e) { 533 throw wrapper.javaSerializationException(e, "writeTo"); 534 } 535 } 536 537 public final byte[] toByteArray() { 538 try { 539 os.flush(); 540 return bos.toByteArray(); } catch (Exception e) { 542 throw wrapper.javaSerializationException(e, "toByteArray"); 543 } 544 } 545 546 548 public final void write_Abstract (java.lang.Object value) { 549 write_abstract_interface(value); 550 } 551 552 public final void write_Value(java.io.Serializable value) { 553 write_value(value); 554 } 555 556 public final void write_any_array(org.omg.CORBA.Any [] value, 557 int offset, int length) { 558 for(int i = 0; i < length; i++) { 559 write_any(value[offset + i]); 560 } 561 } 562 563 565 public final String [] _truncatable_ids() { 566 throw wrapper.giopVersionError(); 567 } 568 569 571 public final int getSize() { 572 try { 573 os.flush(); 574 return bos.size(); 575 } catch (Exception e) { 576 throw wrapper.javaSerializationException(e, "write_boolean"); 577 } 578 } 579 580 public final int getIndex() { 581 return getSize(); 582 } 583 584 protected int getRealIndex(int index) { 585 return getSize(); 586 } 587 588 public final void setIndex(int value) { 589 throw wrapper.giopVersionError(); 590 } 591 592 public final ByteBuffer getByteBuffer() { 593 throw wrapper.giopVersionError(); 594 } 595 596 public final void setByteBuffer(ByteBuffer byteBuffer) { 597 throw wrapper.giopVersionError(); 598 } 599 600 public final boolean isLittleEndian() { 601 return false; 603 } 604 605 public ByteBufferWithInfo getByteBufferWithInfo() { 606 try { 607 os.flush(); 608 } catch (Exception e) { 609 throw wrapper.javaSerializationException( 610 e, "getByteBufferWithInfo"); 611 } 612 ByteBuffer byteBuffer = ByteBuffer.wrap(bos.getByteArray()); 613 byteBuffer.limit(bos.size()); 614 return new ByteBufferWithInfo(this.orb, byteBuffer, bos.size()); 615 } 616 617 public void setByteBufferWithInfo(ByteBufferWithInfo bbwi) { 618 throw wrapper.giopVersionError(); 619 } 620 621 public final BufferManagerWrite getBufferManager() { 622 return bufferManager; 623 } 624 625 public final void write_fixed(java.math.BigDecimal bigDecimal, 631 short digits, short scale) { 632 String string = bigDecimal.toString(); 633 String integerPart; 634 String fractionPart; 635 StringBuffer stringBuffer; 636 637 if (string.charAt(0) == '-' || string.charAt(0) == '+') { 639 string = string.substring(1); 640 } 641 642 int dotIndex = string.indexOf('.'); 644 if (dotIndex == -1) { 645 integerPart = string; 646 fractionPart = null; 647 } else if (dotIndex == 0 ) { 648 integerPart = null; 649 fractionPart = string; 650 } else { 651 integerPart = string.substring(0, dotIndex); 652 fractionPart = string.substring(dotIndex + 1); 653 } 654 655 stringBuffer = new StringBuffer (digits); 657 if (fractionPart != null) { 658 stringBuffer.append(fractionPart); 659 } 660 while (stringBuffer.length() < scale) { 661 stringBuffer.append('0'); 662 } 663 if (integerPart != null) { 664 stringBuffer.insert(0, integerPart); 665 } 666 while (stringBuffer.length() < digits) { 667 stringBuffer.insert(0, '0'); 668 } 669 670 this.write_fixed(stringBuffer.toString(), bigDecimal.signum()); 672 } 673 674 public final void writeOctetSequenceTo( 675 org.omg.CORBA.portable.OutputStream s) { 676 byte[] buf = this.toByteArray(); s.write_long(buf.length); 678 s.write_octet_array(buf, 0, buf.length); 679 } 680 681 public final GIOPVersion getGIOPVersion() { 682 return GIOPVersion.V1_2; 683 } 684 685 public final void writeIndirection(int tag, int posIndirectedTo) { 686 throw wrapper.giopVersionError(); 687 } 688 689 void freeInternalCaches() {} 690 691 void printBuffer() { 692 byte[] buf = this.toByteArray(); 693 694 System.out.println("+++++++ Output Buffer ++++++++"); 695 System.out.println(); 696 System.out.println("Current position: " + buf.length); 697 System.out.println(); 699 700 char[] charBuf = new char[16]; 701 702 try { 703 704 for (int i = 0; i < buf.length; i += 16) { 705 706 int j = 0; 707 708 while (j < 16 && j + i < buf.length) { 713 int k = buf[i + j]; 714 if (k < 0) 715 k = 256 + k; 716 String hex = Integer.toHexString(k); 717 if (hex.length() == 1) 718 hex = "0" + hex; 719 System.out.print(hex + " "); 720 j++; 721 } 722 723 while (j < 16) { 727 System.out.print(" "); 728 j++; 729 } 730 731 int x = 0; 734 735 while (x < 16 && x + i < buf.length) { 736 if (ORBUtility.isPrintable((char)buf[i + x])) { 737 charBuf[x] = (char) buf[i + x]; 738 } else { 739 charBuf[x] = '.'; 740 } 741 x++; 742 } 743 System.out.println(new String (charBuf, 0, x)); 744 } 745 } catch (Throwable t) { 746 t.printStackTrace(); 747 } 748 System.out.println("++++++++++++++++++++++++++++++"); 749 } 750 751 public void alignOnBoundary(int octetBoundary) { 752 throw wrapper.giopVersionError(); 753 } 754 755 public void setHeaderPadding(boolean headerPadding) { 757 } 760 761 763 public void start_value(String rep_id) { 764 throw wrapper.giopVersionError(); 765 } 766 767 public void end_value() { 768 throw wrapper.giopVersionError(); 769 } 770 771 773 775 } 781 | Popular Tags |