jdk/src/java.base/share/specs/serialization/input.md
author ihse
Sun, 23 Apr 2017 21:33:29 +0200
changeset 44787 0b323ea6d5ad
permissions -rw-r--r--
8179022: Add serialization spec as markdown Reviewed-by: erikj, mchung, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44787
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
     1
---
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
     2
# Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
     3
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
     4
#
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
     5
# This code is free software; you can redistribute it and/or modify it
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
     6
# under the terms of the GNU General Public License version 2 only, as
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
     7
# published by the Free Software Foundation.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
     8
#
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
     9
# This code is distributed in the hope that it will be useful, but WITHOUT
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    11
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    12
# version 2 for more details (a copy is included in the LICENSE file that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    13
# accompanied this code).
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    14
#
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    15
# You should have received a copy of the GNU General Public License version
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    16
# 2 along with this work; if not, write to the Free Software Foundation,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    17
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    18
#
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    19
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    20
# or visit www.oracle.com if you need additional information or have any
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    21
# questions.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    22
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    23
include-before: '[CONTENTS](index.html) | [PREV](output.html) | [NEXT](class.html)'
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    24
include-after: '[CONTENTS](index.html) | [PREV](output.html) | [NEXT](class.html)'
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    25
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    26
title: 'Java Object Serialization Specification: 3 - Object Input Classes'
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    27
---
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    28
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    29
-   [The ObjectInputStream Class](#the-objectinputstream-class)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    30
-   [The ObjectInputStream.GetField
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    31
    Class](#the-objectinputstream.getfield-class)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    32
-   [The ObjectInputValidation Interface](#the-objectinputvalidation-interface)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    33
-   [The readObject Method](#the-readobject-method)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    34
-   [The readExternal Method](#the-readexternal-method)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    35
-   [The readResolve Method](#the-readresolve-method)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    36
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    37
-------------------------------------------------------------------------------
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    38
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    39
## 3.1 The ObjectInputStream Class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    40
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    41
Class `ObjectInputStream` implements object deserialization. It maintains the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    42
state of the stream including the set of objects already deserialized. Its
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    43
methods allow primitive types and objects to be read from a stream written by
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    44
`ObjectOutputStream`. It manages restoration of the object and the objects that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    45
it refers to from the stream.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    46
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    47
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    48
package java.io;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    49
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    50
public class ObjectInputStream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    51
    extends InputStream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    52
    implements ObjectInput, ObjectStreamConstants
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    53
{
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    54
    public ObjectInputStream(InputStream in)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    55
        throws StreamCorruptedException, IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    56
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    57
    public final Object readObject()
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    58
        throws OptionalDataException, ClassNotFoundException,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    59
            IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    60
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    61
    public Object readUnshared()
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    62
        throws OptionalDataException, ClassNotFoundException,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    63
            IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    64
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    65
    public void defaultReadObject()
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    66
        throws IOException, ClassNotFoundException,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    67
            NotActiveException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    68
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    69
    public GetField readFields()
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    70
        throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    71
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    72
    public synchronized void registerValidation(
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    73
        ObjectInputValidation obj, int prio)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    74
        throws NotActiveException, InvalidObjectException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    75
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    76
    protected ObjectStreamClass readClassDescriptor()
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    77
        throws IOException, ClassNotFoundException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    78
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    79
    protected Class resolveClass(ObjectStreamClass v)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    80
        throws IOException, ClassNotFoundException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    81
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    82
    protected Object resolveObject(Object obj)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    83
        throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    84
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    85
    protected boolean enableResolveObject(boolean enable)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    86
        throws SecurityException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    87
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    88
    protected void readStreamHeader()
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    89
        throws IOException, StreamCorruptedException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    90
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    91
    public int read() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    92
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    93
    public int read(byte[] data, int offset, int length)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    94
        throws IOException
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    95
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    96
    public int available() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    97
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    98
    public void close() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    99
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   100
    public boolean readBoolean() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   101
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   102
    public byte readByte() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   103
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   104
    public int readUnsignedByte() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   105
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   106
    public short readShort() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   107
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   108
    public int readUnsignedShort() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   109
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   110
    public char readChar() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   111
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   112
    public int readInt() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   113
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   114
    public long readLong() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   115
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   116
    public float readFloat() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   117
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   118
    public double readDouble() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   119
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   120
    public void readFully(byte[] data) throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   121
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   122
    public void readFully(byte[] data, int offset, int size)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   123
        throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   124
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   125
    public int skipBytes(int len) throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   126
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   127
    public String readLine() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   128
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   129
    public String readUTF() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   130
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   131
    // Class to provide access to serializable fields.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   132
    static abstract public class GetField
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   133
    {
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   134
        public ObjectStreamClass getObjectStreamClass();
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   135
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   136
        public boolean defaulted(String name)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   137
            throws IOException, IllegalArgumentException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   138
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   139
        public char get(String name, char default)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   140
            throws IOException, IllegalArgumentException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   141
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   142
        public boolean get(String name, boolean default)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   143
            throws IOException, IllegalArgumentException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   144
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   145
        public byte get(String name, byte default)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   146
            throws IOException, IllegalArgumentException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   147
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   148
        public short get(String name, short default)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   149
            throws IOException, IllegalArgumentException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   150
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   151
        public int get(String name, int default)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   152
            throws IOException, IllegalArgumentException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   153
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   154
        public long get(String name, long default)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   155
            throws IOException, IllegalArgumentException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   156
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   157
        public float get(String name, float default)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   158
            throws IOException, IllegalArgumentException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   159
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   160
        public double get(String name, double default)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   161
            throws IOException, IllegalArgumentException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   162
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   163
        public Object get(String name, Object default)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   164
            throws IOException, IllegalArgumentException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   165
    }
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   166
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   167
    protected ObjectInputStream()
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   168
        throws StreamCorruptedException, IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   169
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   170
    protected readObjectOverride()
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   171
        throws OptionalDataException, ClassNotFoundException,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   172
            IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   173
}
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   174
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   175
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   176
The single-argument `ObjectInputStream` constructor requires an `InputStream`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   177
The constructor calls `readStreamHeader` to read and verifies the header and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   178
version written by the corresponding `ObjectOutputStream.writeStreamHeader`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   179
method. If a security manager is installed, this constructor checks for the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   180
`"enableSubclassImplementation"` `SerializablePermission` when invoked directly
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   181
or indirectly by the constructor of a subclass which overrides the `readFields`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   182
and/or `readUnshared` methods.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   183
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   184
**Note:** The `ObjectInputStream` constructor blocks until it completes reading
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   185
the serialization stream header. Code which waits for an `ObjectInputStream` to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   186
be constructed before creating the corresponding `ObjectOutputStream` for that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   187
stream will deadlock, since the `ObjectInputStream` constructor will block
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   188
until a header is written to the stream, and the header will not be written to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   189
the stream until the `ObjectOutputStream` constructor executes. This problem
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   190
can be resolved by creating the `ObjectOutputStream` before the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   191
`ObjectInputStream`, or otherwise removing the timing dependency between
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   192
completion of `ObjectInputStream` construction and the creation of the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   193
`ObjectOutputStream`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   194
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   195
The `readObject` method is used to deserialize an object from the stream. It
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   196
reads from the stream to reconstruct an object.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   197
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   198
1.  If the `ObjectInputStream` subclass is overriding the implementation, call
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   199
    the `readObjectOverride` method and return. Reimplementation is described
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   200
    at the end of this section.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   201
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   202
2.  If a block data record occurs in the stream, throw a `BlockDataException`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   203
    with the number of available bytes.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   204
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   205
3.  If the object in the stream is null, return null.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   206
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   207
4.  If the object in the stream is a handle to a previous object, return the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   208
    object.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   209
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   210
5.  If the object in the stream is a `Class`, read its `ObjectStreamClass`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   211
    descriptor, add it and its handle to the set of known objects, and return
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   212
    the corresponding `Class` object.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   213
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   214
6.  If the object in the stream is an `ObjectStreamClass`, read in its data
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   215
    according to the formats described in [Section 4.3, "Serialized
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   216
    Form"](class.html#serialized-form). Add it and its handle to the set of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   217
    known objects. In versions 1.3 and later of the Java 2 SDK, Standard
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   218
    Edition, the `readClassDescriptor` method is called to read in the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   219
    `ObjectStreamClass` if it represents a class that is not a dynamic proxy
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   220
    class, as indicated in the stream data. If the class descriptor represents
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   221
    a dynamic proxy class, call the `resolveProxyClass` method on the stream to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   222
    get the local class for the descriptor; otherwise, call the `resolveClass`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   223
    method on the stream to get the local class. If the class cannot be
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   224
    resolved, throw a ClassNotFoundException. Return the resulting
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   225
    `ObjectStreamClass` object.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   226
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   227
7.  If the object in the stream is a `String`, read its length information
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   228
    followed by the contents of the string encoded in modified UTF-8. For
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   229
    details, refer to [Section 6.2, "Stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   230
    Elements"](protocol.html#stream-elements). Add the `String` and its handle
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   231
    to the set of known objects, and proceed to Step 12.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   232
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   233
8.  If the object in the stream is an array, read its `ObjectStreamClass` and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   234
    the length of the array. Allocate the array, and add it and its handle in
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   235
    the set of known objects. Read each element using the appropriate method
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   236
    for its type and assign it to the array. Proceed to Step 12.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   237
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   238
9.  If the object in the stream is an enum constant, read its
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   239
    `ObjectStreamClass` and the enum constant name. If the `ObjectStreamClass`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   240
    represents a class that is not an enum type, an `InvalidClassException` is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   241
    thrown. Obtain a reference to the enum constant by calling the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   242
    `java.lang.Enum.valueOf` method, passing the enum type bound to the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   243
    received `ObjectStreamClass` along with the received name as arguments. If
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   244
    the `valueOf` method throws an `IllegalArgumentException`, an
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   245
    `InvalidObjectException` is thrown with the `IllegalArgumentException` as
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   246
    its cause. Add the enum constant and its handle in the set of known
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   247
    objects, and proceed to Step 12.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   248
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   249
10. For all other objects, the `ObjectStreamClass` of the object is read from
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   250
    the stream. The local class for that `ObjectStreamClass` is retrieved. The
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   251
    class must be serializable or externalizable, and must not be an enum type.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   252
    If the class does not satisfy these criteria, an `InvalidClassException` is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   253
    thrown.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   254
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   255
11. An instance of the class is allocated. The instance and its handle are
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   256
    added to the set of known objects. The contents restored appropriately:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   257
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   258
    a.  For serializable objects, the no-arg constructor for the first
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   259
        non-serializable supertype is run. For serializable classes, the fields
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   260
        are initialized to the default value appropriate for its type. Then the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   261
        fields of each class are restored by calling class-specific
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   262
        `readObject` methods, or if these are not defined, by calling the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   263
        `defaultReadObject` method. Note that field initializers and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   264
        constructors are not executed for serializable classes during
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   265
        deserialization. In the normal case, the version of the class that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   266
        wrote the stream will be the same as the class reading the stream. In
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   267
        this case, all of the supertypes of the object in the stream will match
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   268
        the supertypes in the currently-loaded class. If the version of the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   269
        class that wrote the stream had different supertypes than the loaded
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   270
        class, the `ObjectInputStream` must be more careful about restoring or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   271
        initializing the state of the differing classes. It must step through
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   272
        the classes, matching the available data in the stream with the classes
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   273
        of the object being restored. Data for classes that occur in the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   274
        stream, but do not occur in the object, is discarded. For classes that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   275
        occur in the object, but not in the stream, the class fields are set to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   276
        default values by default serialization.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   277
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   278
    b.  For externalizable objects, the no-arg constructor for the class is run
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   279
        and then the `readExternal` method is called to restore the contents of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   280
        the object.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   281
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   282
12. Process potential substitutions by the class of the object and/or by a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   283
    subclass of `ObjectInputStream`:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   284
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   285
    a.  If the class of the object is not an enum type and defines the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   286
        appropriate `readResolve` method, the method is called to allow the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   287
        object to replace itself.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   288
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   289
    b.  Then if previously enabled by `enableResolveObject,` the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   290
        `resolveObject` method is called to allow subclasses of the stream to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   291
        examine and replace the object. If the previous step did replace the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   292
        original object, the `resolveObject` method is called with the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   293
        replacement object. If a replacement took place, the table of known
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   294
        objects is updated so the replacement object is associated with the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   295
        handle. The replacement object is then returned from `readObject`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   296
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   297
All of the methods for reading primitives types only consume bytes from the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   298
block data records in the stream. If a read for primitive data occurs when the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   299
next item in the stream is an object, the read methods return *-1* or the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   300
`EOFException` as appropriate. The value of a primitive type is read by a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   301
`DataInputStream` from the block data record.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   302
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   303
The exceptions thrown reflect errors during the traversal or exceptions that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   304
occur on the underlying stream. If any exception is thrown, the underlying
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   305
stream is left in an unknown and unusable state.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   306
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   307
When the reset token occurs in the stream, all of the state of the stream is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   308
discarded. The set of known objects is cleared.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   309
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   310
When the exception token occurs in the stream, the exception is read and a new
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   311
`WriteAbortedException` is thrown with the terminating exception as an
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   312
argument. The stream context is reset as described earlier.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   313
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   314
The `readUnshared` method is used to read "unshared" objects from the stream.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   315
This method is identical to `readObject`, except that it prevents subsequent
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   316
calls to `readObject` and `readUnshared` from returning additional references
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   317
to the deserialized instance returned by the original call to `readUnshared`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   318
Specifically:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   319
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   320
-   If `readUnshared` is called to deserialize a back-reference (the stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   321
    representation of an object which has been written previously to the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   322
    stream), an `ObjectStreamException` will be thrown.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   323
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   324
-   If `readUnshared` returns successfully, then any subsequent attempts to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   325
    deserialize back-references to the stream handle deserialized by
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   326
    `readUnshared` will cause an `ObjectStreamException` to be thrown.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   327
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   328
Deserializing an object via `readUnshared` invalidates the stream handle
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   329
associated with the returned object. Note that this in itself does not always
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   330
guarantee that the reference returned by `readUnshared` is unique; the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   331
deserialized object may define a `readResolve` method which returns an object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   332
visible to other parties, or `readUnshared` may return a `Class` object or enum
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   333
constant obtainable elsewhere in the stream or through external means. If the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   334
deserialized object defines a `readResolve` method and the invocation of that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   335
method returns an array, then `readUnshared` returns a shallow clone of that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   336
array; this guarantees that the returned array object is unique and cannot be
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   337
obtained a second time from an invocation of `readObject` or `readUnshared` on
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   338
the `ObjectInputStream`, even if the underlying data stream has been
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   339
manipulated.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   340
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   341
The `defaultReadObject` method is used to read the fields and object from the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   342
stream. It uses the class descriptor in the stream to read the fields in the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   343
canonical order by name and type from the stream. The values are assigned to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   344
the matching fields by name in the current class. Details of the versioning
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   345
mechanism can be found in [Section 5.5, "Compatible Java Type
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   346
Evolution"](version.html#compatible-java-type-evolution). Any field of the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   347
object that does not appear in the stream is set to its default value. Values
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   348
that appear in the stream, but not in the object, are discarded. This occurs
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   349
primarily when a later version of a class has written additional fields that do
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   350
not occur in the earlier version. This method may only be called from the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   351
`readObject` method while restoring the fields of a class. When called at any
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   352
other time, the `NotActiveException` is thrown.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   353
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   354
The `readFields` method reads the values of the serializable fields from the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   355
stream and makes them available via the `GetField` class. The `readFields`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   356
method is only callable from within the `readObject` method of a serializable
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   357
class. It cannot be called more than once or if `defaultReadObject` has been
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   358
called. The `GetFields` object uses the current object's `ObjectStreamClass` to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   359
verify the fields that can be retrieved for this class. The `GetFields` object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   360
returned by `readFields` is only valid during this call to the classes
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   361
`readObject` method. The fields may be retrieved in any order. Additional data
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   362
may only be read directly from stream after `readFields` has been called.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   363
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   364
The `registerValidation` method can be called to request a callback when the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   365
entire graph has been restored but before the object is returned to the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   366
original caller of `readObject`. The order of validate callbacks can be
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   367
controlled using the priority. Callbacks registered with higher values are
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   368
called before those with lower values. The object to be validated must support
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   369
the `ObjectInputValidation` interface and implement the `validateObject`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   370
method. It is only correct to register validations during a call to a class's
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   371
`readObject` method. Otherwise, a `NotActiveException` is thrown. If the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   372
callback object supplied to `registerValidation` is null, an
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   373
`InvalidObjectException` is thrown.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   374
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   375
Starting with the Java SDK, Standard Edition, v1.3, the `readClassDescriptor`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   376
method is used to read in all `ObjectStreamClass` objects.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   377
`readClassDescriptor` is called when the `ObjectInputStream` expects a class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   378
descriptor as the next item in the serialization stream. Subclasses of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   379
`ObjectInputStream` may override this method to read in class descriptors that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   380
have been written in non-standard formats (by subclasses of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   381
`ObjectOutputStream` which have overridden the `writeClassDescriptor` method).
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   382
By default, this method reads class descriptors according to the format
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   383
described in [Section 6.4, "Grammar for the Stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   384
Format"](protocol.html#grammar-for-the-stream-format).
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   385
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   386
The `resolveClass` method is called while a class is being deserialized, and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   387
after the class descriptor has been read. Subclasses may extend this method to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   388
read other information about the class written by the corresponding subclass of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   389
`ObjectOutputStream`. The method must find and return the class with the given
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   390
name and `serialVersionUID`. The default implementation locates the class by
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   391
calling the class loader of the closest caller of `readObject` that has a class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   392
loader. If the class cannot be found `ClassNotFoundException` should be thrown.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   393
Prior to JDK 1.1.6, the `resolveClass` method was required to return the same
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   394
fully qualified class name as the class name in the stream. In order to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   395
accommodate package renaming across releases, `method` `resolveClass` only
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   396
needs to return a class with the same base class name and `SerialVersionUID` in
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   397
JDK 1.1.6 and later versions.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   398
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   399
The `resolveObject` method is used by trusted subclasses to monitor or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   400
substitute one object for another during deserialization. Resolving objects
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   401
must be enabled explicitly by calling `enableResolveObject` before calling
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   402
`readObject` for the first object to be resolved. Once enabled, `resolveObject`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   403
is called once for each serializable object just prior to the first time it is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   404
being returned from `readObject`. Note that the `resolveObject` method is not
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   405
called for objects of the specially handled classes, `Class`,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   406
`ObjectStreamClass`, `String`, and arrays. A subclass's implementation of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   407
`resolveObject` may return a substitute object that will be assigned or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   408
returned instead of the original. The object returned must be of a type that is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   409
consistent and assignable to every reference of the original object or else a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   410
`ClassCastException` will be thrown. All assignments are type-checked. All
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   411
references in the stream to the original object will be replaced by references
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   412
to the substitute object.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   413
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   414
The `enableResolveObject` method is called by trusted subclasses of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   415
`ObjectOutputStream` to enable the monitoring or substitution of one object for
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   416
another during deserialization. Replacing objects is disabled until
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   417
`enableResolveObject` is called with a `true` value. It may thereafter be
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   418
disabled by setting it to `false`. The previous setting is returned. The
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   419
`enableResolveObject` method checks if the stream has permission to request
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   420
substitution during serialization. To ensure that the private state of objects
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   421
is not unintentionally exposed, only trusted streams may use `resolveObject`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   422
Trusted classes are those classes with a class loader equal to null or belong
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   423
to a security protection domain that provides permission to enable
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   424
substitution.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   425
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   426
If the subclass of `ObjectInputStream` is not considered part of the system
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   427
domain, a line has to be added to the security policy file to provide to a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   428
subclass of `ObjectInputStream` permission to call `enableResolveObject`. The
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   429
`SerializablePermission` to add is `"enableSubstitution"`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   430
`AccessControlException` is thrown if the protection domain of the subclass of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   431
`ObjectStreamClass` does not have permission to `"enableSubstitution"` by
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   432
calling `enableResolveObject`. See the document Java Security Architecture (JDK
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   433
1.2) for additional information about the security model.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   434
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   435
The `readStreamHeader` method reads and verifies the magic number and version
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   436
of the stream. If they do not match, the `StreamCorruptedMismatch` is thrown.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   437
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   438
To override the implementation of deserialization, a subclass of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   439
`ObjectInputStream` should call the protected no-arg `ObjectInputStream`,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   440
constructor. There is a security check within the no-arg constructor for
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   441
`SerializablePermission "enableSubclassImplementation"` to ensure that only
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   442
trusted classes are allowed to override the default implementation. This
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   443
constructor does not allocate any private data for `ObjectInputStream` and sets
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   444
a flag that indicates that the final `readObject` method should invoke the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   445
`readObjectOverride` method and return. All other `ObjectInputStream` methods
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   446
are not final and can be directly overridden by the subclass.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   447
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   448
## 3.2 The ObjectInputStream.GetField Class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   449
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   450
The class `ObjectInputStream.GetField` provides the API for getting the values
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   451
of serializable fields. The protocol of the stream is the same as used by
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   452
`defaultReadObject.` Using `readFields` to access the serializable fields does
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   453
not change the format of the stream. It only provides an alternate API to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   454
access the values which does not require the class to have the corresponding
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   455
non-transient and non-static fields for each named serializable field. The
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   456
serializable fields are those declared using `serialPersistentFields` or if it
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   457
is not declared the non-transient and non-static fields of the object. When the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   458
stream is read the available serializable fields are those written to the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   459
stream when the object was serialized. If the class that wrote the stream is a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   460
different version not all fields will correspond to the serializable fields of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   461
the current class. The available fields can be retrieved from the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   462
`ObjectStreamClass` of the `GetField` object.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   463
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   464
The `getObjectStreamClass` method returns an `ObjectStreamClass` object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   465
representing the class in the stream. It contains the list of serializable
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   466
fields.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   467
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   468
The `defaulted` method returns *true* if the field is not present in the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   469
stream. An `IllegalArgumentException` is thrown if the requested field is not a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   470
serializable field of the current class.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   471
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   472
Each `get` method returns the specified serializable field from the stream. I/O
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   473
exceptions will be thrown if the underlying stream throws an exception. An
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   474
`IllegalArgumentException` is thrown if the name or type does not match the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   475
name and type of an field serializable field of the current class. The default
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   476
value is returned if the stream does not contain an explicit value for the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   477
field.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   478
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   479
## 3.3 The ObjectInputValidation Interface
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   480
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   481
This interface allows an object to be called when a complete graph of objects
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   482
has been deserialized. If the object cannot be made valid, it should throw the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   483
`ObjectInvalidException`. Any exception that occurs during a call to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   484
`validateObject` will terminate the validation process, and the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   485
`InvalidObjectException` will be thrown.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   486
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   487
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   488
package java.io;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   489
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   490
public interface ObjectInputValidation
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   491
{
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   492
    public void validateObject()
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   493
        throws InvalidObjectException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   494
}
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   495
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   496
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   497
## 3.4 The readObject Method
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   498
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   499
For serializable objects, the `readObject` method allows a class to control the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   500
deserialization of its own fields. Here is its signature:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   501
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   502
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   503
private void readObject(ObjectInputStream stream)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   504
    throws IOException, ClassNotFoundException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   505
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   506
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   507
Each subclass of a serializable object may define its own `readObject` method.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   508
If a class does not implement the method, the default serialization provided by
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   509
`defaultReadObject` will be used. When implemented, the class is only
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   510
responsible for restoring its own fields, not those of its supertypes or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   511
subtypes.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   512
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   513
The `readObject` method of the class, if implemented, is responsible for
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   514
restoring the state of the class. The values of every field of the object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   515
whether transient or not, static or not are set to the default value for the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   516
fields type. Either `ObjectInputStream`'s `defaultReadObject` or `readFields`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   517
method must be called once (and only once) before reading any optional data
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   518
written by the corresponding `writeObject` method; even if no optional data is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   519
read, `defaultReadObject` or `readFields` must still be invoked once. If the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   520
`readObject` method of the class attempts to read more data than is present in
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   521
the optional part of the stream for this class, the stream will return `-1` for
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   522
bytewise reads, throw an `EOFException` for primitive data reads (e.g.,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   523
`readInt`, `readFloat`), or throw an `OptionalDataException` with the `eof`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   524
field set to `true` for object reads.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   525
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   526
The responsibility for the format, structure, and versioning of the optional
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   527
data lies completely with the class. The `@serialData` javadoc tag within the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   528
javadoc comment for the `readObject` method should be used to document the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   529
format and structure of the optional data.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   530
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   531
If the class being restored is not present in the stream being read, then its
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   532
`readObjectNoData` method, if defined, is invoked (instead of `readObject`);
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   533
otherwise, its fields are initialized to the appropriate default values. For
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   534
further detail, see [Section 3.5, "The readObjectNoData
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   535
Method"](#the-readobjectnodata-method).
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   536
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   537
Reading an object from the `ObjectInputStream` is analogous to creating a new
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   538
object. Just as a new object's constructors are invoked in the order from the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   539
superclass to the subclass, an object being read from a stream is deserialized
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   540
from superclass to subclass. The `readObject` or `readObjectNoData` method is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   541
called instead of the constructor for each `Serializable` subclass during
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   542
deserialization.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   543
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   544
One last similarity between a constructor and a `readObject` method is that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   545
both provide the opportunity to invoke a method on an object that is not fully
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   546
constructed. Any overridable (neither private, static nor final) method called
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   547
while an object is being constructed can potentially be overridden by a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   548
subclass. Methods called during the construction phase of an object are
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   549
resolved by the actual type of the object, not the type currently being
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   550
initialized by either its constructor or `readObject`/`readObjectNoData`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   551
method. Therefore, calling an overridable method from within a `readObject` or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   552
`readObjectNoData` method may result in the unintentional invocation of a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   553
subclass method before the superclass has been fully initialized.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   554
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   555
## 3.5 The readObjectNoData Method
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   556
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   557
For serializable objects, the `readObjectNoData` method allows a class to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   558
control the initialization of its own fields in the event that a subclass
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   559
instance is deserialized and the serialization stream does not list the class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   560
in question as a superclass of the deserialized object. This may occur in cases
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   561
where the receiving party uses a different version of the deserialized
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   562
instance's class than the sending party, and the receiver's version extends
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   563
classes that are not extended by the sender's version. This may also occur if
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   564
the serialization stream has been tampered; hence, `readObjectNoData` is useful
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   565
for initializing deserialized objects properly despite a "hostile" or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   566
incomplete source stream.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   567
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   568
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   569
private void readObjectNoData() throws ObjectStreamException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   570
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   571
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   572
Each serializable class may define its own `readObjectNoData` method. If a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   573
serializable class does not define a `readObjectNoData` method, then in the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   574
circumstances listed above the fields of the class will be initialized to their
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   575
default values (as listed in The Java Language Specification); this behavior is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   576
consistent with that of `ObjectInputStream` prior to version 1.4 of the Java 2
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   577
SDK, Standard Edition, when support for `readObjectNoData` methods was
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   578
introduced. If a serializable class does define a `readObjectNoData` method and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   579
the aforementioned conditions arise, then `readObjectNoData` will be invoked at
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   580
the point during deserialization when a class-defined `readObject` method would
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   581
otherwise be called had the class in question been listed by the stream as a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   582
superclass of the instance being deserialized.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   583
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   584
## 3.6 The readExternal Method
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   585
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   586
Objects implementing `java.io.Externalizable` must implement the `readExternal`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   587
method to restore the entire state of the object. It must coordinate with its
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   588
superclasses to restore their state. All of the methods of `ObjectInput` are
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   589
available to restore the object's primitive typed fields and object fields.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   590
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   591
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   592
public void readExternal(ObjectInput stream)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   593
    throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   594
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   595
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   596
**Note:** The `readExternal` method is public, and it raises the risk of a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   597
client being able to overwrite an existing object from a stream. The class may
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   598
add its own checks to insure that this is only called when appropriate.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   599
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   600
A new stream protocol version has been introduced in JDK 1.2 to correct a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   601
problem with `Externalizable` objects. The old definition of `Externalizable`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   602
objects required the local virtual machine to find a `readExternal` method to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   603
be able to properly read an `Externalizable` object from the stream. The new
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   604
format adds enough information to the stream protocol so serialization can skip
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   605
an `Externalizable` object when the local `readExternal` method is not
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   606
available. Due to class evolution rules, serialization must be able to skip an
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   607
`Externalizable` object in the input stream if there is not a mapping for the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   608
object using the local classes.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   609
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   610
An additional benefit of the new `Externalizable` stream format is that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   611
`ObjectInputStream` can detect attempts to read more External data than is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   612
available, and can also skip by any data that is left unconsumed by a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   613
`readExternal` method. The behavior of `ObjectInputStream` in response to a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   614
read past the end of External data is the same as the behavior when a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   615
class-defined `readObject` method attempts to read past the end of its optional
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   616
data: bytewise reads will return `-1`, primitive reads will throw
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   617
`EOFException`s, and object reads will throw `OptionalDataException`s with the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   618
`eof` field set to `true`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   619
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   620
Due to the format change, JDK 1.1.6 and earlier releases are not able to read
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   621
the new format. `StreamCorruptedException` is thrown when JDK 1.1.6 or earlier
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   622
attempts to read an `Externalizable` object from a stream written in
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   623
`PROTOCOL_VERSION_2`. Compatibility issues are discussed in more detail in
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   624
[Section 6.3, "Stream Protocol
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   625
Versions"](protocol.html#stream-protocol-versions).
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   626
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   627
## 3.7 The readResolve Method
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   628
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   629
For Serializable and Externalizable classes, the `readResolve` method allows a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   630
class to replace/resolve the object read from the stream before it is returned
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   631
to the caller. By implementing the `readResolve` method, a class can directly
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   632
control the types and instances of its own instances being deserialized. The
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   633
method is defined as follows:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   634
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   635
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   636
ANY-ACCESS-MODIFIER Object readResolve()
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   637
            throws ObjectStreamException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   638
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   639
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   640
The `readResolve` method is called when `ObjectInputStream` has read an object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   641
from the stream and is preparing to return it to the caller.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   642
`ObjectInputStream` checks whether the class of the object defines the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   643
`readResolve` method. If the method is defined, the `readResolve` method is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   644
called to allow the object in the stream to designate the object to be
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   645
returned. The object returned should be of a type that is compatible with all
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   646
uses. If it is not compatible, a `ClassCastException` will be thrown when the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   647
type mismatch is discovered.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   648
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   649
For example, a `Symbol` class could be created for which only a single instance
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   650
of each symbol binding existed within a virtual machine. The `readResolve`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   651
method would be implemented to determine if that symbol was already defined and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   652
substitute the preexisting equivalent `Symbol` object to maintain the identity
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   653
constraint. In this way the uniqueness of `Symbol` objects can be maintained
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   654
across serialization.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   655
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   656
**Note:** The `readResolve` method is not invoked on the object until the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   657
object is fully constructed, so any references to this object in its object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   658
graph will not be updated to the new object nominated by `readResolve`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   659
However, during the serialization of an object with the `writeReplace` method,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   660
all references to the original object in the replacement object's object graph
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   661
are replaced with references to the replacement object. Therefore in cases
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   662
where an object being serialized nominates a replacement object whose object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   663
graph has a reference to the original object, deserialization will result in an
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   664
incorrect graph of objects. Furthermore, if the reference types of the object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   665
being read (nominated by `writeReplace`) and the original object are not
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   666
compatible, the construction of the object graph will raise a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   667
`ClassCastException`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   668
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   669
-------------------------------------------------------------------------------
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   670
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   671
*[Copyright](../../../legal/SMICopyright.html) © 2005, 2017, Oracle
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   672
and/or its affiliates. All rights reserved.*