1 /*
2  * Copyright (c) 2002, 2003, 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 
40 import org.omg.CORBA.TypeCode ;
41 import org.omg.CORBA.StructMember ;
42 import org.omg.CORBA.UnionMember ;
43 import org.omg.CORBA.ValueMember ;
44 import org.omg.CORBA.TCKind ;
45 import org.omg.CORBA.Any ;
46 import org.omg.CORBA.Principal ;
47 import org.omg.CORBA.BAD_TYPECODE ;
48 import org.omg.CORBA.BAD_PARAM ;
49 import org.omg.CORBA.BAD_OPERATION ;
50 import org.omg.CORBA.INTERNAL ;
51 import org.omg.CORBA.MARSHAL ;
52 
53 import org.omg.CORBA.TypeCodePackage.BadKind ;
54 
55 import org.omg.CORBA_2_3.portable.InputStream;
56 import org.omg.CORBA_2_3.portable.OutputStream;
57 
58 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
59 import com.sun.corba.se.impl.corba.TypeCodeImpl;
60 import com.sun.corba.se.spi.orb.ORB;
61 import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry;
62 import com.sun.corba.se.impl.encoding.MarshalInputStream;
63 import com.sun.corba.se.impl.encoding.CodeSetConversion;
64 import com.sun.corba.se.impl.encoding.CDRInputStream;
65 import com.sun.corba.se.impl.encoding.CDROutputStream;
66 
67 public class WrapperInputStream extends org.omg.CORBA_2_3.portable.InputStream implements TypeCodeReader
68 {
69     private CDRInputStream stream;
70     private Map typeMap = null;
71     private int startPos = 0;
72 
WrapperInputStream(CDRInputStream s)73     public WrapperInputStream(CDRInputStream s) {
74         super();
75         stream = s;
76         startPos = stream.getPosition();
77     }
78 
read()79     public int read() throws IOException { return stream.read(); }
read(byte b[])80     public int read(byte b[]) throws IOException { return stream.read(b); }
read(byte b[], int off, int len)81     public int read(byte b[], int off, int len) throws IOException {
82         return stream.read(b, off, len);
83     }
skip(long n)84     public long skip(long n) throws IOException { return stream.skip(n); }
available()85     public int available() throws IOException { return stream.available(); }
close()86     public void close() throws IOException { stream.close(); }
mark(int readlimit)87     public void mark(int readlimit) { stream.mark(readlimit); }
reset()88     public void reset() { stream.reset(); }
markSupported()89     public boolean markSupported() { return stream.markSupported(); }
getPosition()90     public int getPosition() { return stream.getPosition(); }
consumeEndian()91     public void consumeEndian() { stream.consumeEndian(); }
read_boolean()92     public boolean read_boolean() { return stream.read_boolean(); }
read_char()93     public char read_char() { return stream.read_char(); }
read_wchar()94     public char read_wchar() { return stream.read_wchar(); }
read_octet()95     public byte read_octet() { return stream.read_octet(); }
read_short()96     public short read_short() { return stream.read_short(); }
read_ushort()97     public short read_ushort() { return stream.read_ushort(); }
read_long()98     public int read_long() { return stream.read_long(); }
read_ulong()99     public int read_ulong() { return stream.read_ulong(); }
read_longlong()100     public long read_longlong() { return stream.read_longlong(); }
read_ulonglong()101     public long read_ulonglong() { return stream.read_ulonglong(); }
read_float()102     public float read_float() { return stream.read_float(); }
read_double()103     public double read_double() { return stream.read_double(); }
read_string()104     public String read_string() { return stream.read_string(); }
read_wstring()105     public String read_wstring() { return stream.read_wstring(); }
106 
read_boolean_array(boolean[] value, int offset, int length)107     public void read_boolean_array(boolean[] value, int offset, int length) {
108         stream.read_boolean_array(value, offset, length);
109     }
read_char_array(char[] value, int offset, int length)110     public void read_char_array(char[] value, int offset, int length) {
111         stream.read_char_array(value, offset, length);
112     }
read_wchar_array(char[] value, int offset, int length)113     public void read_wchar_array(char[] value, int offset, int length) {
114         stream.read_wchar_array(value, offset, length);
115     }
read_octet_array(byte[] value, int offset, int length)116     public void read_octet_array(byte[] value, int offset, int length) {
117         stream.read_octet_array(value, offset, length);
118     }
read_short_array(short[] value, int offset, int length)119     public void read_short_array(short[] value, int offset, int length) {
120         stream.read_short_array(value, offset, length);
121     }
read_ushort_array(short[] value, int offset, int length)122     public void read_ushort_array(short[] value, int offset, int length) {
123         stream.read_ushort_array(value, offset, length);
124     }
read_long_array(int[] value, int offset, int length)125     public void read_long_array(int[] value, int offset, int length) {
126         stream.read_long_array(value, offset, length);
127     }
read_ulong_array(int[] value, int offset, int length)128     public void read_ulong_array(int[] value, int offset, int length) {
129         stream.read_ulong_array(value, offset, length);
130     }
read_longlong_array(long[] value, int offset, int length)131     public void read_longlong_array(long[] value, int offset, int length) {
132         stream.read_longlong_array(value, offset, length);
133     }
read_ulonglong_array(long[] value, int offset, int length)134     public void read_ulonglong_array(long[] value, int offset, int length) {
135         stream.read_ulonglong_array(value, offset, length);
136     }
read_float_array(float[] value, int offset, int length)137     public void read_float_array(float[] value, int offset, int length) {
138         stream.read_float_array(value, offset, length);
139     }
read_double_array(double[] value, int offset, int length)140     public void read_double_array(double[] value, int offset, int length) {
141         stream.read_double_array(value, offset, length);
142     }
143 
read_Object()144     public org.omg.CORBA.Object read_Object() { return stream.read_Object(); }
read_value()145     public java.io.Serializable read_value() {return stream.read_value();}
read_TypeCode()146     public TypeCode read_TypeCode() { return stream.read_TypeCode(); }
read_any()147     public Any read_any() { return stream.read_any(); }
read_Principal()148     public Principal read_Principal() { return stream.read_Principal(); }
read_fixed()149     public java.math.BigDecimal read_fixed() { return stream.read_fixed(); }
read_Context()150     public org.omg.CORBA.Context read_Context() { return stream.read_Context(); }
151 
orb()152     public org.omg.CORBA.ORB orb() { return stream.orb(); }
153 
addTypeCodeAtPosition(TypeCodeImpl tc, int position)154     public void addTypeCodeAtPosition(TypeCodeImpl tc, int position) {
155         if (typeMap == null) {
156             //if (TypeCodeImpl.debug) System.out.println("Creating typeMap");
157             typeMap = new HashMap(16);
158         }
159         //if (TypeCodeImpl.debug) System.out.println(this + " adding tc " + tc + " at position " + position);
160         typeMap.put(new Integer(position), tc);
161     }
162 
getTypeCodeAtPosition(int position)163     public TypeCodeImpl getTypeCodeAtPosition(int position) {
164         if (typeMap == null)
165             return null;
166         //if (TypeCodeImpl.debug) System.out.println("Getting tc " + (TypeCodeImpl)typeMap.get(new Integer(position)) +
167             //" at position " + position);
168         return (TypeCodeImpl)typeMap.get(new Integer(position));
169     }
170 
setEnclosingInputStream(InputStream enclosure)171     public void setEnclosingInputStream(InputStream enclosure) {
172         // WrapperInputStream has no enclosure
173     }
174 
getTopLevelStream()175     public TypeCodeReader getTopLevelStream() {
176         // WrapperInputStream has no enclosure
177         return this;
178     }
179 
getTopLevelPosition()180     public int getTopLevelPosition() {
181         //if (TypeCodeImpl.debug) System.out.println("WrapperInputStream.getTopLevelPosition " +
182             //"returning getPosition " + getPosition() + " - startPos " + startPos +
183             //" = " + (getPosition() - startPos));
184         return getPosition() - startPos;
185     }
186 
performORBVersionSpecificInit()187     public void performORBVersionSpecificInit() {
188         // This is never actually called on a WrapperInputStream, but
189         // exists to satisfy the interface requirement.
190         stream.performORBVersionSpecificInit();
191     }
192 
resetCodeSetConverters()193     public void resetCodeSetConverters() {
194         stream.resetCodeSetConverters();
195     }
196 
197     //public void printBuffer() { stream.printBuffer(); }
198 
printTypeMap()199     public void printTypeMap() {
200         System.out.println("typeMap = {");
201         List sortedKeys = new ArrayList(typeMap.keySet());
202         Collections.sort(sortedKeys);
203         Iterator i = sortedKeys.iterator();
204         while (i.hasNext()) {
205             Integer pos = (Integer)i.next();
206             TypeCodeImpl tci = (TypeCodeImpl)typeMap.get(pos);
207             System.out.println("  key = " + pos.intValue() + ", value = " + tci.description());
208         }
209         System.out.println("}");
210     }
211 }
212