1 7 15 16 package com.sun.corba.se.impl.io; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.io.ObjectOutput ; 22 import java.util.Hashtable ; 23 24 import org.omg.CORBA.INTERNAL ; 25 26 public abstract class OutputStreamHook extends ObjectOutputStream 27 { 28 private HookPutFields putFields = null; 29 30 34 private class HookPutFields extends ObjectOutputStream.PutField 35 { 36 private Hashtable fields = new Hashtable (); 37 38 41 public void put(String name, boolean value){ 42 fields.put(name, new Boolean (value)); 43 } 44 45 48 public void put(String name, char value){ 49 fields.put(name, new Character (value)); 50 } 51 52 55 public void put(String name, byte value){ 56 fields.put(name, new Byte (value)); 57 } 58 59 62 public void put(String name, short value){ 63 fields.put(name, new Short (value)); 64 } 65 66 69 public void put(String name, int value){ 70 fields.put(name, new Integer (value)); 71 } 72 73 76 public void put(String name, long value){ 77 fields.put(name, new Long (value)); 78 } 79 80 84 public void put(String name, float value){ 85 fields.put(name, new Float (value)); 86 } 87 88 91 public void put(String name, double value){ 92 fields.put(name, new Double (value)); 93 } 94 95 98 public void put(String name, Object value){ 99 fields.put(name, value); 100 } 101 102 105 public void write(ObjectOutput out) throws IOException { 106 OutputStreamHook hook = (OutputStreamHook)out; 107 108 ObjectStreamField[] osfields = hook.getFieldsNoCopy(); 109 110 for (int i = 0; i < osfields.length; i++) { 114 115 Object value = fields.get(osfields[i].getName()); 116 117 hook.writeField(osfields[i], value); 118 } 119 } 120 } 121 122 abstract void writeField(ObjectStreamField field, Object value) throws IOException ; 123 124 public OutputStreamHook() 125 throws java.io.IOException { 126 super(); 127 128 } 129 130 public void defaultWriteObject() throws IOException { 131 132 writeObjectState.defaultWriteObject(this); 133 134 defaultWriteObjectDelegate(); 135 } 136 137 public abstract void defaultWriteObjectDelegate(); 138 139 public ObjectOutputStream.PutField putFields() 140 throws IOException { 141 putFields = new HookPutFields(); 142 return putFields; 143 } 144 145 protected byte streamFormatVersion = 1; 147 148 public byte getStreamFormatVersion() { 151 return streamFormatVersion; 152 } 153 154 abstract ObjectStreamField[] getFieldsNoCopy(); 155 156 public void writeFields() 159 throws IOException { 160 161 writeObjectState.defaultWriteObject(this); 162 163 putFields.write(this); 164 } 165 166 public abstract org.omg.CORBA_2_3.portable.OutputStream getOrbStream(); 167 168 protected abstract void beginOptionalCustomData(); 169 170 171 177 protected WriteObjectState writeObjectState = NOT_IN_WRITE_OBJECT; 178 179 protected void setState(WriteObjectState newState) { 180 writeObjectState = newState; 181 } 182 183 protected static class WriteObjectState { 185 public void enterWriteObject(OutputStreamHook stream) throws IOException {} 186 public void exitWriteObject(OutputStreamHook stream) throws IOException {} 187 public void defaultWriteObject(OutputStreamHook stream) throws IOException {} 188 public void writeData(OutputStreamHook stream) throws IOException {} 189 } 190 191 protected static class DefaultState extends WriteObjectState { 192 public void enterWriteObject(OutputStreamHook stream) throws IOException { 193 stream.setState(IN_WRITE_OBJECT); 194 } 195 } 196 197 protected static final WriteObjectState NOT_IN_WRITE_OBJECT = new DefaultState(); 198 protected static final WriteObjectState IN_WRITE_OBJECT = new InWriteObjectState(); 199 protected static final WriteObjectState WROTE_DEFAULT_DATA = new WroteDefaultDataState(); 200 protected static final WriteObjectState WROTE_CUSTOM_DATA = new WroteCustomDataState(); 201 202 protected static class InWriteObjectState extends WriteObjectState { 203 204 public void enterWriteObject(OutputStreamHook stream) throws IOException { 205 throw new IOException ("Internal state failure: Entered writeObject twice"); 207 } 208 209 public void exitWriteObject(OutputStreamHook stream) throws IOException { 210 211 stream.getOrbStream().write_boolean(false); 214 215 if (stream.getStreamFormatVersion() == 2) 219 stream.getOrbStream().write_long(0); 220 221 stream.setState(NOT_IN_WRITE_OBJECT); 222 } 223 224 public void defaultWriteObject(OutputStreamHook stream) throws IOException { 225 226 stream.getOrbStream().write_boolean(true); 230 231 stream.setState(WROTE_DEFAULT_DATA); 232 } 233 234 public void writeData(OutputStreamHook stream) throws IOException { 235 236 stream.getOrbStream().write_boolean(false); 242 stream.beginOptionalCustomData(); 243 stream.setState(WROTE_CUSTOM_DATA); 244 } 245 } 246 247 protected static class WroteDefaultDataState extends InWriteObjectState { 248 249 public void exitWriteObject(OutputStreamHook stream) throws IOException { 250 251 if (stream.getStreamFormatVersion() == 2) 255 stream.getOrbStream().write_long(0); 256 257 stream.setState(NOT_IN_WRITE_OBJECT); 258 } 259 260 public void defaultWriteObject(OutputStreamHook stream) throws IOException { 261 throw new IOException ("Called defaultWriteObject/writeFields twice"); 263 } 264 265 public void writeData(OutputStreamHook stream) throws IOException { 266 267 stream.beginOptionalCustomData(); 271 272 stream.setState(WROTE_CUSTOM_DATA); 273 } 274 } 275 276 protected static class WroteCustomDataState extends InWriteObjectState { 277 278 public void exitWriteObject(OutputStreamHook stream) throws IOException { 279 if (stream.getStreamFormatVersion() == 2) 282 ((org.omg.CORBA.portable.ValueOutputStream )stream.getOrbStream()).end_value(); 283 284 stream.setState(NOT_IN_WRITE_OBJECT); 285 } 286 287 public void defaultWriteObject(OutputStreamHook stream) throws IOException { 288 throw new IOException ("Cannot call defaultWriteObject/writeFields after writing custom data in RMI-IIOP"); 290 } 291 292 public void writeData(OutputStreamHook stream) throws IOException {} 295 } 296 } 297 | Popular Tags |