1 /*
   2  * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.impl.encoding;
  27 
  28 import java.util.HashMap;
  29 import java.util.Map;
  30 import java.util.Iterator;
  31 import java.util.List;
  32 import java.util.Collections;
  33 import java.util.ArrayList;
  34 import java.io.IOException;
  35 import java.io.PrintStream;
  36 import java.io.ByteArrayOutputStream;
  37 import java.math.BigDecimal;
  38 import java.math.BigInteger;
  39 import java.nio.ByteBuffer;
  40 
  41 import org.omg.CORBA.TypeCode ;
  42 import org.omg.CORBA.StructMember ;
  43 import org.omg.CORBA.UnionMember ;
  44 import org.omg.CORBA.ValueMember ;
  45 import org.omg.CORBA.TCKind ;
  46 import org.omg.CORBA.Any ;
  47 import org.omg.CORBA.Principal ;
  48 import org.omg.CORBA.BAD_TYPECODE ;
  49 import org.omg.CORBA.BAD_PARAM ;
  50 import org.omg.CORBA.BAD_OPERATION ;
  51 import org.omg.CORBA.INTERNAL ;
  52 import org.omg.CORBA.MARSHAL ;
  53 
  54 import org.omg.CORBA.TypeCodePackage.BadKind ;
  55 
  56 import org.omg.CORBA_2_3.portable.InputStream;
  57 import org.omg.CORBA_2_3.portable.OutputStream;
  58 
  59 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
  60 import com.sun.corba.se.impl.corba.TypeCodeImpl;
  61 import com.sun.corba.se.spi.orb.ORB;
  62 import com.sun.corba.se.impl.encoding.CodeSetConversion;
  63 import com.sun.corba.se.impl.encoding.CDRInputStream;
  64 import com.sun.corba.se.impl.encoding.CDROutputStream;
  65 import com.sun.corba.se.impl.encoding.MarshalInputStream;
  66 
  67 import sun.corba.EncapsInputStreamFactory;
  68 
  69 public class TypeCodeInputStream extends EncapsInputStream implements TypeCodeReader
  70 {
  71     private Map typeMap = null;
  72     private InputStream enclosure = null;
  73     private boolean isEncapsulation = false;
  74 
  75     public TypeCodeInputStream(org.omg.CORBA.ORB orb, byte[] data, int size) {
  76         super(orb, data, size);
  77     }
  78 
  79     public TypeCodeInputStream(org.omg.CORBA.ORB orb,
  80                                byte[] data,
  81                                int size,
  82                                boolean littleEndian,
  83                                GIOPVersion version) {
  84         super(orb, data, size, littleEndian, version);
  85     }
  86 
  87     public TypeCodeInputStream(org.omg.CORBA.ORB orb,
  88                                ByteBuffer byteBuffer,
  89                                int size,
  90                                boolean littleEndian,
  91                                GIOPVersion version) {
  92         super(orb, byteBuffer, size, littleEndian, version);
  93     }
  94 
  95     public void addTypeCodeAtPosition(TypeCodeImpl tc, int position) {
  96         if (typeMap == null) {
  97             //if (TypeCodeImpl.debug) System.out.println("Creating typeMap");
  98             typeMap = new HashMap(16);
  99         }
 100         //if (TypeCodeImpl.debug) System.out.println(this + " adding tc " + tc + " at position " + position);
 101         typeMap.put(new Integer(position), tc);
 102     }
 103 
 104     public TypeCodeImpl getTypeCodeAtPosition(int position) {
 105         if (typeMap == null)
 106             return null;
 107         //if (TypeCodeImpl.debug) {
 108             //System.out.println("Getting tc " + (TypeCode)typeMap.get(new Integer(position)) +
 109                                //" at position " + position);
 110         //}
 111         return (TypeCodeImpl)typeMap.get(new Integer(position));
 112     }
 113 
 114     public void setEnclosingInputStream(InputStream enclosure) {
 115         this.enclosure = enclosure;
 116     }
 117 
 118     public TypeCodeReader getTopLevelStream() {
 119         if (enclosure == null)
 120             return this;
 121         if (enclosure instanceof TypeCodeReader)
 122             return ((TypeCodeReader)enclosure).getTopLevelStream();
 123         return this;
 124     }
 125 
 126     public int getTopLevelPosition() {
 127         if (enclosure != null && enclosure instanceof TypeCodeReader) {
 128             // The enclosed stream has to consider if the enclosing stream
 129             // had to read the enclosed stream completely when creating it.
 130             // This is why the size of the enclosed stream needs to be substracted.
 131             int topPos = ((TypeCodeReader)enclosure).getTopLevelPosition();
 132             // Substract getBufferLength from the parents pos because it read this stream
 133             // from its own when creating it
 134             int pos = topPos - getBufferLength() + getPosition();
 135             //if (TypeCodeImpl.debug) {
 136                 //System.out.println("TypeCodeInputStream.getTopLevelPosition using getTopLevelPosition " + topPos +
 137                     //(isEncapsulation ? " - encaps length 4" : "") +
 138                     //" - getBufferLength() " + getBufferLength() +
 139                     //" + getPosition() " + getPosition() + " = " + pos);
 140             //}
 141             return pos;
 142         }
 143         //if (TypeCodeImpl.debug) {
 144             //System.out.println("TypeCodeInputStream.getTopLevelPosition returning getPosition() = " +
 145                                //getPosition() + " because enclosure is " + enclosure);
 146         //}
 147         return getPosition();
 148     }
 149 
 150     public static TypeCodeInputStream readEncapsulation(InputStream is, org.omg.CORBA.ORB _orb) {
 151         // _REVISIT_ Would be nice if we didn't have to copy the buffer!
 152         TypeCodeInputStream encap;
 153 
 154         int encapLength = is.read_long();
 155 
 156         // read off part of the buffer corresponding to the encapsulation
 157         byte[] encapBuffer = new byte[encapLength];
 158         is.read_octet_array(encapBuffer, 0, encapBuffer.length);
 159 
 160         // create an encapsulation using the marshal buffer
 161         if (is instanceof CDRInputStream) {
 162             encap = EncapsInputStreamFactory.newTypeCodeInputStream((ORB) _orb,
 163                     encapBuffer, encapBuffer.length,
 164                     ((CDRInputStream) is).isLittleEndian(),
 165                     ((CDRInputStream) is).getGIOPVersion());
 166         } else {
 167             encap = EncapsInputStreamFactory.newTypeCodeInputStream((ORB) _orb,
 168                     encapBuffer, encapBuffer.length);
 169         }
 170         encap.setEnclosingInputStream(is);
 171         encap.makeEncapsulation();
 172         //if (TypeCodeImpl.debug) {
 173             //System.out.println("Created TypeCodeInputStream " + encap + " with parent " + is);
 174             //encap.printBuffer();
 175         //}
 176         return encap;
 177     }
 178 
 179     protected void makeEncapsulation() {
 180         // first entry in an encapsulation is the endianess
 181         consumeEndian();
 182         isEncapsulation = true;
 183     }
 184 
 185     public void printTypeMap() {
 186         System.out.println("typeMap = {");
 187         Iterator i = typeMap.keySet().iterator();
 188         while (i.hasNext()) {
 189             Integer pos = (Integer)i.next();
 190             TypeCodeImpl tci = (TypeCodeImpl)typeMap.get(pos);
 191             System.out.println("  key = " + pos.intValue() + ", value = " + tci.description());
 192         }
 193         System.out.println("}");
 194     }
 195 }