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