1 7 8 package com.sun.corba.se.impl.interceptors; 9 10 import org.omg.CORBA.Any ; 11 import org.omg.CORBA.ORB ; 12 import org.omg.CORBA.TypeCode ; 13 import org.omg.CORBA.LocalObject ; 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 ; 24 import org.omg.IOP.CodecPackage.FormatMismatch ; 25 import org.omg.IOP.CodecPackage.InvalidTypeForEncoding ; 26 import org.omg.IOP.CodecPackage.TypeMismatch ; 27 28 33 public final class CDREncapsCodec 34 extends org.omg.CORBA.LocalObject 35 implements Codec 36 { 37 private ORB orb; 39 ORBUtilSystemException wrapper; 40 41 private GIOPVersion giopVersion; 43 44 50 51 59 public CDREncapsCodec( ORB 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 70 public byte[] encode( Any data ) 71 throws InvalidTypeForEncoding 72 { 73 if ( data == null ) 74 throw wrapper.nullParam() ; 75 return encodeImpl( data, true ); 76 } 77 78 82 public Any decode ( byte[] data ) 83 throws FormatMismatch 84 { 85 if( data == null ) 86 throw wrapper.nullParam() ; 87 return decodeImpl( data, null ); 88 } 89 90 94 public byte[] encode_value( Any data ) 95 throws InvalidTypeForEncoding 96 { 97 if( data == null ) 98 throw wrapper.nullParam() ; 99 return encodeImpl( data, false ); 100 } 101 102 107 public Any decode_value( byte[] data, TypeCode tc ) 108 throws FormatMismatch , TypeMismatch 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 123 private byte[] encodeImpl( Any data, boolean sendTypeCode ) 124 throws InvalidTypeForEncoding 125 { 126 if( data == null ) 127 throw wrapper.nullParam() ; 128 129 139 EncapsOutputStream cdrOut = new EncapsOutputStream( 141 (com.sun.corba.se.spi.orb.ORB)orb, giopVersion ); 142 143 cdrOut.putEndian(); 145 146 if( sendTypeCode ) { 148 cdrOut.write_TypeCode( data.type() ); 149 } 150 151 data.write_value( cdrOut ); 153 154 return cdrOut.toByteArray(); 155 } 156 157 163 private Any decodeImpl( byte[] data, TypeCode tc ) 164 throws FormatMismatch 165 { 166 if( data == null ) 167 throw wrapper.nullParam() ; 168 169 AnyImpl any = null; 171 176 try { 177 EncapsInputStream cdrIn = new EncapsInputStream( orb, data, 178 data.length, giopVersion ); 179 180 cdrIn.consumeEndian(); 181 182 if( tc == null ) { 184 tc = cdrIn.read_TypeCode(); 185 } 186 187 any = new AnyImpl( (com.sun.corba.se.spi.orb.ORB)orb ); 189 any.read_value( cdrIn, tc ); 190 } 191 catch( RuntimeException e ) { 192 throw new FormatMismatch (); 194 } 195 196 return any; 197 } 198 } 199 | Popular Tags |