KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)TypeCodeOutputStream.java 1.12 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 org.omg.CORBA.TypeCode JavaDoc ;
11 import org.omg.CORBA.StructMember JavaDoc ;
12 import org.omg.CORBA.UnionMember JavaDoc ;
13 import org.omg.CORBA.ValueMember JavaDoc ;
14 import org.omg.CORBA.TCKind JavaDoc ;
15 import org.omg.CORBA.Any JavaDoc ;
16 import org.omg.CORBA.Principal JavaDoc ;
17 import org.omg.CORBA.CompletionStatus JavaDoc ;
18
19 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc ;
20
21 import org.omg.CORBA_2_3.portable.InputStream JavaDoc;
22 import org.omg.CORBA_2_3.portable.OutputStream JavaDoc;
23
24 import com.sun.corba.se.spi.orb.ORB;
25 import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry;
26 import com.sun.corba.se.impl.encoding.MarshalInputStream;
27 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
28 import com.sun.corba.se.impl.encoding.CodeSetConversion;
29
30 import com.sun.corba.se.impl.encoding.CDRInputStream;
31 import com.sun.corba.se.impl.encoding.CDROutputStream;
32
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.PrintStream JavaDoc;
41 import java.io.ByteArrayOutputStream JavaDoc;
42 import java.math.BigDecimal JavaDoc;
43 import java.math.BigInteger JavaDoc;
44 import java.nio.ByteBuffer JavaDoc;
45
46 public final class TypeCodeOutputStream extends EncapsOutputStream
47 {
48     private OutputStream JavaDoc enclosure = null;
49     private Map JavaDoc typeMap = null;
50     private boolean isEncapsulation = false;
51
52     public TypeCodeOutputStream(ORB orb) {
53         super(orb, false);
54     }
55
56     public TypeCodeOutputStream(ORB orb, boolean littleEndian) {
57         super(orb, littleEndian);
58     }
59
60     public org.omg.CORBA.portable.InputStream JavaDoc create_input_stream()
61     {
62         //return new TypeCodeInputStream((ORB)orb(), getByteBuffer(), getIndex(), isLittleEndian());
63
TypeCodeInputStream tcis
64             = new TypeCodeInputStream((ORB)orb(), getByteBuffer(), getIndex(), isLittleEndian(), getGIOPVersion());
65         //if (TypeCodeImpl.debug) {
66
//System.out.println("Created TypeCodeInputStream " + tcis + " with no parent");
67
//tcis.printBuffer();
68
//}
69
return tcis;
70     }
71
72     public void setEnclosingOutputStream(OutputStream JavaDoc enclosure) {
73         this.enclosure = enclosure;
74     }
75
76     /*
77       public boolean isEncapsulatedIn(TypeCodeOutputStream outerEnclosure) {
78       if (outerEnclosure == this)
79       return true;
80       if (enclosure == null)
81       return false;
82       if (enclosure instanceof TypeCodeOutputStream)
83       return ((TypeCodeOutputStream)enclosure).isEncapsulatedIn(outerEnclosure);
84       // Last chance! Recursion ends with first non TypeCodeOutputStream.
85       return (enclosure == outerEnclosure);
86       }
87     */

88
89     public TypeCodeOutputStream getTopLevelStream() {
90         if (enclosure == null)
91             return this;
92         if (enclosure instanceof TypeCodeOutputStream)
93             return ((TypeCodeOutputStream)enclosure).getTopLevelStream();
94         return this;
95     }
96
97     public int getTopLevelPosition() {
98         if (enclosure != null && enclosure instanceof TypeCodeOutputStream) {
99             int pos = ((TypeCodeOutputStream)enclosure).getTopLevelPosition() + getPosition();
100             // Add four bytes for the encaps length, not another 4 for the byte order
101
// which is included in getPosition().
102
if (isEncapsulation) pos += 4;
103             //if (TypeCodeImpl.debug) {
104
//System.out.println("TypeCodeOutputStream.getTopLevelPosition using getTopLevelPosition " +
105
//((TypeCodeOutputStream)enclosure).getTopLevelPosition() +
106
//" + getPosition() " + getPosition() +
107
//(isEncapsulation ? " + encaps length 4" : "") +
108
//" = " + pos);
109
//}
110
return pos;
111         }
112         //if (TypeCodeImpl.debug) {
113
//System.out.println("TypeCodeOutputStream.getTopLevelPosition returning getPosition() = " +
114
//getPosition() + ", enclosure is " + enclosure);
115
//}
116
return getPosition();
117     }
118
119     public void addIDAtPosition(String JavaDoc id, int position) {
120         if (typeMap == null)
121             typeMap = new HashMap JavaDoc(16);
122         //if (TypeCodeImpl.debug) System.out.println(this + " adding id " + id + " at position " + position);
123
typeMap.put(id, new Integer JavaDoc(position));
124     }
125
126     public int getPositionForID(String JavaDoc id) {
127         if (typeMap == null)
128         throw wrapper.refTypeIndirType( CompletionStatus.COMPLETED_NO ) ;
129         //if (TypeCodeImpl.debug) System.out.println("Getting position " + ((Integer)typeMap.get(id)).intValue() +
130
//" for id " + id);
131
return ((Integer JavaDoc)typeMap.get(id)).intValue();
132     }
133
134     public void writeRawBuffer(org.omg.CORBA.portable.OutputStream JavaDoc s, int firstLong) {
135         // Writes this streams buffer to the given OutputStream
136
// without byte order flag and length as is the case for encapsulations.
137

138         // Make sure to align s to 4 byte boundaries.
139
// Unfortunately we can't do just this:
140
// s.alignAndReserve(4, 4);
141
// So we have to take the first four bytes given in firstLong and write them
142
// with a call to write_long which will trigger the alignment.
143
// Then write the rest of the byte array.
144

145         //if (TypeCodeImpl.debug) {
146
//System.out.println(this + ".writeRawBuffer(" + s + ", " + firstLong + ")");
147
//if (s instanceof CDROutputStream) {
148
//System.out.println("Parent position before writing kind = " + ((CDROutputStream)s).getIndex());
149
//}
150
//}
151
s.write_long(firstLong);
152         //if (TypeCodeImpl.debug) {
153
//if (s instanceof CDROutputStream) {
154
//System.out.println("Parent position after writing kind = " + ((CDROutputStream)s).getIndex());
155
//}
156
//}
157
ByteBuffer JavaDoc byteBuffer = getByteBuffer();
158         if (byteBuffer.hasArray())
159         {
160              s.write_octet_array(byteBuffer.array(), 4, getIndex() - 4);
161         }
162         else
163         {
164              // get bytes from DirectByteBuffer
165
// NOTE: Microbenchmarks are showing it is faster to do
166
// a loop of ByteBuffer.get(int) than it is to do
167
// a bulk ByteBuffer.get(byte[], offset, length)
168
byte[] buf = new byte[byteBuffer.limit()];
169              for (int i = 0; i < buf.length; i++)
170                   buf[i] = byteBuffer.get(i);
171              s.write_octet_array(buf, 4, getIndex() - 4);
172         }
173         //if (TypeCodeImpl.debug) {
174
//if (s instanceof CDROutputStream) {
175
//System.out.println("Parent position after writing all " + getIndex() + " bytes = " + ((CDROutputStream)s).getIndex());
176
//}
177
//}
178
}
179
180     public TypeCodeOutputStream createEncapsulation(org.omg.CORBA.ORB JavaDoc _orb) {
181     TypeCodeOutputStream encap = new TypeCodeOutputStream((ORB)_orb, isLittleEndian());
182     encap.setEnclosingOutputStream(this);
183         encap.makeEncapsulation();
184         //if (TypeCodeImpl.debug) System.out.println("Created TypeCodeOutputStream " + encap + " with parent " + this);
185
return encap;
186     }
187
188     protected void makeEncapsulation() {
189         // first entry in an encapsulation is the endianess
190
putEndian();
191         isEncapsulation = true;
192     }
193
194     public static TypeCodeOutputStream wrapOutputStream(OutputStream JavaDoc os) {
195         boolean littleEndian = ((os instanceof CDROutputStream) ? ((CDROutputStream)os).isLittleEndian() : false);
196         TypeCodeOutputStream tos = new TypeCodeOutputStream((ORB)os.orb(), littleEndian);
197         tos.setEnclosingOutputStream(os);
198         //if (TypeCodeImpl.debug) System.out.println("Created TypeCodeOutputStream " + tos + " with parent " + os);
199
return tos;
200     }
201
202     public int getPosition() {
203         return getIndex();
204     }
205
206     public int getRealIndex(int index) {
207         int topPos = getTopLevelPosition();
208         //if (TypeCodeImpl.debug) System.out.println("TypeCodeOutputStream.getRealIndex using getTopLevelPosition " +
209
//topPos + " instead of getPosition " + getPosition());
210
return topPos;
211     }
212 /*
213     protected void printBuffer() {
214         super.printBuffer();
215     }
216 */

217     public byte[] getTypeCodeBuffer() {
218         // Returns the buffer trimmed of the trailing zeros and without the
219
// known _kind value at the beginning.
220
ByteBuffer JavaDoc theBuffer = getByteBuffer();
221         //System.out.println("outBuffer length = " + (getIndex() - 4));
222
byte[] tcBuffer = new byte[getIndex() - 4];
223         // Micro-benchmarks show that DirectByteBuffer.get(int) is faster
224
// than DirectByteBuffer.get(byte[], offset, length).
225
// REVISIT - May want to check if buffer is direct or non-direct
226
// and use array copy if ByteBuffer is non-direct.
227
for (int i = 0; i < tcBuffer.length; i++)
228             tcBuffer[i] = theBuffer.get(i+4);
229         return tcBuffer;
230     }
231
232     public void printTypeMap() {
233         System.out.println("typeMap = {");
234         Iterator JavaDoc i = typeMap.keySet().iterator();
235         while (i.hasNext()) {
236             String JavaDoc id = (String JavaDoc)i.next();
237             Integer JavaDoc pos = (Integer JavaDoc)typeMap.get(id);
238             System.out.println(" key = " + id + ", value = " + pos);
239         }
240         System.out.println("}");
241     }
242 }
243
Popular Tags