KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > interceptors > CDREncapsCodec


1 /*
2  * @(#)CDREncapsCodec.java 1.19 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.interceptors;
9
10 import org.omg.CORBA.Any JavaDoc;
11 import org.omg.CORBA.ORB JavaDoc;
12 import org.omg.CORBA.TypeCode JavaDoc;
13 import org.omg.CORBA.LocalObject JavaDoc;
14
15 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
16 import com.sun.corba.se.spi.logging.CORBALogDomains;
17
18 import com.sun.corba.se.impl.corba.AnyImpl;
19 import com.sun.corba.se.impl.encoding.EncapsInputStream;
20 import com.sun.corba.se.impl.encoding.EncapsOutputStream;
21 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
22
23 import org.omg.IOP.Codec JavaDoc;
24 import org.omg.IOP.CodecPackage.FormatMismatch JavaDoc;
25 import org.omg.IOP.CodecPackage.InvalidTypeForEncoding JavaDoc;
26 import org.omg.IOP.CodecPackage.TypeMismatch JavaDoc;
27
28 /**
29  * CDREncapsCodec is an implementation of Codec, as described
30  * in orbos/99-12-02, that supports CDR encapsulation version 1.0, 1.1, and
31  * 1.2.
32  */

33 public final class CDREncapsCodec
34     extends org.omg.CORBA.LocalObject JavaDoc
35     implements Codec JavaDoc
36 {
37     // The ORB that created the factory this codec was created from
38
private ORB JavaDoc orb;
39     ORBUtilSystemException wrapper;
40
41     // The GIOP version we are encoding for
42
private GIOPVersion giopVersion;
43
44     /*
45      *******************************************************************
46      * NOTE: CDREncapsCodec must remain immutable! This is so that we
47      * can pre-create CDREncapsCodecs for each version of GIOP in
48      * CodecFactoryImpl.
49      *******************************************************************/

50
51     /**
52      * Creates a new codec implementation. Uses the given ORB to create
53      * CDRInputStreams when necessary.
54      *
55      * @param orb The ORB to use to create a CDRInputStream or CDROutputStream
56      * @param major The major version of GIOP we are encoding for
57      * @param minor The minor version of GIOP we are encoding for
58      */

59     public CDREncapsCodec( ORB JavaDoc orb, int major, int minor ) {
60         this.orb = orb;
61     wrapper = ORBUtilSystemException.get(
62         (com.sun.corba.se.spi.orb.ORB)orb, CORBALogDomains.RPC_PROTOCOL ) ;
63
64         giopVersion = GIOPVersion.getInstance( (byte)major, (byte)minor );
65     }
66
67     /**
68      * Convert the given any into a CDR encapsulated octet sequence
69      */

70     public byte[] encode( Any JavaDoc data )
71         throws InvalidTypeForEncoding JavaDoc
72     {
73     if ( data == null )
74         throw wrapper.nullParam() ;
75         return encodeImpl( data, true );
76     }
77
78     /**
79      * Decode the given octet sequence into an any based on a CDR
80      * encapsulated octet sequence.
81      */

82     public Any JavaDoc decode ( byte[] data )
83         throws FormatMismatch JavaDoc
84     {
85     if( data == null )
86         throw wrapper.nullParam() ;
87     return decodeImpl( data, null );
88     }
89
90     /**
91      * Convert the given any into a CDR encapsulated octet sequence. Only
92      * the data is stored. The type code is not.
93      */

94     public byte[] encode_value( Any JavaDoc data )
95         throws InvalidTypeForEncoding JavaDoc
96     {
97     if( data == null )
98         throw wrapper.nullParam() ;
99         return encodeImpl( data, false );
100     }
101
102     /**
103      * Decode the given octet sequence into an any based on a CDR
104      * encapsulated octet sequence. The type code is expected not to appear
105      * in the octet sequence, and the given type code is used instead.
106      */

107     public Any JavaDoc decode_value( byte[] data, TypeCode JavaDoc tc )
108         throws FormatMismatch JavaDoc, TypeMismatch JavaDoc
109     {
110     if( data == null )
111         throw wrapper.nullParam() ;
112     if( tc == null )
113         throw wrapper.nullParam() ;
114     return decodeImpl( data, tc );
115     }
116
117     /**
118      * Convert the given any into a CDR encapsulated octet sequence.
119      * If sendTypeCode is true, the type code is sent with the message, as in
120      * a standard encapsulation. If it is false, only the data is sent.
121      * Either way, the endian type is sent as the first part of the message.
122      */

123     private byte[] encodeImpl( Any JavaDoc data, boolean sendTypeCode )
124         throws InvalidTypeForEncoding JavaDoc
125     {
126     if( data == null )
127         throw wrapper.nullParam() ;
128
129     // _REVISIT_ Note that InvalidTypeForEncoding is never thrown in
130
// the body of this method. This is due to the fact that CDR*Stream
131
// will never throw an exception if the encoding is invalid. To
132
// fix this, the CDROutputStream must know the version of GIOP it
133
// is encoding for and it must check to ensure that, for example,
134
// wstring cannot be encoded in GIOP 1.0.
135
//
136
// As part of the GIOP 1.2 work, the CDRInput and OutputStream will
137
// be versioned. This can be handled once this work is complete.
138

139     // Create output stream with default endianness.
140
EncapsOutputStream cdrOut = new EncapsOutputStream(
141         (com.sun.corba.se.spi.orb.ORB)orb, giopVersion );
142
143     // This is an encapsulation, so put out the endian:
144
cdrOut.putEndian();
145
146     // Sometimes encode type code:
147
if( sendTypeCode ) {
148         cdrOut.write_TypeCode( data.type() );
149         }
150
151     // Encode value and return.
152
data.write_value( cdrOut );
153
154     return cdrOut.toByteArray();
155     }
156
157     /**
158      * Decode the given octet sequence into an any based on a CDR
159      * encapsulated octet sequence. If the type code is null, it is
160      * expected to appear in the octet sequence. Otherwise, the given
161      * type code is used.
162      */

163     private Any JavaDoc decodeImpl( byte[] data, TypeCode JavaDoc tc )
164         throws FormatMismatch JavaDoc
165     {
166     if( data == null )
167         throw wrapper.nullParam() ;
168
169     AnyImpl any = null; // return value
170

171     // _REVISIT_ Currently there is no way for us to distinguish between
172
// a FormatMismatch and a TypeMismatch because we cannot get this
173
// information from the CDRInputStream. If a RuntimeException occurs,
174
// it is turned into a FormatMismatch exception.
175

176     try {
177         EncapsInputStream cdrIn = new EncapsInputStream( orb, data,
178                 data.length, giopVersion );
179
180         cdrIn.consumeEndian();
181
182         // If type code not specified, read it from octet stream:
183
if( tc == null ) {
184         tc = cdrIn.read_TypeCode();
185         }
186
187         // Create a new Any object:
188
any = new AnyImpl( (com.sun.corba.se.spi.orb.ORB)orb );
189         any.read_value( cdrIn, tc );
190     }
191     catch( RuntimeException JavaDoc e ) {
192         // See above note.
193
throw new FormatMismatch JavaDoc();
194     }
195
196     return any;
197     }
198 }
199
Popular Tags