1 18 package org.apache.geronimo.interop.rmi.iiop; 19 20 import java.io.IOException ; 21 import java.io.ObjectOutput ; 22 import java.io.ObjectStreamClass ; 23 import java.io.ObjectStreamField ; 24 import java.lang.reflect.Method ; 25 26 31 32 public class PutField extends java.io.ObjectOutputStream.PutField 33 { 34 35 private final ObjectStreamClass desc; 36 37 private final byte[] primVals; 38 39 private final Object [] objVals; 40 41 private int primDataSize = 0; 42 private int numObjFields = 0; 43 private ObjectStreamField [] _fields = null; 44 45 private static Method setOffsetMethod; 46 47 static 48 { 49 try 50 { 51 Class osFieldClass = java.io.ObjectStreamField .class; 52 Class [] params = new Class [1]; 53 params[0] = int.class; 54 setOffsetMethod = osFieldClass.getDeclaredMethod("setOffset", params); 55 setOffsetMethod.setAccessible(true); 56 } 57 catch (Throwable t) 58 { 59 t.printStackTrace(); 60 } 61 } 62 63 67 PutField(ObjectStreamClass desc) 68 { 69 this.desc = desc; 70 computeOffsets(); 71 primVals = new byte[primDataSize]; 72 objVals = new Object [numObjFields]; 73 } 74 75 81 public void put(String name, boolean val) 82 { 83 Bits.putBoolean(primVals, getFieldOffset(name, Boolean.TYPE), val); 84 } 85 86 92 public void put(String name, byte val) 93 { 94 primVals[getFieldOffset(name, Byte.TYPE)] = val; 95 } 96 97 103 public void put(String name, char val) 104 { 105 Bits.putChar(primVals, getFieldOffset(name, Character.TYPE), val); 106 } 107 108 114 public void put(String name, short val) 115 { 116 Bits.putShort(primVals, getFieldOffset(name, Short.TYPE), val); 117 } 118 119 125 public void put(String name, int val) 126 { 127 Bits.putInt(primVals, getFieldOffset(name, Integer.TYPE), val); 128 } 129 130 136 public void put(String name, long val) 137 { 138 Bits.putLong(primVals, getFieldOffset(name, Long.TYPE), val); 139 } 140 141 147 public void put(String name, float val) 148 { 149 Bits.putFloat(primVals, getFieldOffset(name, Float.TYPE), val); 150 } 151 152 158 public void put(String name, double val) 159 { 160 Bits.putDouble(primVals, getFieldOffset(name, Double.TYPE), val); 161 } 162 163 169 public void put(String name, Object val) 170 { 171 objVals[getFieldOffset(name, Object .class)] = val; 172 } 173 174 187 public void write(ObjectOutput out) throws IOException 188 { 189 204 throw new IOException ("PutField.write(ObjectOutput) - not supported for RMI/IIOP"); 205 } 206 207 210 void writeFields(ObjectOutputStream o) throws IOException 211 { 212 org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream out = (org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream)o; 213 214 out._cdrOutput.write_align(4, 4); 216 for(int i = 0; i < primVals.length; i++) 218 { 219 out.writeByte(primVals[i]); 220 } 221 222 java.io.ObjectStreamField [] fields = desc.getFields(); 224 int numPrimFields = fields.length - objVals.length; 225 for (int i = 0; i < objVals.length; i++) 226 { 227 out.writeObject(ValueType.getInstance(objVals[i].getClass()), objVals[i]); 228 } 229 } 230 231 237 private int getFieldOffset(String name, Class type) 238 { 239 ObjectStreamField field = getField(name, type); 240 if (field == null) 241 { 242 throw new IllegalArgumentException ("no such field"); 243 } 244 return field.getOffset(); 245 } 246 247 private ObjectStreamField getField(String name, Class type) 248 { 249 if(type == null) 250 { 251 for(int i = 0; i < _fields.length; i++) 253 { 254 if(_fields[i].getName().equals(name)) 255 { 256 return _fields[i]; 257 } 258 } 259 return (ObjectStreamField )null; 260 } 261 else if(type == java.lang.Object .class) 262 { 263 for(int i = 0; i < _fields.length; i++) 265 { 266 if(_fields[i].getName().equals(name) && !_fields[i].getType().isPrimitive()) 267 { 268 return _fields[i]; 269 } 270 } 271 return (ObjectStreamField )null; 272 } 273 else 274 { 275 for(int i = 0; i < _fields.length; i++) 276 { 277 if(_fields[i].getName().equals(name) && _fields[i].getType().equals(type)) 278 { 279 return _fields[i]; 280 } 281 } 282 return (ObjectStreamField )null; 283 } 284 } 285 286 private void computeOffsets() 287 { 288 try 289 { 290 computeFieldOffsets(); 291 } 292 catch(Exception e) 293 { 294 throw new RuntimeException (org.apache.geronimo.interop.util.ExceptionUtil.causedBy(e)); 295 } 296 } 297 298 private void computeFieldOffsets() throws Exception 299 { 300 primDataSize = 0; 301 numObjFields = 0; 302 int firstObjIndex = -1; 303 java.io.ObjectStreamField [] fields = desc.getFields(); 304 _fields = new ObjectStreamField [fields.length]; 305 Object [] args = new Object [1]; 306 307 for (int i = 0; i < fields.length; i++) 308 { 309 java.io.ObjectStreamField f = fields[i]; 310 _fields[i] = new ObjectStreamField (fields[i].getName(), fields[i].getType()); 311 ObjectStreamField _f = _fields[i]; 312 313 switch (f.getTypeCode()) 314 { 315 case 'Z': 316 case 'B': 317 args[0] = new Integer (primDataSize++); 318 setOffsetMethod.invoke(_f, args); 319 break; 320 321 case 'C': 322 case 'S': 323 args[0] = new Integer (primDataSize); 324 setOffsetMethod.invoke(_f, args); 325 primDataSize += 2; 326 break; 327 328 case 'I': 329 case 'F': 330 args[0] = new Integer (primDataSize); 331 setOffsetMethod.invoke(_f, args); 332 primDataSize += 4; 333 break; 334 335 case 'J': 336 case 'D': 337 args[0] = new Integer (primDataSize); 338 setOffsetMethod.invoke(_f, args); 339 primDataSize += 8; 340 break; 341 342 case '[': 343 case 'L': 344 args[0] = new Integer (numObjFields++); 345 setOffsetMethod.invoke(_f, args); 346 if (firstObjIndex == -1) 347 { 348 firstObjIndex = i; 349 } 350 break; 351 352 default: 353 break; 354 } 355 } 356 if (firstObjIndex != -1 && firstObjIndex + numObjFields != fields.length) 357 { 358 } 360 } 361 362 } 363 | Popular Tags |