1. /*
  2. * @(#)TypeCodeInputStream.java 1.8 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 java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Collections;
  13. import java.util.ArrayList;
  14. import java.io.IOException;
  15. import java.io.PrintStream;
  16. import java.io.ByteArrayOutputStream;
  17. import java.math.BigDecimal;
  18. import java.math.BigInteger;
  19. import java.nio.ByteBuffer;
  20. import org.omg.CORBA.TypeCode ;
  21. import org.omg.CORBA.StructMember ;
  22. import org.omg.CORBA.UnionMember ;
  23. import org.omg.CORBA.ValueMember ;
  24. import org.omg.CORBA.TCKind ;
  25. import org.omg.CORBA.Any ;
  26. import org.omg.CORBA.Principal ;
  27. import org.omg.CORBA.BAD_TYPECODE ;
  28. import org.omg.CORBA.BAD_PARAM ;
  29. import org.omg.CORBA.BAD_OPERATION ;
  30. import org.omg.CORBA.INTERNAL ;
  31. import org.omg.CORBA.MARSHAL ;
  32. import org.omg.CORBA.TypeCodePackage.BadKind ;
  33. import org.omg.CORBA_2_3.portable.InputStream;
  34. import org.omg.CORBA_2_3.portable.OutputStream;
  35. import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
  36. import com.sun.corba.se.impl.corba.TypeCodeImpl;
  37. import com.sun.corba.se.spi.orb.ORB;
  38. import com.sun.corba.se.impl.encoding.CodeSetConversion;
  39. import com.sun.corba.se.impl.encoding.CDRInputStream;
  40. import com.sun.corba.se.impl.encoding.CDROutputStream;
  41. import com.sun.corba.se.impl.encoding.MarshalInputStream;
  42. public class TypeCodeInputStream extends EncapsInputStream implements TypeCodeReader
  43. {
  44. private Map typeMap = null;
  45. private InputStream enclosure = null;
  46. private boolean isEncapsulation = false;
  47. public TypeCodeInputStream(org.omg.CORBA.ORB orb, byte[] data, int size) {
  48. super(orb, data, size);
  49. }
  50. public TypeCodeInputStream(org.omg.CORBA.ORB orb,
  51. byte[] data,
  52. int size,
  53. boolean littleEndian,
  54. GIOPVersion version) {
  55. super(orb, data, size, littleEndian, version);
  56. }
  57. public TypeCodeInputStream(org.omg.CORBA.ORB orb,
  58. ByteBuffer byteBuffer,
  59. int size,
  60. boolean littleEndian,
  61. GIOPVersion version) {
  62. super(orb, byteBuffer, size, littleEndian, version);
  63. }
  64. public void addTypeCodeAtPosition(TypeCodeImpl tc, int position) {
  65. if (typeMap == null) {
  66. //if (TypeCodeImpl.debug) System.out.println("Creating typeMap");
  67. typeMap = new HashMap(16);
  68. }
  69. //if (TypeCodeImpl.debug) System.out.println(this + " adding tc " + tc + " at position " + position);
  70. typeMap.put(new Integer(position), tc);
  71. }
  72. public TypeCodeImpl getTypeCodeAtPosition(int position) {
  73. if (typeMap == null)
  74. return null;
  75. //if (TypeCodeImpl.debug) {
  76. //System.out.println("Getting tc " + (TypeCode)typeMap.get(new Integer(position)) +
  77. //" at position " + position);
  78. //}
  79. return (TypeCodeImpl)typeMap.get(new Integer(position));
  80. }
  81. public void setEnclosingInputStream(InputStream enclosure) {
  82. this.enclosure = enclosure;
  83. }
  84. public TypeCodeReader getTopLevelStream() {
  85. if (enclosure == null)
  86. return this;
  87. if (enclosure instanceof TypeCodeReader)
  88. return ((TypeCodeReader)enclosure).getTopLevelStream();
  89. return this;
  90. }
  91. public int getTopLevelPosition() {
  92. if (enclosure != null && enclosure instanceof TypeCodeReader) {
  93. // The enclosed stream has to consider if the enclosing stream
  94. // had to read the enclosed stream completely when creating it.
  95. // This is why the size of the enclosed stream needs to be substracted.
  96. int topPos = ((TypeCodeReader)enclosure).getTopLevelPosition();
  97. // Substract getBufferLength from the parents pos because it read this stream
  98. // from its own when creating it
  99. int pos = topPos - getBufferLength() + getPosition();
  100. //if (TypeCodeImpl.debug) {
  101. //System.out.println("TypeCodeInputStream.getTopLevelPosition using getTopLevelPosition " + topPos +
  102. //(isEncapsulation ? " - encaps length 4" : "") +
  103. //" - getBufferLength() " + getBufferLength() +
  104. //" + getPosition() " + getPosition() + " = " + pos);
  105. //}
  106. return pos;
  107. }
  108. //if (TypeCodeImpl.debug) {
  109. //System.out.println("TypeCodeInputStream.getTopLevelPosition returning getPosition() = " +
  110. //getPosition() + " because enclosure is " + enclosure);
  111. //}
  112. return getPosition();
  113. }
  114. public static TypeCodeInputStream readEncapsulation(InputStream is, org.omg.CORBA.ORB _orb) {
  115. // _REVISIT_ Would be nice if we didn't have to copy the buffer!
  116. TypeCodeInputStream encap;
  117. int encapLength = is.read_long();
  118. // read off part of the buffer corresponding to the encapsulation
  119. byte[] encapBuffer = new byte[encapLength];
  120. is.read_octet_array(encapBuffer, 0, encapBuffer.length);
  121. // create an encapsulation using the marshal buffer
  122. if (is instanceof CDRInputStream) {
  123. encap = new TypeCodeInputStream((ORB)_orb, encapBuffer, encapBuffer.length,
  124. ((CDRInputStream)is).isLittleEndian(),
  125. ((CDRInputStream)is).getGIOPVersion());
  126. } else {
  127. encap = new TypeCodeInputStream((ORB)_orb, encapBuffer, encapBuffer.length);
  128. }
  129. encap.setEnclosingInputStream(is);
  130. encap.makeEncapsulation();
  131. //if (TypeCodeImpl.debug) {
  132. //System.out.println("Created TypeCodeInputStream " + encap + " with parent " + is);
  133. //encap.printBuffer();
  134. //}
  135. return encap;
  136. }
  137. protected void makeEncapsulation() {
  138. // first entry in an encapsulation is the endianess
  139. consumeEndian();
  140. isEncapsulation = true;
  141. }
  142. public void printTypeMap() {
  143. System.out.println("typeMap = {");
  144. Iterator i = typeMap.keySet().iterator();
  145. while (i.hasNext()) {
  146. Integer pos = (Integer)i.next();
  147. TypeCodeImpl tci = (TypeCodeImpl)typeMap.get(pos);
  148. System.out.println(" key = " + pos.intValue() + ", value = " + tci.description());
  149. }
  150. System.out.println("}");
  151. }
  152. }