Home » openjdk-7 » java » io » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package java.io;
   19   
   20   /**
   21    * An EmulatedFieldsForDumping is an object that represents a set of emulated
   22    * fields for an object being dumped. It is a concrete implementation for
   23    * ObjectOutputStream.PutField
   24    * 
   25    * 
   26    * @see ObjectOutputStream.PutField
   27    * @see EmulatedFieldsForLoading
   28    */
   29   class EmulatedFieldsForDumping extends ObjectOutputStream.PutField {
   30   
   31       // The actual representation, with a more powerful API (set&get)
   32       private EmulatedFields emulatedFields;
   33   
   34       /**
   35        * Constructs a new instance of EmulatedFieldsForDumping.
   36        * 
   37        * @param streamClass
   38        *            a ObjectStreamClass, which describe the fields to be emulated
   39        *            (names, types, etc).
   40        */
   41       EmulatedFieldsForDumping(ObjectStreamClass streamClass) {
   42           super();
   43           emulatedFields = new EmulatedFields(streamClass.fields(),
   44                   (ObjectStreamField[]) null);
   45       }
   46   
   47       /**
   48        * Return the actual EmulatedFields instance used by the receiver. We have
   49        * the actual work in a separate class so that the code can be shared. The
   50        * receiver has to be of a subclass of PutField.
   51        * 
   52        * @return array of ObjectSlot the receiver represents.
   53        */
   54       EmulatedFields emulatedFields() {
   55           return emulatedFields;
   56       }
   57   
   58       /**
   59        * Find and set the byte value of a given field named <code>name</code> in
   60        * the receiver.
   61        * 
   62        * @param name
   63        *            A String, the name of the field to set
   64        * @param value
   65        *            New value for the field.
   66        */
   67       @Override
   68       public void put(String name, byte value) {
   69           emulatedFields.put(name, value);
   70       }
   71   
   72       /**
   73        * Find and set the char value of a given field named <code>name</code> in
   74        * the receiver.
   75        * 
   76        * @param name
   77        *            A String, the name of the field to set
   78        * @param value
   79        *            New value for the field.
   80        */
   81       @Override
   82       public void put(String name, char value) {
   83           emulatedFields.put(name, value);
   84       }
   85   
   86       /**
   87        * Find and set the double value of a given field named <code>name</code>
   88        * in the receiver.
   89        * 
   90        * @param name
   91        *            A String, the name of the field to set
   92        * @param value
   93        *            New value for the field.
   94        */
   95       @Override
   96       public void put(String name, double value) {
   97           emulatedFields.put(name, value);
   98       }
   99   
  100       /**
  101        * Find and set the float value of a given field named <code>name</code>
  102        * in the receiver.
  103        * 
  104        * @param name
  105        *            A String, the name of the field to set
  106        * @param value
  107        *            New value for the field.
  108        */
  109       @Override
  110       public void put(String name, float value) {
  111           emulatedFields.put(name, value);
  112       }
  113   
  114       /**
  115        * Find and set the int value of a given field named <code>name</code> in
  116        * the receiver.
  117        * 
  118        * @param name
  119        *            A String, the name of the field to set
  120        * @param value
  121        *            New value for the field.
  122        */
  123       @Override
  124       public void put(String name, int value) {
  125           emulatedFields.put(name, value);
  126       }
  127   
  128       /**
  129        * Find and set the long value of a given field named <code>name</code> in
  130        * the receiver.
  131        * 
  132        * @param name
  133        *            A String, the name of the field to set
  134        * @param value
  135        *            New value for the field.
  136        */
  137       @Override
  138       public void put(String name, long value) {
  139           emulatedFields.put(name, value);
  140       }
  141   
  142       /**
  143        * Find and set the Object value of a given field named <code>name</code>
  144        * in the receiver.
  145        * 
  146        * @param name
  147        *            A String, the name of the field to set
  148        * @param value
  149        *            New value for the field.
  150        */
  151       @Override
  152       public void put(String name, Object value) {
  153           emulatedFields.put(name, value);
  154       }
  155   
  156       /**
  157        * Find and set the short value of a given field named <code>name</code>
  158        * in the receiver.
  159        * 
  160        * @param name
  161        *            A String, the name of the field to set
  162        * @param value
  163        *            New value for the field.
  164        */
  165       @Override
  166       public void put(String name, short value) {
  167           emulatedFields.put(name, value);
  168       }
  169   
  170       /**
  171        * Find and set the boolean value of a given field named <code>name</code>
  172        * in the receiver.
  173        * 
  174        * @param name
  175        *            A String, the name of the field to set
  176        * @param value
  177        *            New value for the field.
  178        */
  179       @Override
  180       public void put(String name, boolean value) {
  181           emulatedFields.put(name, value);
  182       }
  183   
  184       /**
  185        * Write the field values to the specified ObjectOutput.
  186        * 
  187        * @param output
  188        *            the ObjectOutput
  189        * 
  190        * @throws IOException
  191        *             If an IO exception happened when writing the field values.
  192        */
  193       @Override
  194       @Deprecated
  195       public void write(ObjectOutput output) throws IOException {
  196           EmulatedFields.ObjectSlot[] slots = emulatedFields.slots();
  197           for (int i = 0; i < slots.length; i++) {
  198               EmulatedFields.ObjectSlot slot = slots[i];
  199               Object fieldValue = slot.getFieldValue();
  200               Class<?> type = slot.getField().getType();
  201               // WARNING - default values exist for each primitive type
  202               if (type == Integer.TYPE) {
  203                   output.writeInt(fieldValue != null ? ((Integer) fieldValue)
  204                           .intValue() : 0);
  205               } else if (type == Byte.TYPE) {
  206                   output.writeByte(fieldValue != null ? ((Byte) fieldValue)
  207                           .byteValue() : (byte) 0);
  208               } else if (type == Character.TYPE) {
  209                   output.writeChar(fieldValue != null ? ((Character) fieldValue)
  210                           .charValue() : (char) 0);
  211               } else if (type == Short.TYPE) {
  212                   output.writeShort(fieldValue != null ? ((Short) fieldValue)
  213                           .shortValue() : (short) 0);
  214               } else if (type == Boolean.TYPE) {
  215                   output.writeBoolean(fieldValue != null ? ((Boolean) fieldValue)
  216                           .booleanValue() : false);
  217               } else if (type == Long.TYPE) {
  218                   output.writeLong(fieldValue != null ? ((Long) fieldValue)
  219                           .longValue() : (long) 0);
  220               } else if (type == Float.TYPE) {
  221                   output.writeFloat(fieldValue != null ? ((Float) fieldValue)
  222                           .floatValue() : (float) 0);
  223               } else if (type == Double.TYPE) {
  224                   output.writeDouble(fieldValue != null ? ((Double) fieldValue)
  225                           .doubleValue() : (double) 0);
  226               } else {
  227                   // Either array or Object
  228                   output.writeObject(fieldValue);
  229               }
  230           }
  231       }
  232   }

Home » openjdk-7 » java » io » [javadoc | source]