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