src/java.base/share/classes/java/io/ObjectOutputStream.java
changeset 58288 48e480e56aad
parent 58244 da8673f61e94
child 58679 9c3209ff7550
equal deleted inserted replaced
58287:a7f16447085e 58288:48e480e56aad
   230      *
   230      *
   231      * @param   out output stream to write to
   231      * @param   out output stream to write to
   232      * @throws  IOException if an I/O error occurs while writing stream header
   232      * @throws  IOException if an I/O error occurs while writing stream header
   233      * @throws  SecurityException if untrusted subclass illegally overrides
   233      * @throws  SecurityException if untrusted subclass illegally overrides
   234      *          security-sensitive methods
   234      *          security-sensitive methods
   235      * @throws  NullPointerException if <code>out</code> is <code>null</code>
   235      * @throws  NullPointerException if {@code out} is {@code null}
   236      * @since   1.4
   236      * @since   1.4
   237      * @see     ObjectOutputStream#ObjectOutputStream()
   237      * @see     ObjectOutputStream#ObjectOutputStream()
   238      * @see     ObjectOutputStream#putFields()
   238      * @see     ObjectOutputStream#putFields()
   239      * @see     ObjectInputStream#ObjectInputStream(InputStream)
   239      * @see     ObjectInputStream#ObjectInputStream(InputStream)
   240      */
   240      */
   257      * Provide a way for subclasses that are completely reimplementing
   257      * Provide a way for subclasses that are completely reimplementing
   258      * ObjectOutputStream to not have to allocate private data just used by
   258      * ObjectOutputStream to not have to allocate private data just used by
   259      * this implementation of ObjectOutputStream.
   259      * this implementation of ObjectOutputStream.
   260      *
   260      *
   261      * <p>If there is a security manager installed, this method first calls the
   261      * <p>If there is a security manager installed, this method first calls the
   262      * security manager's <code>checkPermission</code> method with a
   262      * security manager's {@code checkPermission} method with a
   263      * <code>SerializablePermission("enableSubclassImplementation")</code>
   263      * {@code SerializablePermission("enableSubclassImplementation")}
   264      * permission to ensure it's ok to enable subclassing.
   264      * permission to ensure it's ok to enable subclassing.
   265      *
   265      *
   266      * @throws  SecurityException if a security manager exists and its
   266      * @throws  SecurityException if a security manager exists and its
   267      *          <code>checkPermission</code> method denies enabling
   267      *          {@code checkPermission} method denies enabling
   268      *          subclassing.
   268      *          subclassing.
   269      * @throws  IOException if an I/O error occurs while creating this stream
   269      * @throws  IOException if an I/O error occurs while creating this stream
   270      * @see SecurityManager#checkPermission
   270      * @see SecurityManager#checkPermission
   271      * @see java.io.SerializablePermission
   271      * @see java.io.SerializablePermission
   272      */
   272      */
   427      * this stream.  This may only be called from the writeObject method of the
   427      * this stream.  This may only be called from the writeObject method of the
   428      * class being serialized. It will throw the NotActiveException if it is
   428      * class being serialized. It will throw the NotActiveException if it is
   429      * called otherwise.
   429      * called otherwise.
   430      *
   430      *
   431      * @throws  IOException if I/O errors occur while writing to the underlying
   431      * @throws  IOException if I/O errors occur while writing to the underlying
   432      *          <code>OutputStream</code>
   432      *          {@code OutputStream}
   433      */
   433      */
   434     public void defaultWriteObject() throws IOException {
   434     public void defaultWriteObject() throws IOException {
   435         SerialCallbackContext ctx = curContext;
   435         SerialCallbackContext ctx = curContext;
   436         if (ctx == null) {
   436         if (ctx == null) {
   437             throw new NotActiveException("not in call to writeObject");
   437             throw new NotActiveException("not in call to writeObject");
   527      * Subclasses may implement this method to store custom data in the stream
   527      * Subclasses may implement this method to store custom data in the stream
   528      * along with descriptors for dynamic proxy classes.
   528      * along with descriptors for dynamic proxy classes.
   529      *
   529      *
   530      * <p>This method is called exactly once for each unique proxy class
   530      * <p>This method is called exactly once for each unique proxy class
   531      * descriptor in the stream.  The default implementation of this method in
   531      * descriptor in the stream.  The default implementation of this method in
   532      * <code>ObjectOutputStream</code> does nothing.
   532      * {@code ObjectOutputStream} does nothing.
   533      *
   533      *
   534      * <p>The corresponding method in <code>ObjectInputStream</code> is
   534      * <p>The corresponding method in {@code ObjectInputStream} is
   535      * <code>resolveProxyClass</code>.  For a given subclass of
   535      * {@code resolveProxyClass}.  For a given subclass of
   536      * <code>ObjectOutputStream</code> that overrides this method, the
   536      * {@code ObjectOutputStream} that overrides this method, the
   537      * <code>resolveProxyClass</code> method in the corresponding subclass of
   537      * {@code resolveProxyClass} method in the corresponding subclass of
   538      * <code>ObjectInputStream</code> must read any data or objects written by
   538      * {@code ObjectInputStream} must read any data or objects written by
   539      * <code>annotateProxyClass</code>.
   539      * {@code annotateProxyClass}.
   540      *
   540      *
   541      * @param   cl the proxy class to annotate custom data for
   541      * @param   cl the proxy class to annotate custom data for
   542      * @throws  IOException any exception thrown by the underlying
   542      * @throws  IOException any exception thrown by the underlying
   543      *          <code>OutputStream</code>
   543      *          {@code OutputStream}
   544      * @see ObjectInputStream#resolveProxyClass(String[])
   544      * @see ObjectInputStream#resolveProxyClass(String[])
   545      * @since   1.3
   545      * @since   1.3
   546      */
   546      */
   547     protected void annotateProxyClass(Class<?> cl) throws IOException {
   547     protected void annotateProxyClass(Class<?> cl) throws IOException {
   548     }
   548     }
   644      * Write the specified class descriptor to the ObjectOutputStream.  Class
   644      * Write the specified class descriptor to the ObjectOutputStream.  Class
   645      * descriptors are used to identify the classes of objects written to the
   645      * descriptors are used to identify the classes of objects written to the
   646      * stream.  Subclasses of ObjectOutputStream may override this method to
   646      * stream.  Subclasses of ObjectOutputStream may override this method to
   647      * customize the way in which class descriptors are written to the
   647      * customize the way in which class descriptors are written to the
   648      * serialization stream.  The corresponding method in ObjectInputStream,
   648      * serialization stream.  The corresponding method in ObjectInputStream,
   649      * <code>readClassDescriptor</code>, should then be overridden to
   649      * {@code readClassDescriptor}, should then be overridden to
   650      * reconstitute the class descriptor from its custom stream representation.
   650      * reconstitute the class descriptor from its custom stream representation.
   651      * By default, this method writes class descriptors according to the format
   651      * By default, this method writes class descriptors according to the format
   652      * defined in the Object Serialization specification.
   652      * defined in the Object Serialization specification.
   653      *
   653      *
   654      * <p>Note that this method will only be called if the ObjectOutputStream
   654      * <p>Note that this method will only be called if the ObjectOutputStream
   655      * is not using the old serialization stream format (set by calling
   655      * is not using the old serialization stream format (set by calling
   656      * ObjectOutputStream's <code>useProtocolVersion</code> method).  If this
   656      * ObjectOutputStream's {@code useProtocolVersion} method).  If this
   657      * serialization stream is using the old format
   657      * serialization stream is using the old format
   658      * (<code>PROTOCOL_VERSION_1</code>), the class descriptor will be written
   658      * ({@code PROTOCOL_VERSION_1}), the class descriptor will be written
   659      * internally in a manner that cannot be overridden or customized.
   659      * internally in a manner that cannot be overridden or customized.
   660      *
   660      *
   661      * @param   desc class descriptor to write to the stream
   661      * @param   desc class descriptor to write to the stream
   662      * @throws  IOException If an I/O error has occurred.
   662      * @throws  IOException If an I/O error has occurred.
   663      * @see java.io.ObjectInputStream#readClassDescriptor()
   663      * @see java.io.ObjectInputStream#readClassDescriptor()
   887         /**
   887         /**
   888          * Put the value of the named boolean field into the persistent field.
   888          * Put the value of the named boolean field into the persistent field.
   889          *
   889          *
   890          * @param  name the name of the serializable field
   890          * @param  name the name of the serializable field
   891          * @param  val the value to assign to the field
   891          * @param  val the value to assign to the field
   892          * @throws IllegalArgumentException if <code>name</code> does not
   892          * @throws IllegalArgumentException if {@code name} does not
   893          * match the name of a serializable field for the class whose fields
   893          * match the name of a serializable field for the class whose fields
   894          * are being written, or if the type of the named field is not
   894          * are being written, or if the type of the named field is not
   895          * <code>boolean</code>
   895          * {@code boolean}
   896          */
   896          */
   897         public abstract void put(String name, boolean val);
   897         public abstract void put(String name, boolean val);
   898 
   898 
   899         /**
   899         /**
   900          * Put the value of the named byte field into the persistent field.
   900          * Put the value of the named byte field into the persistent field.
   901          *
   901          *
   902          * @param  name the name of the serializable field
   902          * @param  name the name of the serializable field
   903          * @param  val the value to assign to the field
   903          * @param  val the value to assign to the field
   904          * @throws IllegalArgumentException if <code>name</code> does not
   904          * @throws IllegalArgumentException if {@code name} does not
   905          * match the name of a serializable field for the class whose fields
   905          * match the name of a serializable field for the class whose fields
   906          * are being written, or if the type of the named field is not
   906          * are being written, or if the type of the named field is not
   907          * <code>byte</code>
   907          * {@code byte}
   908          */
   908          */
   909         public abstract void put(String name, byte val);
   909         public abstract void put(String name, byte val);
   910 
   910 
   911         /**
   911         /**
   912          * Put the value of the named char field into the persistent field.
   912          * Put the value of the named char field into the persistent field.
   913          *
   913          *
   914          * @param  name the name of the serializable field
   914          * @param  name the name of the serializable field
   915          * @param  val the value to assign to the field
   915          * @param  val the value to assign to the field
   916          * @throws IllegalArgumentException if <code>name</code> does not
   916          * @throws IllegalArgumentException if {@code name} does not
   917          * match the name of a serializable field for the class whose fields
   917          * match the name of a serializable field for the class whose fields
   918          * are being written, or if the type of the named field is not
   918          * are being written, or if the type of the named field is not
   919          * <code>char</code>
   919          * {@code char}
   920          */
   920          */
   921         public abstract void put(String name, char val);
   921         public abstract void put(String name, char val);
   922 
   922 
   923         /**
   923         /**
   924          * Put the value of the named short field into the persistent field.
   924          * Put the value of the named short field into the persistent field.
   925          *
   925          *
   926          * @param  name the name of the serializable field
   926          * @param  name the name of the serializable field
   927          * @param  val the value to assign to the field
   927          * @param  val the value to assign to the field
   928          * @throws IllegalArgumentException if <code>name</code> does not
   928          * @throws IllegalArgumentException if {@code name} does not
   929          * match the name of a serializable field for the class whose fields
   929          * match the name of a serializable field for the class whose fields
   930          * are being written, or if the type of the named field is not
   930          * are being written, or if the type of the named field is not
   931          * <code>short</code>
   931          * {@code short}
   932          */
   932          */
   933         public abstract void put(String name, short val);
   933         public abstract void put(String name, short val);
   934 
   934 
   935         /**
   935         /**
   936          * Put the value of the named int field into the persistent field.
   936          * Put the value of the named int field into the persistent field.
   937          *
   937          *
   938          * @param  name the name of the serializable field
   938          * @param  name the name of the serializable field
   939          * @param  val the value to assign to the field
   939          * @param  val the value to assign to the field
   940          * @throws IllegalArgumentException if <code>name</code> does not
   940          * @throws IllegalArgumentException if {@code name} does not
   941          * match the name of a serializable field for the class whose fields
   941          * match the name of a serializable field for the class whose fields
   942          * are being written, or if the type of the named field is not
   942          * are being written, or if the type of the named field is not
   943          * <code>int</code>
   943          * {@code int}
   944          */
   944          */
   945         public abstract void put(String name, int val);
   945         public abstract void put(String name, int val);
   946 
   946 
   947         /**
   947         /**
   948          * Put the value of the named long field into the persistent field.
   948          * Put the value of the named long field into the persistent field.
   949          *
   949          *
   950          * @param  name the name of the serializable field
   950          * @param  name the name of the serializable field
   951          * @param  val the value to assign to the field
   951          * @param  val the value to assign to the field
   952          * @throws IllegalArgumentException if <code>name</code> does not
   952          * @throws IllegalArgumentException if {@code name} does not
   953          * match the name of a serializable field for the class whose fields
   953          * match the name of a serializable field for the class whose fields
   954          * are being written, or if the type of the named field is not
   954          * are being written, or if the type of the named field is not
   955          * <code>long</code>
   955          * {@code long}
   956          */
   956          */
   957         public abstract void put(String name, long val);
   957         public abstract void put(String name, long val);
   958 
   958 
   959         /**
   959         /**
   960          * Put the value of the named float field into the persistent field.
   960          * Put the value of the named float field into the persistent field.
   961          *
   961          *
   962          * @param  name the name of the serializable field
   962          * @param  name the name of the serializable field
   963          * @param  val the value to assign to the field
   963          * @param  val the value to assign to the field
   964          * @throws IllegalArgumentException if <code>name</code> does not
   964          * @throws IllegalArgumentException if {@code name} does not
   965          * match the name of a serializable field for the class whose fields
   965          * match the name of a serializable field for the class whose fields
   966          * are being written, or if the type of the named field is not
   966          * are being written, or if the type of the named field is not
   967          * <code>float</code>
   967          * {@code float}
   968          */
   968          */
   969         public abstract void put(String name, float val);
   969         public abstract void put(String name, float val);
   970 
   970 
   971         /**
   971         /**
   972          * Put the value of the named double field into the persistent field.
   972          * Put the value of the named double field into the persistent field.
   973          *
   973          *
   974          * @param  name the name of the serializable field
   974          * @param  name the name of the serializable field
   975          * @param  val the value to assign to the field
   975          * @param  val the value to assign to the field
   976          * @throws IllegalArgumentException if <code>name</code> does not
   976          * @throws IllegalArgumentException if {@code name} does not
   977          * match the name of a serializable field for the class whose fields
   977          * match the name of a serializable field for the class whose fields
   978          * are being written, or if the type of the named field is not
   978          * are being written, or if the type of the named field is not
   979          * <code>double</code>
   979          * {@code double}
   980          */
   980          */
   981         public abstract void put(String name, double val);
   981         public abstract void put(String name, double val);
   982 
   982 
   983         /**
   983         /**
   984          * Put the value of the named Object field into the persistent field.
   984          * Put the value of the named Object field into the persistent field.
   985          *
   985          *
   986          * @param  name the name of the serializable field
   986          * @param  name the name of the serializable field
   987          * @param  val the value to assign to the field
   987          * @param  val the value to assign to the field
   988          *         (which may be <code>null</code>)
   988          *         (which may be {@code null})
   989          * @throws IllegalArgumentException if <code>name</code> does not
   989          * @throws IllegalArgumentException if {@code name} does not
   990          * match the name of a serializable field for the class whose fields
   990          * match the name of a serializable field for the class whose fields
   991          * are being written, or if the type of the named field is not a
   991          * are being written, or if the type of the named field is not a
   992          * reference type
   992          * reference type
   993          */
   993          */
   994         public abstract void put(String name, Object val);
   994         public abstract void put(String name, Object val);
   995 
   995 
   996         /**
   996         /**
   997          * Write the data and fields to the specified ObjectOutput stream,
   997          * Write the data and fields to the specified ObjectOutput stream,
   998          * which must be the same stream that produced this
   998          * which must be the same stream that produced this
   999          * <code>PutField</code> object.
   999          * {@code PutField} object.
  1000          *
  1000          *
  1001          * @param  out the stream to write the data and fields to
  1001          * @param  out the stream to write the data and fields to
  1002          * @throws IOException if I/O errors occur while writing to the
  1002          * @throws IOException if I/O errors occur while writing to the
  1003          *         underlying stream
  1003          *         underlying stream
  1004          * @throws IllegalArgumentException if the specified stream is not
  1004          * @throws IllegalArgumentException if the specified stream is not
  1005          *         the same stream that produced this <code>PutField</code>
  1005          *         the same stream that produced this {@code PutField}
  1006          *         object
  1006          *         object
  1007          * @deprecated This method does not write the values contained by this
  1007          * @deprecated This method does not write the values contained by this
  1008          *         <code>PutField</code> object in a proper format, and may
  1008          *         {@code PutField} object in a proper format, and may
  1009          *         result in corruption of the serialization stream.  The
  1009          *         result in corruption of the serialization stream.  The
  1010          *         correct way to write <code>PutField</code> data is by
  1010          *         correct way to write {@code PutField} data is by
  1011          *         calling the {@link java.io.ObjectOutputStream#writeFields()}
  1011          *         calling the {@link java.io.ObjectOutputStream#writeFields()}
  1012          *         method.
  1012          *         method.
  1013          */
  1013          */
  1014         @Deprecated
  1014         @Deprecated
  1015         public abstract void write(ObjectOutput out) throws IOException;
  1015         public abstract void write(ObjectOutput out) throws IOException;