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