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