1. /*
  2. * @(#)CDRInputStream_1_1.java 1.13 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.impl.encoding;
  8. import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
  9. public class CDRInputStream_1_1 extends CDRInputStream_1_0
  10. {
  11. // See notes in CDROutputStream
  12. protected int fragmentOffset = 0;
  13. public GIOPVersion getGIOPVersion() {
  14. return GIOPVersion.V1_1;
  15. }
  16. // Template method
  17. public CDRInputStreamBase dup() {
  18. CDRInputStreamBase result = super.dup();
  19. ((CDRInputStream_1_1)result).fragmentOffset = this.fragmentOffset;
  20. return result;
  21. }
  22. protected int get_offset() {
  23. return bbwi.position() + fragmentOffset;
  24. }
  25. protected void alignAndCheck(int align, int n) {
  26. checkBlockLength(align, n);
  27. // WARNING: Must compute real alignment after calling
  28. // checkBlockLength since it may move the position
  29. int alignment = computeAlignment(bbwi.position(), align);
  30. if (bbwi.position() + n + alignment > bbwi.buflen) {
  31. // Some other ORBs may have found a way to send 1.1
  32. // fragments which put alignment bytes at the end
  33. // of a fragment
  34. if (bbwi.position() + alignment == bbwi.buflen)
  35. {
  36. bbwi.position(bbwi.position() + alignment);
  37. }
  38. grow(align, n);
  39. // We must recalculate the alignment after a possible
  40. // fragmentation since the new bbwi.position() (after the header)
  41. // may require a different alignment.
  42. alignment = computeAlignment(bbwi.position(), align);
  43. }
  44. bbwi.position(bbwi.position() + alignment);
  45. }
  46. //
  47. // This can be overridden....
  48. //
  49. protected void grow(int align, int n) {
  50. bbwi.needed = n;
  51. // Save the size of the current buffer for
  52. // possible fragmentOffset calculation
  53. int oldSize = bbwi.position();
  54. bbwi = bufferManagerRead.underflow(bbwi);
  55. if (bbwi.fragmented) {
  56. // By this point we should be guaranteed to have
  57. // a new fragment whose header has already been
  58. // unmarshalled. bbwi.position() should point to the
  59. // end of the header.
  60. fragmentOffset += (oldSize - bbwi.position());
  61. markAndResetHandler.fragmentationOccured(bbwi);
  62. // Clear the flag
  63. bbwi.fragmented = false;
  64. }
  65. }
  66. // Mark/reset ---------------------------------------
  67. private class FragmentableStreamMemento extends StreamMemento
  68. {
  69. private int fragmentOffset_;
  70. public FragmentableStreamMemento()
  71. {
  72. super();
  73. fragmentOffset_ = fragmentOffset;
  74. }
  75. }
  76. public java.lang.Object createStreamMemento() {
  77. return new FragmentableStreamMemento();
  78. }
  79. public void restoreInternalState(java.lang.Object streamMemento)
  80. {
  81. super.restoreInternalState(streamMemento);
  82. fragmentOffset
  83. = ((FragmentableStreamMemento)streamMemento).fragmentOffset_;
  84. }
  85. // --------------------------------------------------
  86. public char read_wchar() {
  87. // In GIOP 1.1, interoperability with wchar is limited
  88. // to 2 byte fixed width encodings. CORBA formal 99-10-07 15.3.1.6.
  89. // WARNING: For UTF-16, this means that there can be no
  90. // byte order marker, so it must default to big endian!
  91. alignAndCheck(2, 2);
  92. // Because of the alignAndCheck, we should be guaranteed
  93. // 2 bytes of real data.
  94. char[] result = getConvertedChars(2, getWCharConverter());
  95. // Did the provided bytes convert to more than one
  96. // character? This may come up as more unicode values are
  97. // assigned, and a single 16 bit Java char isn't enough.
  98. // Better to use strings for i18n purposes.
  99. if (getWCharConverter().getNumChars() > 1)
  100. throw wrapper.btcResultMoreThanOneChar() ;
  101. return result[0];
  102. }
  103. public String read_wstring() {
  104. // In GIOP 1.1, interoperability with wchar is limited
  105. // to 2 byte fixed width encodings. CORBA formal 99-10-07 15.3.1.6.
  106. int len = read_long();
  107. // Workaround for ORBs which send string lengths of
  108. // zero to mean empty string.
  109. //
  110. // IMPORTANT: Do not replace 'new String("")' with "", it may result
  111. // in a Serialization bug (See serialization.zerolengthstring) and
  112. // bug id: 4728756 for details
  113. if (len == 0)
  114. return new String("");
  115. checkForNegativeLength(len);
  116. // Don't include the two byte null for the
  117. // following computations. Remember that since we're limited
  118. // to a 2 byte fixed width code set, the "length" was the
  119. // number of such 2 byte code points plus a 2 byte null.
  120. len = len - 1;
  121. char[] result = getConvertedChars(len * 2, getWCharConverter());
  122. // Skip over the 2 byte null
  123. read_short();
  124. return new String(result, 0, getWCharConverter().getNumChars());
  125. }
  126. }