src/share/classes/com/sun/corba/se/impl/io/OutputStreamHook.java

Thu, 31 Aug 2017 18:10:36 +0800

author
aoqi
date
Thu, 31 Aug 2017 18:10:36 +0800
changeset 748
6845b95cba6b
parent 553
5ca1b4c282b8
parent 0
7ef37b2cdcad
child 861
22d122be72c5
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25 /*
aoqi@0 26 * Licensed Materials - Property of IBM
aoqi@0 27 * RMI-IIOP v1.0
aoqi@0 28 * Copyright IBM Corp. 1998 1999 All Rights Reserved
aoqi@0 29 *
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 package com.sun.corba.se.impl.io;
aoqi@0 33
aoqi@0 34 import java.io.IOException;
aoqi@0 35 import java.io.OutputStream;
aoqi@0 36 import java.io.ObjectOutputStream;
aoqi@0 37 import java.io.ObjectOutput;
aoqi@0 38 import java.util.Hashtable;
aoqi@0 39
aoqi@0 40 import org.omg.CORBA.INTERNAL;
aoqi@0 41
aoqi@0 42 public abstract class OutputStreamHook extends ObjectOutputStream
aoqi@0 43 {
aoqi@0 44 private HookPutFields putFields = null;
aoqi@0 45
aoqi@0 46 /**
aoqi@0 47 * Since ObjectOutputStream.PutField methods specify no exceptions,
aoqi@0 48 * we are not checking for null parameters on put methods.
aoqi@0 49 */
aoqi@0 50 private class HookPutFields extends ObjectOutputStream.PutField
aoqi@0 51 {
aoqi@0 52 private Hashtable fields = new Hashtable();
aoqi@0 53
aoqi@0 54 /**
aoqi@0 55 * Put the value of the named boolean field into the persistent field.
aoqi@0 56 */
aoqi@0 57 public void put(String name, boolean value){
aoqi@0 58 fields.put(name, new Boolean(value));
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 /**
aoqi@0 62 * Put the value of the named char field into the persistent fields.
aoqi@0 63 */
aoqi@0 64 public void put(String name, char value){
aoqi@0 65 fields.put(name, new Character(value));
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 /**
aoqi@0 69 * Put the value of the named byte field into the persistent fields.
aoqi@0 70 */
aoqi@0 71 public void put(String name, byte value){
aoqi@0 72 fields.put(name, new Byte(value));
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 /**
aoqi@0 76 * Put the value of the named short field into the persistent fields.
aoqi@0 77 */
aoqi@0 78 public void put(String name, short value){
aoqi@0 79 fields.put(name, new Short(value));
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 /**
aoqi@0 83 * Put the value of the named int field into the persistent fields.
aoqi@0 84 */
aoqi@0 85 public void put(String name, int value){
aoqi@0 86 fields.put(name, new Integer(value));
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 /**
aoqi@0 90 * Put the value of the named long field into the persistent fields.
aoqi@0 91 */
aoqi@0 92 public void put(String name, long value){
aoqi@0 93 fields.put(name, new Long(value));
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 /**
aoqi@0 97 * Put the value of the named float field into the persistent fields.
aoqi@0 98 *
aoqi@0 99 */
aoqi@0 100 public void put(String name, float value){
aoqi@0 101 fields.put(name, new Float(value));
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 /**
aoqi@0 105 * Put the value of the named double field into the persistent field.
aoqi@0 106 */
aoqi@0 107 public void put(String name, double value){
aoqi@0 108 fields.put(name, new Double(value));
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 /**
aoqi@0 112 * Put the value of the named Object field into the persistent field.
aoqi@0 113 */
aoqi@0 114 public void put(String name, Object value){
aoqi@0 115 fields.put(name, value);
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 /**
aoqi@0 119 * Write the data and fields to the specified ObjectOutput stream.
aoqi@0 120 */
aoqi@0 121 public void write(ObjectOutput out) throws IOException {
aoqi@0 122 OutputStreamHook hook = (OutputStreamHook)out;
aoqi@0 123
aoqi@0 124 ObjectStreamField[] osfields = hook.getFieldsNoCopy();
aoqi@0 125
aoqi@0 126 // Write the fields to the stream in the order
aoqi@0 127 // provided by the ObjectStreamClass. (They should
aoqi@0 128 // be sorted appropriately already.)
aoqi@0 129 for (int i = 0; i < osfields.length; i++) {
aoqi@0 130
aoqi@0 131 Object value = fields.get(osfields[i].getName());
aoqi@0 132
aoqi@0 133 hook.writeField(osfields[i], value);
aoqi@0 134 }
aoqi@0 135 }
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 abstract void writeField(ObjectStreamField field, Object value) throws IOException;
aoqi@0 139
aoqi@0 140 public OutputStreamHook()
aoqi@0 141 throws java.io.IOException {
aoqi@0 142 super();
aoqi@0 143
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 public void defaultWriteObject() throws IOException {
aoqi@0 147
aoqi@0 148 writeObjectState.defaultWriteObject(this);
aoqi@0 149
aoqi@0 150 defaultWriteObjectDelegate();
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 public abstract void defaultWriteObjectDelegate();
aoqi@0 154
aoqi@0 155 public ObjectOutputStream.PutField putFields()
aoqi@0 156 throws IOException {
aoqi@0 157 putFields = new HookPutFields();
aoqi@0 158 return putFields;
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 // Stream format version, saved/restored during recursive calls
aoqi@0 162 protected byte streamFormatVersion = 1;
aoqi@0 163
aoqi@0 164 // Return the stream format version currently being used
aoqi@0 165 // to serialize an object
aoqi@0 166 public byte getStreamFormatVersion() {
aoqi@0 167 return streamFormatVersion;
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 abstract ObjectStreamField[] getFieldsNoCopy();
aoqi@0 171
aoqi@0 172 // User uses PutFields to simulate default data.
aoqi@0 173 // See java.io.ObjectOutputStream.PutFields
aoqi@0 174 public void writeFields()
aoqi@0 175 throws IOException {
aoqi@0 176
aoqi@0 177 writeObjectState.defaultWriteObject(this);
aoqi@0 178
aoqi@0 179 putFields.write(this);
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 abstract org.omg.CORBA_2_3.portable.OutputStream getOrbStream();
aoqi@0 183
aoqi@0 184 protected abstract void beginOptionalCustomData();
aoqi@0 185
aoqi@0 186
aoqi@0 187 // The following is a State pattern implementation of what
aoqi@0 188 // should be done when a Serializable has a
aoqi@0 189 // writeObject method. This was especially necessary for
aoqi@0 190 // RMI-IIOP stream format version 2. Please see the
aoqi@0 191 // state diagrams in the docs directory of the workspace.
aoqi@0 192
aoqi@0 193 protected WriteObjectState writeObjectState = NOT_IN_WRITE_OBJECT;
aoqi@0 194
aoqi@0 195 protected void setState(WriteObjectState newState) {
aoqi@0 196 writeObjectState = newState;
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 // Description of possible actions
aoqi@0 200 protected static class WriteObjectState {
aoqi@0 201 public void enterWriteObject(OutputStreamHook stream) throws IOException {}
aoqi@0 202 public void exitWriteObject(OutputStreamHook stream) throws IOException {}
aoqi@0 203 public void defaultWriteObject(OutputStreamHook stream) throws IOException {}
aoqi@0 204 public void writeData(OutputStreamHook stream) throws IOException {}
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 protected static class DefaultState extends WriteObjectState {
aoqi@0 208 public void enterWriteObject(OutputStreamHook stream) throws IOException {
aoqi@0 209 stream.setState(IN_WRITE_OBJECT);
aoqi@0 210 }
aoqi@0 211 }
aoqi@0 212
aoqi@0 213 protected static final WriteObjectState NOT_IN_WRITE_OBJECT = new DefaultState();
aoqi@0 214 protected static final WriteObjectState IN_WRITE_OBJECT = new InWriteObjectState();
aoqi@0 215 protected static final WriteObjectState WROTE_DEFAULT_DATA = new WroteDefaultDataState();
aoqi@0 216 protected static final WriteObjectState WROTE_CUSTOM_DATA = new WroteCustomDataState();
aoqi@0 217
aoqi@0 218 protected static class InWriteObjectState extends WriteObjectState {
aoqi@0 219
aoqi@0 220 public void enterWriteObject(OutputStreamHook stream) throws IOException {
aoqi@0 221 // XXX I18N, logging needed.
aoqi@0 222 throw new IOException("Internal state failure: Entered writeObject twice");
aoqi@0 223 }
aoqi@0 224
aoqi@0 225 public void exitWriteObject(OutputStreamHook stream) throws IOException {
aoqi@0 226
aoqi@0 227 // We didn't write any data, so write the
aoqi@0 228 // called defaultWriteObject indicator as false
aoqi@0 229 stream.getOrbStream().write_boolean(false);
aoqi@0 230
aoqi@0 231 // If we're in stream format verison 2, we must
aoqi@0 232 // put the "null" marker to say that there isn't
aoqi@0 233 // any optional data
aoqi@0 234 if (stream.getStreamFormatVersion() == 2)
aoqi@0 235 stream.getOrbStream().write_long(0);
aoqi@0 236
aoqi@0 237 stream.setState(NOT_IN_WRITE_OBJECT);
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 public void defaultWriteObject(OutputStreamHook stream) throws IOException {
aoqi@0 241
aoqi@0 242 // The writeObject method called defaultWriteObject
aoqi@0 243 // or writeFields, so put the called defaultWriteObject
aoqi@0 244 // indicator as true
aoqi@0 245 stream.getOrbStream().write_boolean(true);
aoqi@0 246
aoqi@0 247 stream.setState(WROTE_DEFAULT_DATA);
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 public void writeData(OutputStreamHook stream) throws IOException {
aoqi@0 251
aoqi@0 252 // The writeObject method first called a direct
aoqi@0 253 // write operation. Write the called defaultWriteObject
aoqi@0 254 // indicator as false, put the special stream format
aoqi@0 255 // version 2 header (if stream format version 2, of course),
aoqi@0 256 // and write the data
aoqi@0 257 stream.getOrbStream().write_boolean(false);
aoqi@0 258 stream.beginOptionalCustomData();
aoqi@0 259 stream.setState(WROTE_CUSTOM_DATA);
aoqi@0 260 }
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 protected static class WroteDefaultDataState extends InWriteObjectState {
aoqi@0 264
aoqi@0 265 public void exitWriteObject(OutputStreamHook stream) throws IOException {
aoqi@0 266
aoqi@0 267 // We only wrote default data, so if in stream format
aoqi@0 268 // version 2, put the null indicator to say that there
aoqi@0 269 // is no optional data
aoqi@0 270 if (stream.getStreamFormatVersion() == 2)
aoqi@0 271 stream.getOrbStream().write_long(0);
aoqi@0 272
aoqi@0 273 stream.setState(NOT_IN_WRITE_OBJECT);
aoqi@0 274 }
aoqi@0 275
aoqi@0 276 public void defaultWriteObject(OutputStreamHook stream) throws IOException {
aoqi@0 277 // XXX I18N, logging needed.
aoqi@0 278 throw new IOException("Called defaultWriteObject/writeFields twice");
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 public void writeData(OutputStreamHook stream) throws IOException {
aoqi@0 282
aoqi@0 283 // The writeObject method called a direct write operation.
aoqi@0 284 // If in stream format version 2, put the fake valuetype
aoqi@0 285 // header.
aoqi@0 286 stream.beginOptionalCustomData();
aoqi@0 287
aoqi@0 288 stream.setState(WROTE_CUSTOM_DATA);
aoqi@0 289 }
aoqi@0 290 }
aoqi@0 291
aoqi@0 292 protected static class WroteCustomDataState extends InWriteObjectState {
aoqi@0 293
aoqi@0 294 public void exitWriteObject(OutputStreamHook stream) throws IOException {
aoqi@0 295 // In stream format version 2, we must tell the ORB
aoqi@0 296 // stream to close the fake custom valuetype.
aoqi@0 297 if (stream.getStreamFormatVersion() == 2)
aoqi@0 298 ((org.omg.CORBA.portable.ValueOutputStream)stream.getOrbStream()).end_value();
aoqi@0 299
aoqi@0 300 stream.setState(NOT_IN_WRITE_OBJECT);
aoqi@0 301 }
aoqi@0 302
aoqi@0 303 public void defaultWriteObject(OutputStreamHook stream) throws IOException {
aoqi@0 304 // XXX I18N, logging needed.
aoqi@0 305 throw new IOException("Cannot call defaultWriteObject/writeFields after writing custom data in RMI-IIOP");
aoqi@0 306 }
aoqi@0 307
aoqi@0 308 // We don't have to do anything special here, just let
aoqi@0 309 // the stream write the data.
aoqi@0 310 public void writeData(OutputStreamHook stream) throws IOException {}
aoqi@0 311 }
aoqi@0 312 }

mercurial