1 package org.jacorb.orb.giop; 2 3 22 23 import java.io.IOException ; 24 import java.util.Vector ; 25 import org.jacorb.orb.CDROutputStream; 26 import org.jacorb.orb.ORBConstants; 27 import org.omg.CORBA.MARSHAL ; 28 import org.omg.IOP.ServiceContext ; 29 import org.omg.IOP.ServiceContextHelper ; 30 31 40 41 public class ServiceContextTransportingOutputStream 42 extends MessageOutputStream 43 { 44 48 private int header_end = -1; 49 50 54 private int header_padding = 0; 55 56 61 private static ServiceContext padding_ctx = 62 new ServiceContext (ORBConstants.SERVICE_PADDING_CONTEXT, new byte[0]); 63 64 private Vector contexts; 65 66 67 public ServiceContextTransportingOutputStream() 68 { 69 super(); 70 } 71 72 78 protected void markHeaderEnd() 79 { 80 header_end = size(); 81 82 header_padding = 8 - (size() % 8); header_padding = (header_padding == 8)? 0 : header_padding; 84 85 skip( header_padding ); 86 } 87 88 private int getHeaderEnd() 89 { 90 return header_end; 91 } 92 93 private int getBodyBegin() 94 { 95 return header_end + header_padding; 96 } 97 98 99 private int getHeaderPadding() 100 { 101 return header_padding; 102 } 103 104 private boolean hasBody() 105 { 106 return size() > getBodyBegin(); 107 } 108 109 110 public void insertMsgSize() 111 { 112 if( header_padding == 0 ) 113 { 114 insertMsgSize( size() - Messages.MSG_HEADER_SIZE ); 115 } 116 else 117 { 118 if( size() > header_end + header_padding) 119 { 120 insertMsgSize( size() - Messages.MSG_HEADER_SIZE ); 122 } 123 else 124 { 125 insertMsgSize( size() - header_padding - Messages.MSG_HEADER_SIZE ); 127 128 reduceSize( header_padding ); 129 } 130 } 131 } 132 133 public void write_to( GIOPConnection conn ) 134 throws IOException 135 { 136 CDROutputStream ctx_out = null; 137 138 if( contexts == null || contexts.size() == 0 ) 139 { 140 insertMsgSize(); 143 write( conn, 0, size() ); 144 } 145 else 146 { 147 switch( giop_minor ) 148 { 149 case 0 : 150 { 151 } 153 case 1 : 154 { 155 157 160 contexts.addElement( padding_ctx ); 162 163 ctx_out = createContextStream(); 164 165 167 int difference = 172 (8 - ((Messages.MSG_HEADER_SIZE + ctx_out.size ()) % 8)); 173 difference = (difference == 8)? 0 : difference; 174 175 if( difference > 0 ) 176 { 177 183 ctx_out.reduceSize( 4 ); 185 186 ctx_out.write_ulong( difference ); 188 189 ctx_out.increaseSize( difference ); 193 } 194 195 insertMsgSize( size() 201 - Messages.MSG_HEADER_SIZE 202 - 4 203 + ctx_out.size() ); 204 205 206 write( conn, 0, Messages.MSG_HEADER_SIZE ); 210 211 ctx_out.write( conn, 0, ctx_out.size() ); 213 214 217 write( conn, 218 Messages.MSG_HEADER_SIZE + 4, 219 size() - 220 (Messages.MSG_HEADER_SIZE + 4) ); 221 break; 222 } 223 case 2 : 224 { 225 227 230 233 ctx_out = createContextStream(); 234 235 int new_header_end = getHeaderEnd() - 4 + ctx_out.size(); 240 241 int difference = 8 - (new_header_end % 8); 243 difference = (difference == 8)? 0 : difference; 244 245 if( difference > 0 && hasBody() ) 246 { 247 ctx_out.increaseSize( difference ); 252 } 253 254 insertMsgSize( size() 262 - Messages.MSG_HEADER_SIZE 263 - 4 264 - getHeaderPadding() 265 + ctx_out.size() ); 266 267 write( conn, 273 0, 274 getHeaderEnd() - 4 ); 275 276 278 ctx_out.write( conn, 0, ctx_out.size()); 279 280 283 write( conn, 284 getBodyBegin(), 285 size() - getBodyBegin() ); 286 287 break; 288 } 289 default : 290 { 291 throw new MARSHAL ( "Unknown GIOP minor: " + giop_minor ); 292 } 293 } 294 } 295 close(); 296 if ( ctx_out != null ) 297 { 298 ctx_out.close(); 299 ctx_out = null; 300 } 301 } 302 303 public void addServiceContext( ServiceContext ctx ) 304 { 305 if( contexts == null ) 306 { 307 contexts = new Vector (); 308 } 309 310 contexts.add( ctx ); 311 } 312 313 314 317 318 public byte[] getBody() 319 { 320 byte [] result = 321 org.jacorb.orb.BufferManager.getInstance().getBuffer( size() - getBodyBegin()); 322 323 System.arraycopy( getBufferCopy(), getBodyBegin(), result, 0, result.length ); 324 325 return result; 326 } 327 328 329 private CDROutputStream createContextStream() 330 { 331 CDROutputStream out = new CDROutputStream( (org.omg.CORBA.ORB ) null ); 332 333 out.write_ulong( contexts.size() ); 335 336 for( int i = 0; i < contexts.size(); i++ ) 337 { 338 ServiceContextHelper.write( out, 339 (ServiceContext ) contexts.elementAt( i )); 340 } 341 342 return out; 343 } 344 345 } | Popular Tags |