jdk/src/share/classes/java/io/ObjectOutputStream.java
author smarks
Mon, 20 Dec 2010 13:47:04 -0800
changeset 7803 56bc97d69d93
parent 7030 90cf66131063
child 11117 b6e68b1344d4
permissions -rw-r--r--
6880112: Project Coin: Port JDK core library code to use diamond operator Reviewed-by: darcy, lancea, alanb, briangoetz, mduigou, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ObjectStreamClass.WeakClassKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.ConcurrentMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import static java.io.ObjectStreamClass.processQueue;
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
    38
import java.io.SerialCallbackContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * An ObjectOutputStream writes primitive data types and graphs of Java objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * to an OutputStream.  The objects can be read (reconstituted) using an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * ObjectInputStream.  Persistent storage of objects can be accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * using a file for the stream.  If the stream is a network socket stream, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * objects can be reconstituted on another host or in another process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>Only objects that support the java.io.Serializable interface can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * written to streams.  The class of each serializable object is encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * including the class name and signature of the class, the values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * object's fields and arrays, and the closure of any other objects referenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * from the initial objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>The method writeObject is used to write an object to the stream.  Any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * object, including Strings and arrays, is written with writeObject. Multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * objects or primitives can be written to the stream.  The objects must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * read back from the corresponding ObjectInputstream with the same types and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * in the same order as they were written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>Primitive data types can also be written to the stream using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * appropriate methods from DataOutput. Strings can also be written using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * writeUTF method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>The default serialization mechanism for an object writes the class of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * object, the class signature, and the values of all non-transient and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * non-static fields.  References to other objects (except in transient or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * static fields) cause those objects to be written also. Multiple references
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * to a single object are encoded using a reference sharing mechanism so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * graphs of objects can be restored to the same shape as when the original was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>For example to write an object that can be read by the example in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * ObjectInputStream:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      FileOutputStream fos = new FileOutputStream("t.tmp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *      ObjectOutputStream oos = new ObjectOutputStream(fos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      oos.writeInt(12345);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *      oos.writeObject("Today");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      oos.writeObject(new Date());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *      oos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p>Classes that require special handling during the serialization and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * deserialization process must implement special methods with these exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * signatures:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * private void readObject(java.io.ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *     throws IOException, ClassNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * private void writeObject(java.io.ObjectOutputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *     throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * private void readObjectNoData()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *     throws ObjectStreamException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <p>The writeObject method is responsible for writing the state of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * for its particular class so that the corresponding readObject method can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * restore it.  The method does not need to concern itself with the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * belonging to the object's superclasses or subclasses.  State is saved by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * writing the individual fields to the ObjectOutputStream using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * writeObject method or by using the methods for primitive data types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * supported by DataOutput.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <p>Serialization does not write out the fields of any object that does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * implement the java.io.Serializable interface.  Subclasses of Objects that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * are not serializable can be serializable. In this case the non-serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * class must have a no-arg constructor to allow its fields to be initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * In this case it is the responsibility of the subclass to save and restore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * the state of the non-serializable class. It is frequently the case that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * fields of that class are accessible (public, package, or protected) or that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * there are get and set methods that can be used to restore the state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <p>Serialization of an object can be prevented by implementing writeObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * and readObject methods that throw the NotSerializableException.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * exception will be caught by the ObjectOutputStream and abort the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * serialization process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <p>Implementing the Externalizable interface allows the object to assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * complete control over the contents and format of the object's serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * form.  The methods of the Externalizable interface, writeExternal and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * readExternal, are called to save and restore the objects state.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * implemented by a class they can write and read their own state using all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * the methods of ObjectOutput and ObjectInput.  It is the responsibility of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * the objects to handle any versioning that occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <p>Enum constants are serialized differently than ordinary serializable or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * externalizable objects.  The serialized form of an enum constant consists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * solely of its name; field values of the constant are not transmitted.  To
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * serialize an enum constant, ObjectOutputStream writes the string returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * the constant's name method.  Like other serializable or externalizable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * objects, enum constants can function as the targets of back references
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * appearing subsequently in the serialization stream.  The process by which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * enum constants are serialized cannot be customized; any class-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * writeObject and writeReplace methods defined by enum types are ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * during serialization.  Similarly, any serialPersistentFields or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * serialVersionUID field declarations are also ignored--all enum types have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * fixed serialVersionUID of 0L.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <p>Primitive data, excluding serializable fields and externalizable data, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * written to the ObjectOutputStream in block-data records. A block data record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * is composed of a header and data. The block data header consists of a marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * and the number of bytes to follow the header.  Consecutive primitive data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * writes are merged into one block-data record.  The blocking factor used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * a block-data record will be 1024 bytes.  Each block-data record will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * filled up to 1024 bytes, or be written whenever there is a termination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * block-data mode.  Calls to the ObjectOutputStream methods writeObject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * defaultWriteObject and writeFields initially terminate any existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * block-data record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * @author      Mike Warres
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * @author      Roger Riggs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * @see java.io.DataOutput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * @see java.io.ObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * @see java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * @see java.io.Externalizable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * @see <a href="../../../platform/serialization/spec/output.html">Object Serialization Specification, Section 2, Object Output Classes</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
public class ObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    extends OutputStream implements ObjectOutput, ObjectStreamConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static class Caches {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        /** cache of subclass security audit results */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7030
diff changeset
   168
            new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        /** queue for WeakReferences to audited subclasses */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        static final ReferenceQueue<Class<?>> subclassAuditsQueue =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7030
diff changeset
   172
            new ReferenceQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /** filter stream for handling block data conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private final BlockDataOutputStream bout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /** obj -> wire handle map */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private final HandleTable handles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /** obj -> replacement obj map */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private final ReplaceTable subs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /** stream protocol version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private int protocol = PROTOCOL_VERSION_2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /** recursion depth */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private int depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /** buffer for writing primitive field values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private byte[] primVals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /** if true, invoke writeObjectOverride() instead of writeObject() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private final boolean enableOverride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /** if true, invoke replaceObject() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private boolean enableReplace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    // values below valid only during upcalls to writeObject()/writeExternal()
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   195
    /**
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   196
     * Context during upcalls to class-defined writeObject methods; holds
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   197
     * object currently being serialized and descriptor for current class.
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   198
     * Null when not during writeObject upcall.
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   199
     */
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   200
    private SerialCallbackContext curContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /** current PutField object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private PutFieldImpl curPut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /** custom storage for debug trace info */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private final DebugTraceInfoStack debugInfoStack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * value of "sun.io.serialization.extendedDebugInfo" property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * as true or false for extended information about exception's place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private static final boolean extendedDebugInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            new sun.security.action.GetBooleanAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                "sun.io.serialization.extendedDebugInfo")).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Creates an ObjectOutputStream that writes to the specified OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * This constructor writes the serialization stream header to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * underlying stream; callers may wish to flush the stream immediately to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * ensure that constructors for receiving ObjectInputStreams will not block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * when reading the header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <p>If a security manager is installed, this constructor will check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * the "enableSubclassImplementation" SerializablePermission when invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * directly or indirectly by the constructor of a subclass which overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * the ObjectOutputStream.putFields or ObjectOutputStream.writeUnshared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param   out output stream to write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @throws  IOException if an I/O error occurs while writing stream header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @throws  SecurityException if untrusted subclass illegally overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *          security-sensitive methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @throws  NullPointerException if <code>out</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @see     ObjectOutputStream#ObjectOutputStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @see     ObjectOutputStream#putFields()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @see     ObjectInputStream#ObjectInputStream(InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public ObjectOutputStream(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        verifySubclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        bout = new BlockDataOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        handles = new HandleTable(10, (float) 3.00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        subs = new ReplaceTable(10, (float) 3.00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        enableOverride = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        writeStreamHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        bout.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            debugInfoStack = new DebugTraceInfoStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            debugInfoStack = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Provide a way for subclasses that are completely reimplementing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * ObjectOutputStream to not have to allocate private data just used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * this implementation of ObjectOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <p>If there is a security manager installed, this method first calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * security manager's <code>checkPermission</code> method with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <code>SerializablePermission("enableSubclassImplementation")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * permission to ensure it's ok to enable subclassing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @throws  SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *          <code>checkPermission</code> method denies enabling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *          subclassing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @see java.io.SerializablePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    protected ObjectOutputStream() throws IOException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        bout = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        handles = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        subs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        enableOverride = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        debugInfoStack = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Specify stream protocol version to use when writing the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <p>This routine provides a hook to enable the current version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Serialization to write in a format that is backwards compatible to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * previous version of the stream format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>Every effort will be made to avoid introducing additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * backwards incompatibilities; however, sometimes there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * other alternative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param   version use ProtocolVersion from java.io.ObjectStreamConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @throws  IllegalStateException if called after any objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *          have been serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @throws  IllegalArgumentException if invalid version is passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @throws  IOException if I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @see java.io.ObjectStreamConstants#PROTOCOL_VERSION_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @see java.io.ObjectStreamConstants#PROTOCOL_VERSION_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public void useProtocolVersion(int version) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (handles.size() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // REMIND: implement better check for pristine stream?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            throw new IllegalStateException("stream non-empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        switch (version) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            case PROTOCOL_VERSION_1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            case PROTOCOL_VERSION_2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                protocol = version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    "unknown version: " + version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Write the specified object to the ObjectOutputStream.  The class of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * object, the signature of the class, and the values of the non-transient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * and non-static fields of the class and all of its supertypes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * written.  Default serialization for a class can be overridden using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * writeObject and the readObject methods.  Objects referenced by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * object are written transitively so that a complete equivalent graph of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * objects can be reconstructed by an ObjectInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * <p>Exceptions are thrown for problems with the OutputStream and for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * classes that should not be serialized.  All exceptions are fatal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * OutputStream, which is left in an indeterminate state, and it is up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * the caller to ignore or recover the stream state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @throws  InvalidClassException Something is wrong with a class used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *          serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @throws  NotSerializableException Some object to be serialized does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *          implement the java.io.Serializable interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws  IOException Any exception thrown by the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *          OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public final void writeObject(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (enableOverride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            writeObjectOverride(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            writeObject0(obj, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (depth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                writeFatalException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Method used by subclasses to override the default writeObject method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * This method is called by trusted subclasses of ObjectInputStream that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * constructed ObjectInputStream using the protected no-arg constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * The subclass is expected to provide an override method with the modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * "final".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @param   obj object to be written to the underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws  IOException if there are I/O errors while writing to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *          underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @see #ObjectOutputStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @see #writeObject(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    protected void writeObjectOverride(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Writes an "unshared" object to the ObjectOutputStream.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * identical to writeObject, except that it always writes the given object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * as a new, unique object in the stream (as opposed to a back-reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * pointing to a previously serialized instance).  Specifically:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *   <li>An object written via writeUnshared is always serialized in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *       same manner as a newly appearing object (an object that has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *       been written to the stream yet), regardless of whether or not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *       object has been written previously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *   <li>If writeObject is used to write an object that has been previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *       written with writeUnshared, the previous writeUnshared operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *       is treated as if it were a write of a separate object.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *       words, ObjectOutputStream will never generate back-references to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *       object data written by calls to writeUnshared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * While writing an object via writeUnshared does not in itself guarantee a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * unique reference to the object when it is deserialized, it allows a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * single object to be defined multiple times in a stream, so that multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * calls to readUnshared by the receiver will not conflict.  Note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * rules described above only apply to the base-level object written with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * writeUnshared, and not to any transitively referenced sub-objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * object graph to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <p>ObjectOutputStream subclasses which override this method can only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * constructed in security contexts possessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * "enableSubclassImplementation" SerializablePermission; any attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * instantiate such a subclass without this permission will cause a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * SecurityException to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param   obj object to write to stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @throws  NotSerializableException if an object in the graph to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *          serialized does not implement the Serializable interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @throws  InvalidClassException if a problem exists with the class of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *          object to be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @throws  IOException if an I/O error occurs during serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public void writeUnshared(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            writeObject0(obj, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (depth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                writeFatalException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Write the non-static and non-transient fields of the current class to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * this stream.  This may only be called from the writeObject method of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * class being serialized. It will throw the NotActiveException if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * called otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *          <code>OutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public void defaultWriteObject() throws IOException {
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   432
        if ( curContext == null ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            throw new NotActiveException("not in call to writeObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   435
        Object curObj = curContext.getObj();
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   436
        ObjectStreamClass curDesc = curContext.getDesc();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        bout.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        defaultWriteFields(curObj, curDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        bout.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Retrieve the object used to buffer persistent fields to be written to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * the stream.  The fields will be written to the stream when writeFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return  an instance of the class Putfield that holds the serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *          fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @throws  IOException if I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    public ObjectOutputStream.PutField putFields() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (curPut == null) {
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   454
            if (curContext == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                throw new NotActiveException("not in call to writeObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   457
            Object curObj = curContext.getObj();
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   458
            ObjectStreamClass curDesc = curContext.getDesc();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            curPut = new PutFieldImpl(curDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return curPut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Write the buffered fields to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @throws  NotActiveException Called when a classes writeObject method was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *          not called to write the state of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public void writeFields() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (curPut == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            throw new NotActiveException("no current PutField object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        bout.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        curPut.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        bout.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Reset will disregard the state of any objects already written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * stream.  The state is reset to be the same as a new ObjectOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * The current point in the stream is marked as reset so the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * ObjectInputStream will be reset at the same point.  Objects previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * written to the stream will not be refered to as already being in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * stream.  They will be written to the stream again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @throws  IOException if reset() is invoked while serializing an object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (depth != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            throw new IOException("stream active");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        bout.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        bout.writeByte(TC_RESET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        bout.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * Subclasses may implement this method to allow class data to be stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * the stream. By default this method does nothing.  The corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * method in ObjectInputStream is resolveClass.  This method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * exactly once for each unique class in the stream.  The class name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * signature will have already been written to the stream.  This method may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * make free use of the ObjectOutputStream to save any representation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * the class it deems suitable (for example, the bytes of the class file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * The resolveClass method in the corresponding subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * ObjectInputStream must read and use any data or objects written by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * annotateClass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @param   cl the class to annotate custom data for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @throws  IOException Any exception thrown by the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *          OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    protected void annotateClass(Class<?> cl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * Subclasses may implement this method to store custom data in the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * along with descriptors for dynamic proxy classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * <p>This method is called exactly once for each unique proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * descriptor in the stream.  The default implementation of this method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * <code>ObjectOutputStream</code> does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * <p>The corresponding method in <code>ObjectInputStream</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * <code>resolveProxyClass</code>.  For a given subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <code>ObjectOutputStream</code> that overrides this method, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * <code>resolveProxyClass</code> method in the corresponding subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * <code>ObjectInputStream</code> must read any data or objects written by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <code>annotateProxyClass</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param   cl the proxy class to annotate custom data for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @throws  IOException any exception thrown by the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *          <code>OutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @see ObjectInputStream#resolveProxyClass(String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    protected void annotateProxyClass(Class<?> cl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * This method will allow trusted subclasses of ObjectOutputStream to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * substitute one object for another during serialization. Replacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * objects is disabled until enableReplaceObject is called. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * enableReplaceObject method checks that the stream requesting to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * replacement can be trusted.  The first occurrence of each object written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * into the serialization stream is passed to replaceObject.  Subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * references to the object are replaced by the object returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * original call to replaceObject.  To ensure that the private state of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * objects is not unintentionally exposed, only trusted streams may use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * replaceObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * <p>The ObjectOutputStream.writeObject method takes a parameter of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Object (as opposed to type Serializable) to allow for cases where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * non-serializable objects are replaced by serializable ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * <p>When a subclass is replacing objects it must insure that either a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * complementary substitution must be made during deserialization or that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * the substituted object is compatible with every field where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * reference will be stored.  Objects whose type is not a subclass of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * type of the field or array element abort the serialization by raising an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * exception and the object is not be stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * <p>This method is called only once when each object is first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * encountered.  All subsequent references to the object will be redirected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * to the new object. This method should return the object to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * substituted or the original object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <p>Null can be returned as the object to be substituted, but may cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * NullReferenceException in classes that contain references to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * original object since they may be expecting an object instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @param   obj the object to be replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @return  the alternate object that replaced the specified one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @throws  IOException Any exception thrown by the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *          OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    protected Object replaceObject(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * Enable the stream to do replacement of objects in the stream.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * enabled, the replaceObject method is called for every object being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * <p>If <code>enable</code> is true, and there is a security manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * installed, this method first calls the security manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * <code>checkPermission</code> method with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * <code>SerializablePermission("enableSubstitution")</code> permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * ensure it's ok to enable the stream to do replacement of objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param   enable boolean parameter to enable replacement of objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @return  the previous setting before this method was invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @throws  SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *          <code>checkPermission</code> method denies enabling the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *          to do replacement of objects in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @see java.io.SerializablePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    protected boolean enableReplaceObject(boolean enable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (enable == enableReplace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            return enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                sm.checkPermission(SUBSTITUTION_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        enableReplace = enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return !enableReplace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * The writeStreamHeader method is provided so subclasses can append or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * prepend their own header to the stream.  It writes the magic number and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * version to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    protected void writeStreamHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        bout.writeShort(STREAM_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        bout.writeShort(STREAM_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * Write the specified class descriptor to the ObjectOutputStream.  Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * descriptors are used to identify the classes of objects written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * stream.  Subclasses of ObjectOutputStream may override this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * customize the way in which class descriptors are written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * serialization stream.  The corresponding method in ObjectInputStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * <code>readClassDescriptor</code>, should then be overridden to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * reconstitute the class descriptor from its custom stream representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * By default, this method writes class descriptors according to the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * defined in the Object Serialization specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <p>Note that this method will only be called if the ObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * is not using the old serialization stream format (set by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * ObjectOutputStream's <code>useProtocolVersion</code> method).  If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * serialization stream is using the old format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * (<code>PROTOCOL_VERSION_1</code>), the class descriptor will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * internally in a manner that cannot be overridden or customized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @param   desc class descriptor to write to the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @throws  IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @see java.io.ObjectInputStream#readClassDescriptor()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @see #useProtocolVersion(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @see java.io.ObjectStreamConstants#PROTOCOL_VERSION_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    protected void writeClassDescriptor(ObjectStreamClass desc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        desc.writeNonProxy(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Writes a byte. This method will block until the byte is actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @param   val the byte to be written to the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @throws  IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public void write(int val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        bout.write(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * Writes an array of bytes. This method will block until the bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * actually written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @param   buf the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @throws  IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    public void write(byte[] buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        bout.write(buf, 0, buf.length, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * Writes a sub array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @param   buf the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @param   off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @param   len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @throws  IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    public void write(byte[] buf, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        int endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        bout.write(buf, off, len, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * Flushes the stream. This will write any buffered output bytes and flush
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * through to the underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @throws  IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        bout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Drain any buffered data in ObjectOutputStream.  Similar to flush but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * does not propagate the flush to the underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    protected void drain() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        bout.drain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Closes the stream. This method must be called to release any resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @throws  IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        bout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * Writes a boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @param   val the boolean to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    public void writeBoolean(boolean val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        bout.writeBoolean(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * Writes an 8 bit byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @param   val the byte value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    public void writeByte(int val) throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        bout.writeByte(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * Writes a 16 bit short.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @param   val the short value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    public void writeShort(int val)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        bout.writeShort(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Writes a 16 bit char.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @param   val the char value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    public void writeChar(int val)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        bout.writeChar(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * Writes a 32 bit int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @param   val the integer value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    public void writeInt(int val)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        bout.writeInt(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * Writes a 64 bit long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @param   val the long value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    public void writeLong(long val)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        bout.writeLong(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * Writes a 32 bit float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @param   val the float value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    public void writeFloat(float val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        bout.writeFloat(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * Writes a 64 bit double.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @param   val the double value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    public void writeDouble(double val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        bout.writeDouble(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * Writes a String as a sequence of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @param   str the String of bytes to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    public void writeBytes(String str) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        bout.writeBytes(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * Writes a String as a sequence of chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @param   str the String of chars to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    public void writeChars(String str) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        bout.writeChars(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * Primitive data write of this String in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * format.  Note that there is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * significant difference between writing a String into the stream as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * primitive data or as an Object. A String instance written by writeObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * is written into the stream as a String initially. Future writeObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * calls write references to the string into the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @param   str the String to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @throws  IOException if I/O errors occur while writing to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *          stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    public void writeUTF(String str) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        bout.writeUTF(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * Provide programmatic access to the persistent fields to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * to ObjectOutput.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public static abstract class PutField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
         * Put the value of the named boolean field into the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
         * @param  name the name of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
         * @param  val the value to assign to the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
         * @throws IllegalArgumentException if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
         * match the name of a serializable field for the class whose fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
         * are being written, or if the type of the named field is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
         * <code>boolean</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        public abstract void put(String name, boolean val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
         * Put the value of the named byte field into the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         * @param  name the name of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         * @param  val the value to assign to the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         * @throws IllegalArgumentException if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
         * match the name of a serializable field for the class whose fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
         * are being written, or if the type of the named field is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
         * <code>byte</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        public abstract void put(String name, byte val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
         * Put the value of the named char field into the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
         * @param  name the name of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
         * @param  val the value to assign to the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
         * @throws IllegalArgumentException if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
         * match the name of a serializable field for the class whose fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
         * are being written, or if the type of the named field is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
         * <code>char</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        public abstract void put(String name, char val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
         * Put the value of the named short field into the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
         * @param  name the name of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
         * @param  val the value to assign to the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
         * @throws IllegalArgumentException if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
         * match the name of a serializable field for the class whose fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
         * are being written, or if the type of the named field is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
         * <code>short</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        public abstract void put(String name, short val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
         * Put the value of the named int field into the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
         * @param  name the name of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         * @param  val the value to assign to the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         * @throws IllegalArgumentException if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
         * match the name of a serializable field for the class whose fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
         * are being written, or if the type of the named field is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
         * <code>int</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        public abstract void put(String name, int val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
         * Put the value of the named long field into the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
         * @param  name the name of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
         * @param  val the value to assign to the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
         * @throws IllegalArgumentException if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
         * match the name of a serializable field for the class whose fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
         * are being written, or if the type of the named field is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
         * <code>long</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        public abstract void put(String name, long val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
         * Put the value of the named float field into the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
         * @param  name the name of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
         * @param  val the value to assign to the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
         * @throws IllegalArgumentException if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
         * match the name of a serializable field for the class whose fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
         * are being written, or if the type of the named field is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
         * <code>float</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        public abstract void put(String name, float val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
         * Put the value of the named double field into the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         * @param  name the name of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         * @param  val the value to assign to the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
         * @throws IllegalArgumentException if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         * match the name of a serializable field for the class whose fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * are being written, or if the type of the named field is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
         * <code>double</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        public abstract void put(String name, double val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
         * Put the value of the named Object field into the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
         * @param  name the name of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
         * @param  val the value to assign to the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
         *         (which may be <code>null</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
         * @throws IllegalArgumentException if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
         * match the name of a serializable field for the class whose fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
         * are being written, or if the type of the named field is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
         * reference type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        public abstract void put(String name, Object val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
         * Write the data and fields to the specified ObjectOutput stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         * which must be the same stream that produced this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         * <code>PutField</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         * @param  out the stream to write the data and fields to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
         * @throws IOException if I/O errors occur while writing to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
         *         underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
         * @throws IllegalArgumentException if the specified stream is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
         *         the same stream that produced this <code>PutField</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
         *         object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
         * @deprecated This method does not write the values contained by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
         *         <code>PutField</code> object in a proper format, and may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
         *         result in corruption of the serialization stream.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
         *         correct way to write <code>PutField</code> data is by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         *         calling the {@link java.io.ObjectOutputStream#writeFields()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         *         method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        public abstract void write(ObjectOutput out) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * Returns protocol version in use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    int getProtocolVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        return protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * Writes string without allowing it to be replaced in stream.  Used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * ObjectStreamClass to write class descriptor type strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    void writeTypeString(String str) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        int handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        if (str == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            writeNull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        } else if ((handle = handles.lookup(str)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            writeHandle(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            writeString(str, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * Verifies that this (possibly subclass) instance can be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * without violating security constraints: the subclass must not override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * security-sensitive non-final methods, or else the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * "enableSubclassImplementation" SerializablePermission is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    private void verifySubclass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        Class cl = getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        if (cl == ObjectOutputStream.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        processQueue(Caches.subclassAuditsQueue, Caches.subclassAudits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        WeakClassKey key = new WeakClassKey(cl, Caches.subclassAuditsQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        Boolean result = Caches.subclassAudits.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            result = Boolean.valueOf(auditSubclass(cl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            Caches.subclassAudits.putIfAbsent(key, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        if (result.booleanValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * Performs reflective checks on given subclass to verify that it doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * override security-sensitive non-final methods.  Returns true if subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * is "safe", false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    private static boolean auditSubclass(final Class subcl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        Boolean result = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            new PrivilegedAction<Boolean>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                public Boolean run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                    for (Class cl = subcl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                         cl != ObjectOutputStream.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                         cl = cl.getSuperclass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                            cl.getDeclaredMethod(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                                "writeUnshared", new Class[] { Object.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                            return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                        } catch (NoSuchMethodException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                            cl.getDeclaredMethod("putFields", (Class[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                            return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                        } catch (NoSuchMethodException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                    return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        return result.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * Clears internal data structures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    private void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        subs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        handles.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * Underlying writeObject/writeUnshared implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    private void writeObject0(Object obj, boolean unshared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        boolean oldMode = bout.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        depth++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            // handle previously written and non-replaceable objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            int h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            if ((obj = subs.lookup(obj)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                writeNull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            } else if (!unshared && (h = handles.lookup(obj)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                writeHandle(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            } else if (obj instanceof Class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                writeClass((Class) obj, unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            } else if (obj instanceof ObjectStreamClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                writeClassDesc((ObjectStreamClass) obj, unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            // check for replacement object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            Object orig = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            Class cl = obj.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            ObjectStreamClass desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                // REMIND: skip this check for strings/arrays?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                Class repCl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                desc = ObjectStreamClass.lookup(cl, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                if (!desc.hasWriteReplaceMethod() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                    (obj = desc.invokeWriteReplace(obj)) == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    (repCl = obj.getClass()) == cl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                cl = repCl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            if (enableReplace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                Object rep = replaceObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                if (rep != obj && rep != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                    cl = rep.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                    desc = ObjectStreamClass.lookup(cl, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                obj = rep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            // if object replaced, run through original checks a second time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            if (obj != orig) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                subs.assign(orig, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                    writeNull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                } else if (!unshared && (h = handles.lookup(obj)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                    writeHandle(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                } else if (obj instanceof Class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                    writeClass((Class) obj, unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                } else if (obj instanceof ObjectStreamClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    writeClassDesc((ObjectStreamClass) obj, unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            // remaining cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            if (obj instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                writeString((String) obj, unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            } else if (cl.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                writeArray(obj, desc, unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            } else if (obj instanceof Enum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                writeEnum((Enum) obj, desc, unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            } else if (obj instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                writeOrdinaryObject(obj, desc, unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                    throw new NotSerializableException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                        cl.getName() + "\n" + debugInfoStack.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                    throw new NotSerializableException(cl.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            depth--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            bout.setBlockDataMode(oldMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * Writes null code to stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    private void writeNull() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        bout.writeByte(TC_NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * Writes given object handle to stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    private void writeHandle(int handle) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        bout.writeByte(TC_REFERENCE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        bout.writeInt(baseWireHandle + handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * Writes representation of given class to stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    private void writeClass(Class cl, boolean unshared) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        bout.writeByte(TC_CLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        writeClassDesc(ObjectStreamClass.lookup(cl, true), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        handles.assign(unshared ? null : cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * Writes representation of given class descriptor to stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    private void writeClassDesc(ObjectStreamClass desc, boolean unshared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        int handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        if (desc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            writeNull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        } else if (!unshared && (handle = handles.lookup(desc)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            writeHandle(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        } else if (desc.isProxy()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            writeProxyDesc(desc, unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            writeNonProxyDesc(desc, unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * Writes class descriptor representing a dynamic proxy class to stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    private void writeProxyDesc(ObjectStreamClass desc, boolean unshared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        bout.writeByte(TC_PROXYCLASSDESC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        handles.assign(unshared ? null : desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        Class cl = desc.forClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        Class[] ifaces = cl.getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        bout.writeInt(ifaces.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        for (int i = 0; i < ifaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            bout.writeUTF(ifaces[i].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        bout.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        annotateProxyClass(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        bout.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        bout.writeByte(TC_ENDBLOCKDATA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        writeClassDesc(desc.getSuperDesc(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * Writes class descriptor representing a standard (i.e., not a dynamic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * proxy) class to stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    private void writeNonProxyDesc(ObjectStreamClass desc, boolean unshared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        bout.writeByte(TC_CLASSDESC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        handles.assign(unshared ? null : desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        if (protocol == PROTOCOL_VERSION_1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            // do not invoke class descriptor write hook with old protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            desc.writeNonProxy(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            writeClassDescriptor(desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        Class cl = desc.forClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        bout.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        annotateClass(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        bout.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        bout.writeByte(TC_ENDBLOCKDATA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        writeClassDesc(desc.getSuperDesc(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * Writes given string to stream, using standard or long UTF format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * depending on string length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    private void writeString(String str, boolean unshared) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        handles.assign(unshared ? null : str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        long utflen = bout.getUTFLength(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        if (utflen <= 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            bout.writeByte(TC_STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            bout.writeUTF(str, utflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            bout.writeByte(TC_LONGSTRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            bout.writeLongUTF(str, utflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * Writes given array object to stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    private void writeArray(Object array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                            ObjectStreamClass desc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                            boolean unshared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        bout.writeByte(TC_ARRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        writeClassDesc(desc, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        handles.assign(unshared ? null : array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        Class ccl = desc.forClass().getComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        if (ccl.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            if (ccl == Integer.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                int[] ia = (int[]) array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                bout.writeInt(ia.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                bout.writeInts(ia, 0, ia.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            } else if (ccl == Byte.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                byte[] ba = (byte[]) array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                bout.writeInt(ba.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                bout.write(ba, 0, ba.length, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            } else if (ccl == Long.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                long[] ja = (long[]) array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                bout.writeInt(ja.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                bout.writeLongs(ja, 0, ja.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            } else if (ccl == Float.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                float[] fa = (float[]) array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                bout.writeInt(fa.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                bout.writeFloats(fa, 0, fa.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            } else if (ccl == Double.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                double[] da = (double[]) array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                bout.writeInt(da.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                bout.writeDoubles(da, 0, da.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            } else if (ccl == Short.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                short[] sa = (short[]) array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                bout.writeInt(sa.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                bout.writeShorts(sa, 0, sa.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            } else if (ccl == Character.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                char[] ca = (char[]) array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                bout.writeInt(ca.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                bout.writeChars(ca, 0, ca.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            } else if (ccl == Boolean.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                boolean[] za = (boolean[]) array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                bout.writeInt(za.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                bout.writeBooleans(za, 0, za.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            Object[] objs = (Object[]) array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            int len = objs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            bout.writeInt(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                debugInfoStack.push(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                    "array (class \"" + array.getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                    "\", size: " + len  + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                    if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                        debugInfoStack.push(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                            "element of array (index: " + i + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                        writeObject0(objs[i], false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                        if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                            debugInfoStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                    debugInfoStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * Writes given enum constant to stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    private void writeEnum(Enum en,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                           ObjectStreamClass desc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                           boolean unshared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        bout.writeByte(TC_ENUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        ObjectStreamClass sdesc = desc.getSuperDesc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        writeClassDesc((sdesc.forClass() == Enum.class) ? desc : sdesc, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        handles.assign(unshared ? null : en);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        writeString(en.name(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * Writes representation of a "ordinary" (i.e., not a String, Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * ObjectStreamClass, array, or enum constant) serializable object to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    private void writeOrdinaryObject(Object obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                                     ObjectStreamClass desc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                                     boolean unshared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            debugInfoStack.push(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                (depth == 1 ? "root " : "") + "object (class \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                obj.getClass().getName() + "\", " + obj.toString() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            desc.checkSerialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            bout.writeByte(TC_OBJECT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            writeClassDesc(desc, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            handles.assign(unshared ? null : obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            if (desc.isExternalizable() && !desc.isProxy()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                writeExternalData((Externalizable) obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                writeSerialData(obj, desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                debugInfoStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * Writes externalizable data of given object by invoking its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * writeExternal() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    private void writeExternalData(Externalizable obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        PutFieldImpl oldPut = curPut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        curPut = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            debugInfoStack.push("writeExternal data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        }
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
  1436
        SerialCallbackContext oldContext = curContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        try {
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
  1438
            curContext = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            if (protocol == PROTOCOL_VERSION_1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                obj.writeExternal(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                bout.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                obj.writeExternal(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                bout.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                bout.writeByte(TC_ENDBLOCKDATA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        } finally {
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
  1448
            curContext = oldContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                debugInfoStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        curPut = oldPut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * Writes instance data for each serializable class of given object, from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * superclass to subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    private void writeSerialData(Object obj, ObjectStreamClass desc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        ObjectStreamClass.ClassDataSlot[] slots = desc.getClassDataLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        for (int i = 0; i < slots.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            ObjectStreamClass slotDesc = slots[i].desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            if (slotDesc.hasWriteObjectMethod()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                PutFieldImpl oldPut = curPut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                curPut = null;
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
  1470
                SerialCallbackContext oldContext = curContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                    debugInfoStack.push(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                        "custom writeObject data (class \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                        slotDesc.getName() + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                try {
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
  1478
                    curContext = new SerialCallbackContext(obj, slotDesc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                    bout.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                    slotDesc.invokeWriteObject(obj, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                    bout.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                    bout.writeByte(TC_ENDBLOCKDATA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                } finally {
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
  1484
                    curContext.setUsed();
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
  1485
                    curContext = oldContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                    if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                        debugInfoStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                curPut = oldPut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                defaultWriteFields(obj, slotDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * Fetches and writes values of serializable fields of given object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * stream.  The given class descriptor specifies which field values to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * write, and in which order they should be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    private void defaultWriteFields(Object obj, ObjectStreamClass desc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        // REMIND: perform conservative isInstance check here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        desc.checkDefaultSerialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        int primDataSize = desc.getPrimDataSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        if (primVals == null || primVals.length < primDataSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            primVals = new byte[primDataSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        desc.getPrimFieldValues(obj, primVals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        bout.write(primVals, 0, primDataSize, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        ObjectStreamField[] fields = desc.getFields(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        Object[] objVals = new Object[desc.getNumObjFields()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        int numPrimFields = fields.length - objVals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        desc.getObjFieldValues(obj, objVals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        for (int i = 0; i < objVals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                debugInfoStack.push(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                    "field (class \"" + desc.getName() + "\", name: \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                    fields[numPrimFields + i].getName() + "\", type: \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                    fields[numPrimFields + i].getType() + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                writeObject0(objVals[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                             fields[numPrimFields + i].isUnshared());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                    debugInfoStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * Attempts to write to stream fatal IOException that has caused
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * serialization to abort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
    private void writeFatalException(IOException ex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
         * Note: the serialization specification states that if a second
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
         * IOException occurs while attempting to serialize the original fatal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
         * exception to the stream, then a StreamCorruptedException should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
         * thrown (section 2.1).  However, due to a bug in previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
         * implementations of serialization, StreamCorruptedExceptions were
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
         * rarely (if ever) actually thrown--the "root" exceptions from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
         * underlying streams were thrown instead.  This historical behavior is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
         * followed here for consistency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        boolean oldMode = bout.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            bout.writeByte(TC_EXCEPTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            writeObject0(ex, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            bout.setBlockDataMode(oldMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * Converts specified span of float values into byte values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    // REMIND: remove once hotspot inlines Float.floatToIntBits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    private static native void floatsToBytes(float[] src, int srcpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                                             byte[] dst, int dstpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                                             int nfloats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * Converts specified span of double values into byte values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    // REMIND: remove once hotspot inlines Double.doubleToLongBits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    private static native void doublesToBytes(double[] src, int srcpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                                              byte[] dst, int dstpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                                              int ndoubles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * Default PutField implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
    private class PutFieldImpl extends PutField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        /** class descriptor describing serializable fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        private final ObjectStreamClass desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        /** primitive field values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        private final byte[] primVals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        /** object field values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        private final Object[] objVals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
         * Creates PutFieldImpl object for writing fields defined in given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
         * class descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        PutFieldImpl(ObjectStreamClass desc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            this.desc = desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            primVals = new byte[desc.getPrimDataSize()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            objVals = new Object[desc.getNumObjFields()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        public void put(String name, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            Bits.putBoolean(primVals, getFieldOffset(name, Boolean.TYPE), val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        public void put(String name, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            primVals[getFieldOffset(name, Byte.TYPE)] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        public void put(String name, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            Bits.putChar(primVals, getFieldOffset(name, Character.TYPE), val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        public void put(String name, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            Bits.putShort(primVals, getFieldOffset(name, Short.TYPE), val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        public void put(String name, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            Bits.putInt(primVals, getFieldOffset(name, Integer.TYPE), val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        public void put(String name, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            Bits.putFloat(primVals, getFieldOffset(name, Float.TYPE), val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        public void put(String name, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            Bits.putLong(primVals, getFieldOffset(name, Long.TYPE), val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        public void put(String name, double val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            Bits.putDouble(primVals, getFieldOffset(name, Double.TYPE), val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        public void put(String name, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            objVals[getFieldOffset(name, Object.class)] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        // deprecated in ObjectOutputStream.PutField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        public void write(ObjectOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
             * Applications should *not* use this method to write PutField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
             * data, as it will lead to stream corruption if the PutField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
             * object writes any primitive data (since block data mode is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
             * unset/set properly, as is done in OOS.writeFields()).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
             * broken implementation is being retained solely for behavioral
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
             * compatibility, in order to support applications which use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
             * OOS.PutField.write() for writing only non-primitive data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
             * Serialization of unshared objects is not implemented here since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
             * it is not necessary for backwards compatibility; also, unshared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
             * semantics may not be supported by the given ObjectOutput
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
             * instance.  Applications which write unshared objects using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
             * PutField API must use OOS.writeFields().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            if (ObjectOutputStream.this != out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                throw new IllegalArgumentException("wrong stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            out.write(primVals, 0, primVals.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            ObjectStreamField[] fields = desc.getFields(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            int numPrimFields = fields.length - objVals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            // REMIND: warn if numPrimFields > 0?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            for (int i = 0; i < objVals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                if (fields[numPrimFields + i].isUnshared()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                    throw new IOException("cannot write unshared object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                out.writeObject(objVals[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
         * Writes buffered primitive data and object fields to stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        void writeFields() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            bout.write(primVals, 0, primVals.length, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            ObjectStreamField[] fields = desc.getFields(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            int numPrimFields = fields.length - objVals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            for (int i = 0; i < objVals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                    debugInfoStack.push(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                        "field (class \"" + desc.getName() + "\", name: \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                        fields[numPrimFields + i].getName() + "\", type: \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                        fields[numPrimFields + i].getType() + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                    writeObject0(objVals[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                                 fields[numPrimFields + i].isUnshared());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                    if (extendedDebugInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                        debugInfoStack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
         * Returns offset of field with given name and type.  A specified type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
         * of null matches all types, Object.class matches all non-primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
         * types, and any other non-null type matches assignable types only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
         * Throws IllegalArgumentException if no matching field found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        private int getFieldOffset(String name, Class type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            ObjectStreamField field = desc.getField(name, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            if (field == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                throw new IllegalArgumentException("no such field " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                                                   " with type " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            return field.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * Buffered output stream with two modes: in default mode, outputs data in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * same format as DataOutputStream; in "block data" mode, outputs data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * bracketed by block data markers (see object serialization specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * for details).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    private static class BlockDataOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        extends OutputStream implements DataOutput
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        /** maximum data block length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        private static final int MAX_BLOCK_SIZE = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        /** maximum data block header length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        private static final int MAX_HEADER_SIZE = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        /** (tunable) length of char buffer (for writing strings) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        private static final int CHAR_BUF_SIZE = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        /** buffer for writing general/block data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        private final byte[] buf = new byte[MAX_BLOCK_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        /** buffer for writing block data headers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        private final byte[] hbuf = new byte[MAX_HEADER_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        /** char buffer for fast string writes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        private final char[] cbuf = new char[CHAR_BUF_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        /** block data mode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        private boolean blkmode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        /** current offset into buf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        private int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        /** underlying output stream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        private final OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        /** loopback stream (for data writes that span data blocks) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        private final DataOutputStream dout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
         * Creates new BlockDataOutputStream on top of given underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
         * Block data mode is turned off by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        BlockDataOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            dout = new DataOutputStream(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
         * Sets block data mode to the given mode (true == on, false == off)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
         * and returns the previous mode value.  If the new mode is the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
         * the old mode, no action is taken.  If the new mode differs from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
         * old mode, any buffered data is flushed before switching to the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
         * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        boolean setBlockDataMode(boolean mode) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            if (blkmode == mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                return blkmode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            drain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            blkmode = mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            return !blkmode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
         * Returns true if the stream is currently in block data mode, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
         * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        boolean getBlockDataMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
            return blkmode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        /* ----------------- generic output stream methods ----------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
         * The following methods are equivalent to their counterparts in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
         * OutputStream, except that they partition written data into data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
         * blocks when in block data mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            if (pos >= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                drain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            buf[pos++] = (byte) b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        public void write(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            write(b, 0, b.length, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        public void write(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            write(b, off, len, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            drain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
            flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
         * Writes specified span of byte values from given array.  If copy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
         * true, copies the values to an intermediate buffer before writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
         * them to underlying stream (to avoid exposing a reference to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
         * original byte array).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        void write(byte[] b, int off, int len, boolean copy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            if (!(copy || blkmode)) {           // write directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                drain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                if (pos >= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                    drain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                if (len >= MAX_BLOCK_SIZE && !copy && pos == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                    // avoid unnecessary copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                    writeBlockHeader(MAX_BLOCK_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                    out.write(b, off, MAX_BLOCK_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                    off += MAX_BLOCK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                    len -= MAX_BLOCK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                    int wlen = Math.min(len, MAX_BLOCK_SIZE - pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                    System.arraycopy(b, off, buf, pos, wlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                    pos += wlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                    off += wlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                    len -= wlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
         * Writes all buffered data from this stream to the underlying stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
         * but does not flush underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        void drain() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
            if (pos == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            if (blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                writeBlockHeader(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            out.write(buf, 0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
         * Writes block data header.  Data blocks shorter than 256 bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
         * prefixed with a 2-byte header; all others start with a 5-byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
         * header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        private void writeBlockHeader(int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            if (len <= 0xFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                hbuf[0] = TC_BLOCKDATA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                hbuf[1] = (byte) len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                out.write(hbuf, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                hbuf[0] = TC_BLOCKDATALONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                Bits.putInt(hbuf, 1, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                out.write(hbuf, 0, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        /* ----------------- primitive data output methods ----------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
         * The following methods are equivalent to their counterparts in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
         * DataOutputStream, except that they partition written data into data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
         * blocks when in block data mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        public void writeBoolean(boolean v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
            if (pos >= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                drain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            Bits.putBoolean(buf, pos++, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        public void writeByte(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            if (pos >= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                drain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            buf[pos++] = (byte) v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        public void writeChar(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            if (pos + 2 <= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                Bits.putChar(buf, pos, (char) v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                dout.writeChar(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        public void writeShort(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            if (pos + 2 <= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                Bits.putShort(buf, pos, (short) v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                dout.writeShort(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        public void writeInt(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
            if (pos + 4 <= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                Bits.putInt(buf, pos, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                dout.writeInt(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        public void writeFloat(float v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            if (pos + 4 <= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                Bits.putFloat(buf, pos, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                dout.writeFloat(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        public void writeLong(long v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            if (pos + 8 <= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                Bits.putLong(buf, pos, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                pos += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                dout.writeLong(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        public void writeDouble(double v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            if (pos + 8 <= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                Bits.putDouble(buf, pos, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                pos += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                dout.writeDouble(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        public void writeBytes(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            int endoff = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            int cpos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            int csize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            for (int off = 0; off < endoff; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                if (cpos >= csize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                    cpos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                    csize = Math.min(endoff - off, CHAR_BUF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                    s.getChars(off, off + csize, cbuf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                if (pos >= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                    drain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                int n = Math.min(csize - cpos, MAX_BLOCK_SIZE - pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                int stop = pos + n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                while (pos < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                    buf[pos++] = (byte) cbuf[cpos++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                off += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        public void writeChars(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            int endoff = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            for (int off = 0; off < endoff; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                int csize = Math.min(endoff - off, CHAR_BUF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                s.getChars(off, off + csize, cbuf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                writeChars(cbuf, 0, csize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                off += csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        public void writeUTF(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            writeUTF(s, getUTFLength(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        /* -------------- primitive data array output methods -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
         * The following methods write out spans of primitive data values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
         * Though equivalent to calling the corresponding primitive write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
         * methods repeatedly, these methods are optimized for writing groups
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
         * of primitive data values more efficiently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        void writeBooleans(boolean[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            int endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                if (pos >= MAX_BLOCK_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                    drain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                int stop = Math.min(endoff, off + (MAX_BLOCK_SIZE - pos));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                while (off < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                    Bits.putBoolean(buf, pos++, v[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        void writeChars(char[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            int limit = MAX_BLOCK_SIZE - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            int endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                if (pos <= limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                    int avail = (MAX_BLOCK_SIZE - pos) >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                    int stop = Math.min(endoff, off + avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                    while (off < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                        Bits.putChar(buf, pos, v[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                        pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                    dout.writeChar(v[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        void writeShorts(short[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            int limit = MAX_BLOCK_SIZE - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            int endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                if (pos <= limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                    int avail = (MAX_BLOCK_SIZE - pos) >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                    int stop = Math.min(endoff, off + avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                    while (off < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                        Bits.putShort(buf, pos, v[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                        pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                    dout.writeShort(v[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        void writeInts(int[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            int limit = MAX_BLOCK_SIZE - 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            int endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                if (pos <= limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                    int avail = (MAX_BLOCK_SIZE - pos) >> 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                    int stop = Math.min(endoff, off + avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                    while (off < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                        Bits.putInt(buf, pos, v[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                        pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                    dout.writeInt(v[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        void writeFloats(float[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
            int limit = MAX_BLOCK_SIZE - 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            int endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                if (pos <= limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                    int avail = (MAX_BLOCK_SIZE - pos) >> 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                    int chunklen = Math.min(endoff - off, avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                    floatsToBytes(v, off, buf, pos, chunklen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                    off += chunklen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                    pos += chunklen << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                    dout.writeFloat(v[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        void writeLongs(long[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            int limit = MAX_BLOCK_SIZE - 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            int endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                if (pos <= limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                    int avail = (MAX_BLOCK_SIZE - pos) >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                    int stop = Math.min(endoff, off + avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                    while (off < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                        Bits.putLong(buf, pos, v[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                        pos += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                    dout.writeLong(v[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        void writeDoubles(double[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
            int limit = MAX_BLOCK_SIZE - 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            int endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                if (pos <= limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                    int avail = (MAX_BLOCK_SIZE - pos) >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                    int chunklen = Math.min(endoff - off, avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                    doublesToBytes(v, off, buf, pos, chunklen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                    off += chunklen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                    pos += chunklen << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                    dout.writeDouble(v[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
         * Returns the length in bytes of the UTF encoding of the given string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        long getUTFLength(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            long utflen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            for (int off = 0; off < len; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                int csize = Math.min(len - off, CHAR_BUF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                s.getChars(off, off + csize, cbuf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                for (int cpos = 0; cpos < csize; cpos++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                    char c = cbuf[cpos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                    if (c >= 0x0001 && c <= 0x007F) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                        utflen++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                    } else if (c > 0x07FF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                        utflen += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                        utflen += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                off += csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            return utflen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
         * Writes the given string in UTF format.  This method is used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
         * situations where the UTF encoding length of the string is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
         * known; specifying it explicitly avoids a prescan of the string to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
         * determine its UTF length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        void writeUTF(String s, long utflen) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            if (utflen > 0xFFFFL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                throw new UTFDataFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            writeShort((int) utflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            if (utflen == (long) s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                writeBytes(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                writeUTFBody(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
         * Writes given string in "long" UTF format.  "Long" UTF format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
         * identical to standard UTF, except that it uses an 8 byte header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
         * (instead of the standard 2 bytes) to convey the UTF encoding length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
        void writeLongUTF(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            writeLongUTF(s, getUTFLength(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
         * Writes given string in "long" UTF format, where the UTF encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
         * length of the string is already known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        void writeLongUTF(String s, long utflen) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            writeLong(utflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            if (utflen == (long) s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                writeBytes(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                writeUTFBody(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
         * Writes the "body" (i.e., the UTF representation minus the 2-byte or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
         * 8-byte length header) of the UTF encoding for the given string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
        private void writeUTFBody(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
            int limit = MAX_BLOCK_SIZE - 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
            int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
            for (int off = 0; off < len; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                int csize = Math.min(len - off, CHAR_BUF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                s.getChars(off, off + csize, cbuf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                for (int cpos = 0; cpos < csize; cpos++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                    char c = cbuf[cpos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                    if (pos <= limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                        if (c <= 0x007F && c != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                            buf[pos++] = (byte) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                        } else if (c > 0x07FF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                            buf[pos + 2] = (byte) (0x80 | ((c >> 0) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                            buf[pos + 1] = (byte) (0x80 | ((c >> 6) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                            buf[pos + 0] = (byte) (0xE0 | ((c >> 12) & 0x0F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                            pos += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                            buf[pos + 1] = (byte) (0x80 | ((c >> 0) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                            buf[pos + 0] = (byte) (0xC0 | ((c >> 6) & 0x1F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                    } else {    // write one byte at a time to normalize block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                        if (c <= 0x007F && c != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                            write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                        } else if (c > 0x07FF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                            write(0xE0 | ((c >> 12) & 0x0F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                            write(0x80 | ((c >> 6) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                            write(0x80 | ((c >> 0) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                            write(0xC0 | ((c >> 6) & 0x1F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                            write(0x80 | ((c >> 0) & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                off += csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * Lightweight identity hash table which maps objects to integer handles,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * assigned in ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
    private static class HandleTable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        /* number of mappings in table/next available handle */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
        /* size threshold determining when to expand hash spine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        private int threshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
        /* factor for computing size threshold */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        private final float loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
        /* maps hash value -> candidate handle value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
        private int[] spine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        /* maps handle value -> next candidate handle value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
        private int[] next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        /* maps handle value -> associated object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
        private Object[] objs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
         * Creates new HandleTable with given capacity and load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        HandleTable(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            this.loadFactor = loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            spine = new int[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            next = new int[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            objs = new Object[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            threshold = (int) (initialCapacity * loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
         * Assigns next available handle to given object, and returns handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
         * value.  Handles are assigned in ascending order starting at 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
        int assign(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            if (size >= next.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                growEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            if (size >= threshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                growSpine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
            insert(obj, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
            return size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
         * Looks up and returns handle associated with given object, or -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
         * no mapping found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        int lookup(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            int index = hash(obj) % spine.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            for (int i = spine[index]; i >= 0; i = next[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                if (objs[i] == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
         * Resets table to its initial (empty) state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            Arrays.fill(spine, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            Arrays.fill(objs, 0, size, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
         * Returns the number of mappings currently in table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
        int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
         * Inserts mapping object -> handle mapping into table.  Assumes table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
         * is large enough to accommodate new mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
        private void insert(Object obj, int handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            int index = hash(obj) % spine.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
            objs[handle] = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            next[handle] = spine[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
            spine[index] = handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
         * Expands the hash "spine" -- equivalent to increasing the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
         * buckets in a conventional hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        private void growSpine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            spine = new int[(spine.length << 1) + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            threshold = (int) (spine.length * loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            Arrays.fill(spine, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                insert(objs[i], i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
         * Increases hash table capacity by lengthening entry arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
        private void growEntries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            int newLength = (next.length << 1) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            int[] newNext = new int[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            System.arraycopy(next, 0, newNext, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
            next = newNext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            Object[] newObjs = new Object[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            System.arraycopy(objs, 0, newObjs, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            objs = newObjs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
         * Returns hash value for given object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        private int hash(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
            return System.identityHashCode(obj) & 0x7FFFFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
     * Lightweight identity hash table which maps objects to replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
     * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
    private static class ReplaceTable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
        /* maps object -> index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
        private final HandleTable htab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
        /* maps index -> replacement object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        private Object[] reps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
         * Creates new ReplaceTable with given capacity and load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        ReplaceTable(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            htab = new HandleTable(initialCapacity, loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
            reps = new Object[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
         * Enters mapping from object to replacement object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        void assign(Object obj, Object rep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
            int index = htab.assign(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            while (index >= reps.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            reps[index] = rep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
         * Looks up and returns replacement for given object.  If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
         * replacement is found, returns the lookup object itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        Object lookup(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
            int index = htab.lookup(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            return (index >= 0) ? reps[index] : obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
         * Resets table to its initial (empty) state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
        void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
            Arrays.fill(reps, 0, htab.size(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            htab.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
         * Returns the number of mappings currently in table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
        int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            return htab.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
         * Increases table capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        private void grow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
            Object[] newReps = new Object[(reps.length << 1) + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
            System.arraycopy(reps, 0, newReps, 0, reps.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            reps = newReps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * Stack to keep debug information about the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * serialization process, for embedding in exception messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
    private static class DebugTraceInfoStack {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        private final List<String> stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
        DebugTraceInfoStack() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7030
diff changeset
  2416
            stack = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
         * Removes all of the elements from enclosed list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
            stack.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
         * Removes the object at the top of enclosed list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        void pop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
            stack.remove(stack.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
         * Pushes a String onto the top of enclosed list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        void push(String entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
            stack.add("\t- " + entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
         * Returns a string representation of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
            StringBuilder buffer = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
            if (!stack.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                for(int i = stack.size(); i > 0; i-- ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                    buffer.append(stack.get(i-1) + ((i != 1) ? "\n" : ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
            return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
}