Path: blob/master/src/java.base/share/classes/java/io/Externalizable.java
41152 views
/*1* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627import java.io.ObjectOutput;28import java.io.ObjectInput;2930/**31* Only the identity of the class of an Externalizable instance is32* written in the serialization stream and it is the responsibility33* of the class to save and restore the contents of its instances.34*35* The writeExternal and readExternal methods of the Externalizable36* interface are implemented by a class to give the class complete37* control over the format and contents of the stream for an object38* and its supertypes. These methods must explicitly39* coordinate with the supertype to save its state. These methods supersede40* customized implementations of writeObject and readObject methods.<br>41*42* Object Serialization uses the Serializable and Externalizable43* interfaces. Object persistence mechanisms can use them as well. Each44* object to be stored is tested for the Externalizable interface. If45* the object supports Externalizable, the writeExternal method is called. If the46* object does not support Externalizable and does implement47* Serializable, the object is saved using48* ObjectOutputStream. <br> When an Externalizable object is49* reconstructed, an instance is created using the public no-arg50* constructor, then the readExternal method called. Serializable51* objects are restored by reading them from an ObjectInputStream.<br>52*53* An Externalizable instance can designate a substitution object via54* the writeReplace and readResolve methods documented in the Serializable55* interface.<br>56*57* @see java.io.ObjectOutputStream58* @see java.io.ObjectInputStream59* @see java.io.ObjectOutput60* @see java.io.ObjectInput61* @see java.io.Serializable62* @since 1.163*/64public interface Externalizable extends java.io.Serializable {65/**66* The object implements the writeExternal method to save its contents67* by calling the methods of DataOutput for its primitive values or68* calling the writeObject method of ObjectOutput for objects, strings,69* and arrays.70*71* @serialData Overriding methods should use this tag to describe72* the data layout of this Externalizable object.73* List the sequence of element types and, if possible,74* relate the element to a public/protected field and/or75* method of this Externalizable class.76*77* @param out the stream to write the object to78* @throws IOException Includes any I/O exceptions that may occur79*/80void writeExternal(ObjectOutput out) throws IOException;8182/**83* The object implements the readExternal method to restore its84* contents by calling the methods of DataInput for primitive85* types and readObject for objects, strings and arrays. The86* readExternal method must read the values in the same sequence87* and with the same types as were written by writeExternal.88*89* @param in the stream to read data from in order to restore the object90* @throws IOException if I/O errors occur91* @throws ClassNotFoundException If the class for an object being92* restored cannot be found.93*/94void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;95}969798