1. /*
  2. * @(#)Externalizable.java 1.17 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.io;
  8. import java.io.ObjectOutput;
  9. import java.io.ObjectInput;
  10. /**
  11. * Only the identity of the class of an Externalizable instance is
  12. * written in the serialization stream and it is the responsibility
  13. * of the class to save and restore the contents of its instances.
  14. *
  15. * The writeExternal and readExternal methods of the Externalizable
  16. * interface are implemented by a class to give the class complete
  17. * control over the format and contents of the stream for an object
  18. * and its supertypes. These methods must explicitly
  19. * coordinate with the supertype to save its state. These methods supercede
  20. * customized implementations of writeObject and readObject methods.<br>
  21. *
  22. * Object Serialization uses the Serializable and Externalizable
  23. * interfaces. Object persistence mechanisms can use them as well. Each
  24. * object to be stored is tested for the Externalizable interface. If
  25. * the object supports Externalizable, the writeExternal method is called. If the
  26. * object does not support Externalizable and does implement
  27. * Serializable, the object is saved using
  28. * ObjectOutputStream. <br> When an Externalizable object is
  29. * reconstructed, an instance is created using the public no-arg
  30. * constructor, then the readExternal method called. Serializable
  31. * objects are restored by reading them from an ObjectInputStream.<br>
  32. *
  33. * An Externalizable instance can designate a substitution object via
  34. * the writeReplace and readResolve methods documented in the Serializable
  35. * interface.<br>
  36. *
  37. * @author unascribed
  38. * @version 1.17, 01/23/03
  39. * @see java.io.ObjectOutputStream
  40. * @see java.io.ObjectInputStream
  41. * @see java.io.ObjectOutput
  42. * @see java.io.ObjectInput
  43. * @see java.io.Serializable
  44. * @since JDK1.1
  45. */
  46. public interface Externalizable extends java.io.Serializable {
  47. /**
  48. * The object implements the writeExternal method to save its contents
  49. * by calling the methods of DataOutput for its primitive values or
  50. * calling the writeObject method of ObjectOutput for objects, strings,
  51. * and arrays.
  52. *
  53. * @serialData Overriding methods should use this tag to describe
  54. * the data layout of this Externalizable object.
  55. * List the sequence of element types and, if possible,
  56. * relate the element to a public/protected field and/or
  57. * method of this Externalizable class.
  58. *
  59. * @param out the stream to write the object to
  60. * @exception IOException Includes any I/O exceptions that may occur
  61. */
  62. void writeExternal(ObjectOutput out) throws IOException;
  63. /**
  64. * The object implements the readExternal method to restore its
  65. * contents by calling the methods of DataInput for primitive
  66. * types and readObject for objects, strings and arrays. The
  67. * readExternal method must read the values in the same sequence
  68. * and with the same types as were written by writeExternal.
  69. *
  70. * @param in the stream to read data from in order to restore the object
  71. * @exception IOException if I/O errors occur
  72. * @exception ClassNotFoundException If the class for an object being
  73. * restored cannot be found.
  74. */
  75. void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;
  76. }