Externalizable.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Externalizable.java -- Interface for saving and restoring object data
  2. Copyright (C) 1998 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. /**
  33. * This interface provides a way that classes can completely control how
  34. * the data of their object instances are written and read to and from
  35. * streams. It has two methods which are used to write the data to a stream
  36. * and to read the data from a stream. The read method must read the data
  37. * in exactly the way it was written by the write method.
  38. * <p>
  39. * Note that classes which implement this interface must take into account
  40. * that all superclass data must also be written to the stream as well.
  41. * The class implementing this interface must figure out how to make that
  42. * happen.
  43. * <p>
  44. * This interface can be used to provide object persistence. When an
  45. * object is to be stored externally, the <code>writeExternal</code> method is
  46. * called to save state. When the object is restored, an instance is
  47. * created using the default no-argument constructor and the
  48. * <code>readExternal</code> method is used to restore the state.
  49. *
  50. * @author Aaron M. Renn (arenn@urbanophile.com)
  51. */
  52. public interface Externalizable extends Serializable
  53. {
  54. /**
  55. * This method restores an object's state by reading in the instance data
  56. * for the object from the passed in stream. Note that this stream is not
  57. * a subclass of <code>InputStream</code>, but rather is a class that
  58. * implements
  59. * the <code>ObjectInput</code> interface. That interface provides a
  60. * mechanism for
  61. * reading in Java data types from a stream.
  62. * <p>
  63. * Note that this method must be compatible with <code>writeExternal</code>.
  64. * It must read back the exact same types that were written by that
  65. * method in the exact order they were written.
  66. * <p>
  67. * If this method needs to read back an object instance, then the class
  68. * for that object must be found and loaded. If that operation fails,
  69. * then this method throws a <code>ClassNotFoundException</code>
  70. *
  71. * @param in An <code>ObjectInput</code> instance for reading in the object
  72. * state
  73. *
  74. * @exception ClassNotFoundException If the class of an object being
  75. * restored cannot be found
  76. * @exception IOException If any other error occurs
  77. */
  78. void readExternal(ObjectInput in)
  79. throws ClassNotFoundException, IOException;
  80. /**
  81. * This method is responsible for writing the instance data of an object
  82. * to the passed in stream. Note that this stream is not a subclass of
  83. * <code>OutputStream</code>, but rather is a class that implements the
  84. * <code>ObjectOutput</code> interface. That interface provides a
  85. * number of methods
  86. * for writing Java data values to a stream.
  87. * <p>
  88. * Not that the implementation of this method must be coordinated with
  89. * the implementation of <code>readExternal</code>.
  90. *
  91. * @param out An <code>ObjectOutput</code> instance for writing the
  92. * object state
  93. *
  94. * @exception IOException If an error occurs
  95. */
  96. void writeExternal(ObjectOutput out) throws IOException;
  97. }