< prev index next >

src/java.base/share/classes/java/io/ObjectOutputStream.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 340      * @throws  IOException Any exception thrown by the underlying
 341      *          OutputStream.
 342      */
 343     public final void writeObject(Object obj) throws IOException {
 344         if (enableOverride) {
 345             writeObjectOverride(obj);
 346             return;
 347         }
 348         try {
 349             writeObject0(obj, false);
 350         } catch (IOException ex) {
 351             if (depth == 0) {
 352                 writeFatalException(ex);
 353             }
 354             throw ex;
 355         }
 356     }
 357 
 358     /**
 359      * Method used by subclasses to override the default writeObject method.
 360      * This method is called by trusted subclasses of ObjectOutputStream that
 361      * constructed ObjectOutputStream using the protected no-arg constructor.
 362      * The subclass is expected to provide an override method with the modifier
 363      * "final".
 364      *
 365      * @param   obj object to be written to the underlying stream
 366      * @throws  IOException if there are I/O errors while writing to the
 367      *          underlying stream
 368      * @see #ObjectOutputStream()
 369      * @see #writeObject(Object)
 370      * @since 1.2
 371      */
 372     protected void writeObjectOverride(Object obj) throws IOException {
 373     }
 374 
 375     /**
 376      * Writes an "unshared" object to the ObjectOutputStream.  This method is
 377      * identical to writeObject, except that it always writes the given object
 378      * as a new, unique object in the stream (as opposed to a back-reference
 379      * pointing to a previously serialized instance).  Specifically:
 380      * <ul>
 381      *   <li>An object written via writeUnshared is always serialized in the


   1 /*
   2  * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 340      * @throws  IOException Any exception thrown by the underlying
 341      *          OutputStream.
 342      */
 343     public final void writeObject(Object obj) throws IOException {
 344         if (enableOverride) {
 345             writeObjectOverride(obj);
 346             return;
 347         }
 348         try {
 349             writeObject0(obj, false);
 350         } catch (IOException ex) {
 351             if (depth == 0) {
 352                 writeFatalException(ex);
 353             }
 354             throw ex;
 355         }
 356     }
 357 
 358     /**
 359      * Method used by subclasses to override the default writeObject method.
 360      * This method is called by trusted subclasses of ObjectInputStream that
 361      * constructed ObjectInputStream using the protected no-arg constructor.
 362      * The subclass is expected to provide an override method with the modifier
 363      * "final".
 364      *
 365      * @param   obj object to be written to the underlying stream
 366      * @throws  IOException if there are I/O errors while writing to the
 367      *          underlying stream
 368      * @see #ObjectOutputStream()
 369      * @see #writeObject(Object)
 370      * @since 1.2
 371      */
 372     protected void writeObjectOverride(Object obj) throws IOException {
 373     }
 374 
 375     /**
 376      * Writes an "unshared" object to the ObjectOutputStream.  This method is
 377      * identical to writeObject, except that it always writes the given object
 378      * as a new, unique object in the stream (as opposed to a back-reference
 379      * pointing to a previously serialized instance).  Specifically:
 380      * <ul>
 381      *   <li>An object written via writeUnshared is always serialized in the


< prev index next >