jdk/src/java.base/share/specs/serialization/serial-arch.md
author rriggs
Tue, 16 May 2017 09:42:38 -0400
changeset 45142 be2c212f16dd
parent 44787 0b323ea6d5ad
permissions -rw-r--r--
8180319: Update Serialization spec to omit obsolete serialver -show and change history Reviewed-by: chegar
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
45142
be2c212f16dd 8180319: Update Serialization spec to omit obsolete serialver -show and change history
rriggs
parents: 44787
diff changeset
    23
include-before: '[CONTENTS](index.html) | [PREV](index.html) | [NEXT](output.html)'
be2c212f16dd 8180319: Update Serialization spec to omit obsolete serialver -show and change history
rriggs
parents: 44787
diff changeset
    24
include-after: '[CONTENTS](index.html) | [PREV](index.html) | [NEXT](output.html)'
44787
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: 1 - System Architecture'
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
-   [Overview](#overview)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    30
-   [Writing to an Object Stream](#writing-to-an-object-stream)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    31
-   [Reading from an Object Stream](#reading-from-an-object-stream)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    32
-   [Object Streams as Containers](#object-streams-as-containers)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    33
-   [Defining Serializable Fields for a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    34
    Class](#defining-serializable-fields-for-a-class)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    35
-   [Documenting Serializable Fields and Data for a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    36
    Class](#documenting-serializable-fields-and-data-for-a-class)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    37
-   [Accessing Serializable Fields of a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    38
    Class](#accessing-serializable-fields-of-a-class)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    39
-   [The ObjectOutput Interface](#the-objectoutput-interface)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    40
-   [The ObjectInput Interface](#the-objectinput-interface)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    41
-   [The Serializable Interface](#the-serializable-interface)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    42
-   [The Externalizable Interface](#the-externalizable-interface)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    43
-   [Serialization of Enum Constants](#serialization-of-enum-constants)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    44
-   [Protecting Sensitive Information](#protecting-sensitive-information)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    45
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
## 1.1 Overview
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    49
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    50
The ability to store and retrieve Java^TM^ objects is essential to building all
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    51
but the most transient applications. The key to storing and retrieving objects
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    52
in a serialized form is representing the state of objects sufficient to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    53
reconstruct the object(s). Objects to be saved in the stream may support either
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    54
the `Serializable` or the `Externalizable` interface. For Java^TM^ objects, the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    55
serialized form must be able to identify and verify the Java^TM^ class from
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    56
which the contents of the object were saved and to restore the contents to a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    57
new instance. For serializable objects, the stream includes sufficient
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    58
information to restore the fields in the stream to a compatible version of the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    59
class. For Externalizable objects, the class is solely responsible for the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    60
external format of its contents.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    61
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    62
Objects to be stored and retrieved frequently refer to other objects. Those
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    63
other objects must be stored and retrieved at the same time to maintain the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    64
relationships between the objects. When an object is stored, all of the objects
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    65
that are reachable from that object are stored as well.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    66
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    67
The goals for serializing Java^TM^ objects are to:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    68
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    69
-   Have a simple yet extensible mechanism.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    70
-   Maintain the Java^TM^ object type and safety properties in the serialized
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    71
    form.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    72
-   Be extensible to support marshaling and unmarshaling as needed for remote
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    73
    objects.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    74
-   Be extensible to support simple persistence of Java^TM^ objects.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    75
-   Require per class implementation only for customization.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    76
-   Allow the object to define its external format.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    77
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    78
## 1.2 Writing to an Object Stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    79
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    80
Writing objects and primitives to a stream is a straightforward process. For
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    81
example:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    82
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    83
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    84
// Serialize today's date to a file.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    85
    FileOutputStream f = new FileOutputStream("tmp");
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    86
    ObjectOutput s = new ObjectOutputStream(f);
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    87
    s.writeObject("Today");
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    88
    s.writeObject(new Date());
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    89
    s.flush();
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    90
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    91
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    92
First an `OutputStream`, in this case a `FileOutputStream`, is needed to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    93
receive the bytes. Then an `ObjectOutputStream` is created that writes to the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    94
`FileOutputStream`. Next, the string "Today" and a Date object are written to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    95
the stream. More generally, objects are written with the `writeObject` method
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    96
and primitives are written to the stream with the methods of `DataOutput`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    97
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    98
The `writeObject` method (see [Section 2.3, "The writeObject
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
    99
Method"](output.html#the-writeobject-method)) serializes the specified object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   100
and traverses its references to other objects in the object graph recursively
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   101
to create a complete serialized representation of the graph. Within a stream,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   102
the first reference to any object results in the object being serialized or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   103
externalized and the assignment of a handle for that object. Subsequent
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   104
references to that object are encoded as the handle. Using object handles
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   105
preserves sharing and circular references that occur naturally in object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   106
graphs. Subsequent references to an object use only the handle allowing a very
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   107
compact representation.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   108
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   109
Special handling is required for arrays, enum constants, and objects of type
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   110
`Class`, `ObjectStreamClass`, and `String`. Other objects must implement either
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   111
the `Serializable` or the `Externalizable` interface to be saved in or restored
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   112
from a stream.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   113
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   114
Primitive data types are written to the stream with the methods in the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   115
`DataOutput` interface, such as `writeInt`, `writeFloat`, or `writeUTF`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   116
Individual bytes and arrays of bytes are written with the methods of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   117
`OutputStream`. Except for serializable fields, primitive data is written to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   118
the stream in block-data records, with each record prefixed by a marker and an
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   119
indication of the number of bytes in the record.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   120
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   121
`ObjectOutputStream` can be extended to customize the information about classes
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   122
in the stream or to replace objects to be serialized. Refer to the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   123
`annotateClass` and `replaceObject` method descriptions for details.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   124
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   125
## 1.3 Reading from an Object Stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   126
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   127
Reading an object from a stream, like writing, is straightforward:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   128
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   129
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   130
// Deserialize a string and date from a file.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   131
    FileInputStream in = new FileInputStream("tmp");
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   132
    ObjectInputStream s = new ObjectInputStream(in);
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   133
    String today = (String)s.readObject();
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   134
    Date date = (Date)s.readObject();
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   135
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   136
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   137
First an `InputStream`, in this case a `FileInputStream`, is needed as the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   138
source stream. Then an `ObjectInputStream` is created that reads from the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   139
`InputStream`. Next, the string "Today" and a Date object are read from the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   140
stream. Generally, objects are read with the `readObject` method and primitives
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   141
are read from the stream with the methods of `DataInput`.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   142
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   143
The `readObject` method deserializes the next object in the stream and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   144
traverses its references to other objects recursively to create the complete
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   145
graph of objects serialized.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   146
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   147
Primitive data types are read from the stream with the methods in the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   148
`DataInput` interface, such as `readInt`, `readFloat`, or `readUTF`. Individual
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   149
bytes and arrays of bytes are read with the methods of `InputStream`. Except
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   150
for serializable fields, primitive data is read from block-data records.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   151
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   152
`ObjectInputStream` can be extended to utilize customized information in the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   153
stream about classes or to replace objects that have been deserialized. Refer
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   154
to the `resolveClass` and `resolveObject` method descriptions for details.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   155
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   156
## 1.4 Object Streams as Containers
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   157
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   158
Object Serialization produces and consumes a stream of bytes that contain one
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   159
or more primitives and objects. The objects written to the stream, in turn,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   160
refer to other objects, which are also represented in the stream. Object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   161
Serialization produces just one stream format that encodes and stores the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   162
contained objects.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   163
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   164
Each object that acts as a container implements an interface which allows
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   165
primitives and objects to be stored in or retrieved from it. These interfaces
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   166
are the `ObjectOutput` and `ObjectInput` interfaces which:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   167
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   168
-   Provide a stream to write to and to read from
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   169
-   Handle requests to write primitive types and objects to the stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   170
-   Handle requests to read primitive types and objects from the stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   171
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   172
Each object which is to be stored in a stream must explicitly allow itself to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   173
be stored and must implement the protocols needed to save and restore its
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   174
state. Object Serialization defines two such protocols. The protocols allow the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   175
container to ask the object to write and read its state.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   176
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   177
To be stored in an Object Stream, each object must implement either the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   178
`Serializable` or the `Externalizable` interface:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   179
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   180
-   For a `Serializable` class, Object Serialization can automatically save and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   181
    restore fields of each class of an object and automatically handle classes
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   182
    that evolve by adding fields or supertypes. A serializable class can
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   183
    declare which of its fields are saved or restored, and write and read
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   184
    optional values and objects.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   185
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   186
-   For an `Externalizable` class, Object Serialization delegates to the class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   187
    complete control over its external format and how the state of the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   188
    supertype(s) is saved and restored.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   189
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   190
## 1.5 Defining Serializable Fields for a Class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   191
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   192
The serializable fields of a class can be defined two different ways. Default
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   193
serializable fields of a class are defined to be the non-transient and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   194
non-static fields. This default computation can be overridden by declaring a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   195
special field in the `Serializable` class, `serialPersistentFields`. This field
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   196
must be initialized with an array of `ObjectStreamField` objects that list the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   197
names and types of the serializable fields. The modifiers for the field are
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   198
required to be private, static, and final. If the field's value is null or is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   199
otherwise not an instance of `ObjectStreamField[]`, or if the field does not
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   200
have the required modifiers, then the behavior is as if the field were not
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   201
declared at all.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   202
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   203
For example, the following declaration duplicates the default behavior.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   204
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   205
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   206
class List implements Serializable {
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   207
    List next;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   208
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   209
    private static final ObjectStreamField[] serialPersistentFields
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   210
                 = {new ObjectStreamField("next", List.class)};
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   211
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   212
}
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   213
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   214
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   215
By using `serialPersistentFields` to define the Serializable fields for a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   216
class, there no longer is a limitation that a serializable field must be a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   217
field within the current definition of the `Serializable` class. The
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   218
`writeObject` and `readObject` methods of the `Serializable` class can map the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   219
current implementation of the class to the serializable fields of the class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   220
using the interface that is described in [Section 1.7, "Accessing Serializable
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   221
Fields of a Class"](#accessing-serializable-fields-of-a-class). Therefore, the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   222
fields for a `Serializable` class can change in a later release, as long as it
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   223
maintains the mapping back to its Serializable fields that must remain
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   224
compatible across release boundaries.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   225
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   226
**Note:** There is, however, a limitation to the use of this mechanism to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   227
specify serializable fields for inner classes. Inner classes can only contain
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   228
final static fields that are initialized to constants or expressions built up
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   229
from constants. Consequently, it is not possible to set
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   230
`serialPersistentFields` for an inner class (though it is possible to set it
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   231
for static member classes). For other restrictions pertaining to serialization
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   232
of inner class instances, see section [Section 1.10, "The Serializable
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   233
Interface"](#the-serializable-interface).
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   234
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   235
## 1.6 Documenting Serializable Fields and Data for a Class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   236
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   237
It is important to document the serializable state of a class to enable
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   238
interoperability with alternative implementations of a Serializable class and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   239
to document class evolution. Documenting a serializable field gives one a final
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   240
opportunity to review whether or not the field should be serializable. The
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   241
serialization javadoc tags, `@serial`, `@serialField`, and `@serialData`,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   242
provide a way to document the serialized form for a Serializable class within
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   243
the source code.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   244
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   245
-   The `@serial` tag should be placed in the javadoc comment for a default
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   246
    serializable field. The syntax is as follows: `@serial` *field-description*
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   247
    The optional *field-description* describes the meaning of the field and its
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   248
    acceptable values. The *field-description* can span multiple lines. When a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   249
    field is added after the initial release, a *@since* tag indicates the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   250
    version the field was added. The *field-description* for `@serial` provides
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   251
    serialization-specific documentation and is appended to the javadoc comment
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   252
    for the field within the serialized form documentation.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   253
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   254
-   The `@serialField` tag is used to document an `ObjectStreamField` component
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   255
    of a `serialPersistentFields` array. One of these tags should be used for
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   256
    each `ObjectStreamField` component. The syntax is as follows:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   257
    `@serialField` *field-name field-type field-description*
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   258
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   259
-   The `@serialData` tag describes the sequences and types of data written or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   260
    read. The tag describes the sequence and type of optional data written by
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   261
    `writeObject` or all data written by the `Externalizable.writeExternal`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   262
    method. The syntax is as follows: `@serialData` *data-description*
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   263
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   264
The javadoc application recognizes the serialization javadoc tags and generates
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   265
a specification for each Serializable and Externalizable class. See [Section
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   266
C.1, "Example Alternate Implementation of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   267
java.io.File"](examples.html#c.1-example-alternate-implementation-of-java.io.file)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   268
for an example that uses these tags.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   269
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   270
When a class is declared Serializable, the serializable state of the object is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   271
defined by serializable fields (by name and type) plus optional data. Optional
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   272
data can only be written explicitly by the `writeObject` method of a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   273
`Serializable` class. Optional data can be read by the `Serializable` class'
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   274
`readObject` method or serialization will skip unread optional data.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   275
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   276
When a class is declared Externalizable, the data that is written to the stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   277
by the class itself defines the serialized state. The class must specify the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   278
order, types, and meaning of each datum that is written to the stream. The
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   279
class must handle its own evolution, so that it can continue to read data
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   280
written by and write data that can be read by previous versions. The class must
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   281
coordinate with the superclass when saving and restoring data. The location of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   282
the superclasses data in the stream must be specified.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   283
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   284
The designer of a Serializable class must ensure that the information saved for
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   285
the class is appropriate for persistence and follows the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   286
serialization-specified rules for interoperability and evolution. Class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   287
evolution is explained in greater detail in [Chapter
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   288
5](version.html#versioning-of-serializable-objects), "Versioning of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   289
Serializable Objects".
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   290
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   291
## 1.7 Accessing Serializable Fields of a Class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   292
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   293
Serialization provides two mechanisms for accessing the serializable fields in
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   294
a stream:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   295
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   296
-   The default mechanism requires no customization
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   297
-   The Serializable Fields API allows a class to explicitly access/set the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   298
    serializable fields by name and type
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   299
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   300
The default mechanism is used automatically when reading or writing objects
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   301
that implement the `Serializable` interface and do no further customization.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   302
The serializable fields are mapped to the corresponding fields of the class and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   303
values are either written to the stream from those fields or are read in and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   304
assigned respectively. If the class provides `writeObject` and `readObject`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   305
methods, the default mechanism can be invoked by calling `defaultWriteObject`
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   306
and `defaultReadObject`. When the `writeObject` and `readObject` methods are
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   307
implemented, the class has an opportunity to modify the serializable field
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   308
values before they are written or after they are read.
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 default mechanism cannot be used, the serializable class can use the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   311
`putFields` method of `ObjectOutputStream` to put the values for the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   312
serializable fields into the stream. The `writeFields` method of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   313
`ObjectOutputStream` puts the values in the correct order, then writes them to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   314
the stream using the existing protocol for serialization. Correspondingly, the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   315
`readFields` method of `ObjectInputStream` reads the values from the stream and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   316
makes them available to the class by name in any order. See [Section 2.2, "The
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   317
ObjectOutputStream.PutField
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   318
Class"](output.html#the-objectoutputstream.putfield-class) and [Section 3.2,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   319
"The ObjectInputStream.GetField
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   320
Class"](input.html#the-objectinputstream.getfield-class) for a detailed
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   321
description of the Serializable Fields API.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   322
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   323
## 1.8 The ObjectOutput Interface
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   324
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   325
The `ObjectOutput` interface provides an abstract, stream-based interface to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   326
object storage. It extends the DataOutput interface so those methods can be
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   327
used for writing primitive data types. Objects that implement this interface
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   328
can be used to store primitives and objects.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   329
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   330
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   331
package java.io;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   332
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   333
public interface ObjectOutput extends DataOutput
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   334
{
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   335
    public void writeObject(Object obj) throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   336
    public void write(int b) throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   337
    public void write(byte b[]) throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   338
    public void write(byte b[], int off, int len) throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   339
    public void flush() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   340
    public void close() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   341
}
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   342
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   343
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   344
`The` `writeObject` method is used to write an object. The exceptions thrown
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   345
reflect errors while accessing the object or its fields, or exceptions that
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   346
occur in writing to storage. If any exception is thrown, the underlying storage
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   347
may be corrupted. If this occurs, refer to the object that is implementing this
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   348
interface for more information.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   349
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   350
## 1.9 The ObjectInput Interface
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   351
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   352
The `ObjectInput` interface provides an abstract stream based interface to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   353
object retrieval. It extends the `DataInput` interface so those methods for
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   354
reading primitive data types are accessible in this interface.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   355
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   356
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   357
package java.io;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   358
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   359
public interface ObjectInput extends DataInput
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   360
{
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   361
    public Object readObject()
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   362
        throws ClassNotFoundException, IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   363
    public int read() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   364
    public int read(byte b[]) throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   365
    public int read(byte b[], int off, int len) throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   366
    public long skip(long n) throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   367
    public int available() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   368
    public void close() throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   369
}
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   370
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   371
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   372
The `readObject` method is used to read and return an object. The exceptions
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   373
thrown reflect errors while accessing the objects or its fields or exceptions
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   374
that occur in reading from the storage. If any exception is thrown, the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   375
underlying storage may be corrupted. If this occurs, refer to the object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   376
implementing this interface for additional information.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   377
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   378
## 1.10 The Serializable Interface
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   379
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   380
Object Serialization produces a stream with information about the Java^TM^
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   381
classes for the objects which are being saved. For serializable objects,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   382
sufficient information is kept to restore those objects even if a different
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   383
(but compatible) version of the implementation of the class is present. The
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   384
`Serializable` interface is defined to identify classes which implement the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   385
serializable protocol:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   386
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   387
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   388
package java.io;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   389
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   390
public interface Serializable {};
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   391
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   392
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   393
A Serializable class must do the following:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   394
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   395
-   Implement the `java.io.Serializable` interface
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   396
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   397
-   Identify the fields that should be serializable
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   398
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   399
    (Use the `serialPersistentFields` member to explicitly declare them
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   400
    serializable or use the transient keyword to denote nonserializable
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   401
    fields.)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   402
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   403
-   Have access to the no-arg constructor of its first nonserializable
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   404
    superclass
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   405
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   406
The class can optionally define the following methods:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   407
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   408
-   A `writeObject` method to control what information is saved or to append
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   409
    additional information to the stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   410
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   411
-   A `readObject` method either to read the information written by the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   412
    corresponding `writeObject` method or to update the state of the object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   413
    after it has been restored
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   414
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   415
-   A `writeReplace` method to allow a class to nominate a replacement object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   416
    to be written to the stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   417
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   418
    (See [Section 2.5, "The writeReplace
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   419
    Method"](output.html#the-writereplace-method) for additional information.)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   420
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   421
-   A `readResolve` method to allow a class to designate a replacement object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   422
    for the object just read from the stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   423
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   424
    (See [Section 3.7, "The readResolve
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   425
    Method](input.html#the-readresolve-method) for additional information.)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   426
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   427
`ObjectOutputStream` and `ObjectInputStream` allow the serializable classes on
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   428
which they operate to evolve (allow changes to the classes that are compatible
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   429
with the earlier versions of the classes). See [Section 5.5, "Compatible Java
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   430
Type Evolution"](version.html#compatible-java-type-evolution) for information
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   431
about the mechanism which is used to allow compatible changes.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   432
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   433
**Note:** Serialization of inner classes (i.e., nested classes that are not
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   434
static member classes), including local and anonymous classes, is strongly
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   435
discouraged for several reasons. Because inner classes declared in non-static
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   436
contexts contain implicit non-transient references to enclosing class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   437
instances, serializing such an inner class instance will result in
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   438
serialization of its associated outer class instance as well. Synthetic fields
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   439
generated by `javac` (or other Java^TM^ compilers) to implement inner classes
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   440
are implementation dependent and may vary between compilers; differences in
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   441
such fields can disrupt compatibility as well as result in conflicting default
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   442
`serialVersionUID` values. The names assigned to local and anonymous inner
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   443
classes are also implementation dependent and may differ between compilers.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   444
Since inner classes cannot declare static members other than compile-time
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   445
constant fields, they cannot use the `serialPersistentFields` mechanism to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   446
designate serializable fields. Finally, because inner classes associated with
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   447
outer instances do not have zero-argument constructors (constructors of such
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   448
inner classes implicitly accept the enclosing instance as a prepended
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   449
parameter), they cannot implement `Externalizable`. None of the issues listed
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   450
above, however, apply to static member classes.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   451
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   452
## 1.11 The Externalizable Interface
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   453
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   454
For Externalizable objects, only the identity of the class of the object is
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   455
saved by the container; the class must save and restore the contents. The
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   456
`Externalizable` interface is defined as follows:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   457
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   458
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   459
package java.io;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   460
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   461
public interface Externalizable extends Serializable
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   462
{
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   463
    public void writeExternal(ObjectOutput out)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   464
        throws IOException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   465
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   466
    public void readExternal(ObjectInput in)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   467
        throws IOException, java.lang.ClassNotFoundException;
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   468
}
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   469
```
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   470
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   471
The class of an Externalizable object must do the following:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   472
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   473
-   Implement the `java.io.Externalizable` interface
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   474
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   475
-   Implement a `writeExternal` method to save the state of the object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   476
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   477
    (It must explicitly coordinate with its supertype to save its state.)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   478
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   479
-   Implement a `readExternal` method to read the data written by the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   480
    `writeExternal` method from the stream and restore the state of the object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   481
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   482
    (It must explicitly coordinate with the supertype to save its state.)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   483
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   484
-   Have the `writeExternal` and `readExternal` methods be solely responsible
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   485
    for the format, if an externally defined format is written
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   486
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   487
    **Note:** The `writeExternal` and `readExternal` methods are public and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   488
    raise the risk that a client may be able to write or read information in
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   489
    the object other than by using its methods and fields. These methods must
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   490
    be used only when the information held by the object is not sensitive or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   491
    when exposing it does not present a security risk.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   492
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   493
-   Have a public no-arg constructor
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   494
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   495
    **Note:** Inner classes associated with enclosing instances cannot have
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   496
    no-arg constructors, since constructors of such classes implicitly accept
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   497
    the enclosing instance as a prepended parameter. Consequently the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   498
    `Externalizable` interface mechanism cannot be used for inner classes and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   499
    they should implement the `Serializable` interface, if they must be
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   500
    serialized. Several limitations exist for serializable inner classes as
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   501
    well, however; see [Section 1.10, "The Serializable
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   502
    Interface"](#the-serializable-interface), for a full enumeration.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   503
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   504
An Externalizable class can optionally define the following methods:
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   505
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   506
-   A `writeReplace` method to allow a class to nominate a replacement object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   507
    to be written to the stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   508
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   509
    (See [Section 2.5, "The writeReplace
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   510
    Method"](output.html#the-writereplace-method) for additional information.)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   511
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   512
-   A `readResolve` method to allow a class to designate a replacement object
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   513
    for the object just read from the stream
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   514
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   515
    (See [Section 3.7, "The readResolve
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   516
    Method"](input.html#the-readresolve-method) for additional information.)
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   517
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   518
## 1.12 Serialization of Enum Constants
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   519
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   520
Enum constants are serialized differently than ordinary serializable or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   521
externalizable objects. The serialized form of an enum constant consists solely
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   522
of its name; field values of the constant are not present in the form. To
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   523
serialize an enum constant, `ObjectOutputStream` writes the value returned by
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   524
the enum constant's `name` method. To deserialize an enum constant,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   525
`ObjectInputStream` reads the constant name from the stream; the deserialized
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   526
constant is then obtained by calling the `java.lang.Enum.valueOf` method,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   527
passing the constant's enum type along with the received constant name as
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   528
arguments. Like other serializable or externalizable objects, enum constants
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   529
can function as the targets of back references appearing subsequently in the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   530
serialization stream.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   531
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   532
The process by which enum constants are serialized cannot be customized: any
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   533
class-specific `writeObject`, `readObject`, `readObjectNoData`, `writeReplace`,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   534
and `readResolve` methods defined by enum types are ignored during
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   535
serialization and deserialization. Similarly, any `serialPersistentFields` or
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   536
`serialVersionUID` field declarations are also ignored--all enum types have a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   537
fixed `serialVersionUID` of `0L`. Documenting serializable fields and data for
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   538
enum types is unnecessary, since there is no variation in the type of data
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   539
sent.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   540
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   541
## 1.13 Protecting Sensitive Information
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   542
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   543
When developing a class that provides controlled access to resources, care must
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   544
be taken to protect sensitive information and functions. During
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   545
deserialization, the private state of the object is restored. For example, a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   546
file descriptor contains a handle that provides access to an operating system
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   547
resource. Being able to forge a file descriptor would allow some forms of
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   548
illegal access, since restoring state is done from a stream. Therefore, the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   549
serializing runtime must take the conservative approach and not trust the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   550
stream to contain only valid representations of objects. To avoid compromising
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   551
a class, the sensitive state of an object must not be restored from the stream,
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   552
or it must be reverified by the class. Several techniques are available to
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   553
protect sensitive data in classes.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   554
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   555
The easiest technique is to mark fields that contain sensitive data as *private
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   556
transient*. Transient fields are not persistent and will not be saved by any
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   557
persistence mechanism. Marking the field will prevent the state from appearing
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   558
in the stream and from being restored during deserialization. Since writing and
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   559
reading (of private fields) cannot be superseded outside of the class, the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   560
transient fields of the class are safe.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   561
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   562
Particularly sensitive classes should not be serialized at all. To accomplish
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   563
this, the object should not implement either the `Serializable` or the
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   564
`Externalizable` interface.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   565
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   566
Some classes may find it beneficial to allow writing and reading but
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   567
specifically handle and revalidate the state as it is deserialized. The class
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   568
should implement `writeObject` and `readObject` methods to save and restore
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   569
only the appropriate state. If access should be denied, throwing a
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   570
`NotSerializableException` will prevent further access.
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   571
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   572
-------------------------------------------------------------------------------
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   573
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   574
*[Copyright](../../../legal/SMICopyright.html) © 2005, 2017, Oracle
0b323ea6d5ad 8179022: Add serialization spec as markdown
ihse
parents:
diff changeset
   575
and/or its affiliates. All rights reserved.*