KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > encoding > TypeCodeInputStream


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