< prev index next >

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

Print this page




 359      *
 360      * @throws  SecurityException if a security manager exists and its
 361      *          <code>checkPermission</code> method denies enabling
 362      *          subclassing.
 363      * @throws  IOException if an I/O error occurs while creating this stream
 364      * @see SecurityManager#checkPermission
 365      * @see java.io.SerializablePermission
 366      */
 367     protected ObjectInputStream() throws IOException, SecurityException {
 368         SecurityManager sm = System.getSecurityManager();
 369         if (sm != null) {
 370             sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
 371         }
 372         bin = null;
 373         handles = null;
 374         vlist = null;
 375         serialFilter = ObjectInputFilter.Config.getSerialFilter();
 376         enableOverride = true;
 377     }
 378 


 379     /**
 380      * Read an object from the ObjectInputStream.  The class of the object, the
 381      * signature of the class, and the values of the non-transient and
 382      * non-static fields of the class and all of its supertypes are read.
 383      * Default deserializing for a class can be overridden using the writeObject
 384      * and readObject methods.  Objects referenced by this object are read
 385      * transitively so that a complete equivalent graph of objects is
 386      * reconstructed by readObject.
 387      *
 388      * <p>The root object is completely restored when all of its fields and the
 389      * objects it references are completely restored.  At this point the object
 390      * validation callbacks are executed in order based on their registered
 391      * priorities. The callbacks are registered by objects (in the readObject
 392      * special methods) as they are individually restored.
 393      *
 394      * <p>The serialization filter, when not {@code null}, is invoked for
 395      * each object (regular or class) read to reconstruct the root object.
 396      * See {@link #setObjectInputFilter(ObjectInputFilter) setObjectInputFilter} for details.
 397      *
 398      * <p>Exceptions are thrown for problems with the InputStream and for


 400      * the InputStream and leave it in an indeterminate state; it is up to the
 401      * caller to ignore or recover the stream state.
 402      *
 403      * @throws  ClassNotFoundException Class of a serialized object cannot be
 404      *          found.
 405      * @throws  InvalidClassException Something is wrong with a class used by
 406      *          serialization.
 407      * @throws  StreamCorruptedException Control information in the
 408      *          stream is inconsistent.
 409      * @throws  OptionalDataException Primitive data was found in the
 410      *          stream instead of objects.
 411      * @throws  IOException Any of the usual Input/Output related exceptions.
 412      */
 413     public final Object readObject()
 414         throws IOException, ClassNotFoundException
 415     {
 416         if (enableOverride) {
 417             return readObjectOverride();
 418         }
 419 








 420         // if nested read, passHandle contains handle of enclosing object
 421         int outerHandle = passHandle;
 422         try {
 423             Object obj = readObject0(false);
 424             handles.markDependency(outerHandle, passHandle);
 425             ClassNotFoundException ex = handles.lookupException(passHandle);
 426             if (ex != null) {
 427                 throw ex;
 428             }
 429             if (depth == 0) {
 430                 vlist.doCallbacks();
 431                 freeze();
 432             }
 433             return obj;
 434         } finally {
 435             passHandle = outerHandle;



 436             if (closed && depth == 0) {
 437                 clear();
 438             }
 439         }
 440     }
 441 
 442     /**
 443      * This method is called by trusted subclasses of ObjectOutputStream that
 444      * constructed ObjectOutputStream using the protected no-arg constructor.
 445      * The subclass is expected to provide an override method with the modifier
 446      * "final".
 447      *
 448      * @return  the Object read from the stream.
 449      * @throws  ClassNotFoundException Class definition of a serialized object
 450      *          cannot be found.
 451      * @throws  OptionalDataException Primitive data was found in the stream
 452      *          instead of objects.
 453      * @throws  IOException if I/O errors occurred while reading from the
 454      *          underlying stream
 455      * @see #ObjectInputStream()


 495      * See {@link #setObjectInputFilter(ObjectInputFilter) setObjectInputFilter} for details.
 496      *
 497      * <p>ObjectInputStream subclasses which override this method can only be
 498      * constructed in security contexts possessing the
 499      * "enableSubclassImplementation" SerializablePermission; any attempt to
 500      * instantiate such a subclass without this permission will cause a
 501      * SecurityException to be thrown.
 502      *
 503      * @return  reference to deserialized object
 504      * @throws  ClassNotFoundException if class of an object to deserialize
 505      *          cannot be found
 506      * @throws  StreamCorruptedException if control information in the stream
 507      *          is inconsistent
 508      * @throws  ObjectStreamException if object to deserialize has already
 509      *          appeared in stream
 510      * @throws  OptionalDataException if primitive data is next in stream
 511      * @throws  IOException if an I/O error occurs during deserialization
 512      * @since   1.4
 513      */
 514     public Object readUnshared() throws IOException, ClassNotFoundException {







 515         // if nested read, passHandle contains handle of enclosing object
 516         int outerHandle = passHandle;
 517         try {
 518             Object obj = readObject0(true);
 519             handles.markDependency(outerHandle, passHandle);
 520             ClassNotFoundException ex = handles.lookupException(passHandle);
 521             if (ex != null) {
 522                 throw ex;
 523             }
 524             if (depth == 0) {
 525                 vlist.doCallbacks();
 526                 freeze();
 527             }
 528             return obj;
 529         } finally {
 530             passHandle = outerHandle;



 531             if (closed && depth == 0) {
 532                 clear();
 533             }
 534         }
 535     }
 536 
 537     /**
 538      * Read the non-static and non-transient fields of the current class from
 539      * this stream.  This may only be called from the readObject method of the
 540      * class being deserialized. It will throw the NotActiveException if it is
 541      * called otherwise.
 542      *
 543      * @throws  ClassNotFoundException if the class of a serialized object
 544      *          could not be found.
 545      * @throws  IOException if an I/O error occurs.
 546      * @throws  NotActiveException if the stream is not currently reading
 547      *          objects.
 548      */
 549     public void defaultReadObject()
 550         throws IOException, ClassNotFoundException


 666      * <code>ClassNotFoundException</code> and the name of the passed
 667      * <code>ObjectStreamClass</code> instance is the Java language keyword
 668      * for a primitive type or void, then the <code>Class</code> object
 669      * representing that primitive type or void will be returned
 670      * (e.g., an <code>ObjectStreamClass</code> with the name
 671      * <code>"int"</code> will be resolved to <code>Integer.TYPE</code>).
 672      * Otherwise, the <code>ClassNotFoundException</code> will be thrown to
 673      * the caller of this method.
 674      *
 675      * @param   desc an instance of class <code>ObjectStreamClass</code>
 676      * @return  a <code>Class</code> object corresponding to <code>desc</code>
 677      * @throws  IOException any of the usual Input/Output exceptions.
 678      * @throws  ClassNotFoundException if class of a serialized object cannot
 679      *          be found.
 680      */
 681     protected Class<?> resolveClass(ObjectStreamClass desc)
 682         throws IOException, ClassNotFoundException
 683     {
 684         String name = desc.getName();
 685         try {



 686             return Class.forName(name, false, latestUserDefinedLoader());
 687         } catch (ClassNotFoundException ex) {
 688             Class<?> cl = primClasses.get(name);
 689             if (cl != null) {
 690                 return cl;
 691             } else {
 692                 throw ex;
 693             }
 694         }
 695     }
 696 
 697     /**
 698      * Returns a proxy class that implements the interfaces named in a proxy
 699      * class descriptor; subclasses may implement this method to read custom
 700      * data from the stream along with the descriptors for dynamic proxy
 701      * classes, allowing them to use an alternate loading mechanism for the
 702      * interfaces and the proxy class.
 703      *
 704      * <p>This method is called exactly once for each unique proxy class
 705      * descriptor in the stream.




 359      *
 360      * @throws  SecurityException if a security manager exists and its
 361      *          <code>checkPermission</code> method denies enabling
 362      *          subclassing.
 363      * @throws  IOException if an I/O error occurs while creating this stream
 364      * @see SecurityManager#checkPermission
 365      * @see java.io.SerializablePermission
 366      */
 367     protected ObjectInputStream() throws IOException, SecurityException {
 368         SecurityManager sm = System.getSecurityManager();
 369         if (sm != null) {
 370             sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
 371         }
 372         bin = null;
 373         handles = null;
 374         vlist = null;
 375         serialFilter = ObjectInputFilter.Config.getSerialFilter();
 376         enableOverride = true;
 377     }
 378 
 379     private ClassLoader cachedLoader = null;
 380     
 381     /**
 382      * Read an object from the ObjectInputStream.  The class of the object, the
 383      * signature of the class, and the values of the non-transient and
 384      * non-static fields of the class and all of its supertypes are read.
 385      * Default deserializing for a class can be overridden using the writeObject
 386      * and readObject methods.  Objects referenced by this object are read
 387      * transitively so that a complete equivalent graph of objects is
 388      * reconstructed by readObject.
 389      *
 390      * <p>The root object is completely restored when all of its fields and the
 391      * objects it references are completely restored.  At this point the object
 392      * validation callbacks are executed in order based on their registered
 393      * priorities. The callbacks are registered by objects (in the readObject
 394      * special methods) as they are individually restored.
 395      *
 396      * <p>The serialization filter, when not {@code null}, is invoked for
 397      * each object (regular or class) read to reconstruct the root object.
 398      * See {@link #setObjectInputFilter(ObjectInputFilter) setObjectInputFilter} for details.
 399      *
 400      * <p>Exceptions are thrown for problems with the InputStream and for


 402      * the InputStream and leave it in an indeterminate state; it is up to the
 403      * caller to ignore or recover the stream state.
 404      *
 405      * @throws  ClassNotFoundException Class of a serialized object cannot be
 406      *          found.
 407      * @throws  InvalidClassException Something is wrong with a class used by
 408      *          serialization.
 409      * @throws  StreamCorruptedException Control information in the
 410      *          stream is inconsistent.
 411      * @throws  OptionalDataException Primitive data was found in the
 412      *          stream instead of objects.
 413      * @throws  IOException Any of the usual Input/Output related exceptions.
 414      */
 415     public final Object readObject()
 416         throws IOException, ClassNotFoundException
 417     {
 418         if (enableOverride) {
 419             return readObjectOverride();
 420         }
 421 
 422         ClassLoader prevClassLoader = null;
 423         boolean cached = false;
 424         if (curContext == null) {
 425             prevClassLoader = cachedLoader;
 426             cachedLoader = latestUserDefinedLoader();
 427             cached = true;
 428         }
 429 
 430         // if nested read, passHandle contains handle of enclosing object
 431         int outerHandle = passHandle;
 432         try {
 433             Object obj = readObject0(false);
 434             handles.markDependency(outerHandle, passHandle);
 435             ClassNotFoundException ex = handles.lookupException(passHandle);
 436             if (ex != null) {
 437                 throw ex;
 438             }
 439             if (depth == 0) {
 440                 vlist.doCallbacks();
 441                 freeze();
 442             }
 443             return obj;
 444         } finally {
 445             passHandle = outerHandle;
 446             if (cached) {
 447                 cachedLoader = prevClassLoader;
 448             }
 449             if (closed && depth == 0) {
 450                 clear();
 451             }
 452         }
 453     }
 454 
 455     /**
 456      * This method is called by trusted subclasses of ObjectOutputStream that
 457      * constructed ObjectOutputStream using the protected no-arg constructor.
 458      * The subclass is expected to provide an override method with the modifier
 459      * "final".
 460      *
 461      * @return  the Object read from the stream.
 462      * @throws  ClassNotFoundException Class definition of a serialized object
 463      *          cannot be found.
 464      * @throws  OptionalDataException Primitive data was found in the stream
 465      *          instead of objects.
 466      * @throws  IOException if I/O errors occurred while reading from the
 467      *          underlying stream
 468      * @see #ObjectInputStream()


 508      * See {@link #setObjectInputFilter(ObjectInputFilter) setObjectInputFilter} for details.
 509      *
 510      * <p>ObjectInputStream subclasses which override this method can only be
 511      * constructed in security contexts possessing the
 512      * "enableSubclassImplementation" SerializablePermission; any attempt to
 513      * instantiate such a subclass without this permission will cause a
 514      * SecurityException to be thrown.
 515      *
 516      * @return  reference to deserialized object
 517      * @throws  ClassNotFoundException if class of an object to deserialize
 518      *          cannot be found
 519      * @throws  StreamCorruptedException if control information in the stream
 520      *          is inconsistent
 521      * @throws  ObjectStreamException if object to deserialize has already
 522      *          appeared in stream
 523      * @throws  OptionalDataException if primitive data is next in stream
 524      * @throws  IOException if an I/O error occurs during deserialization
 525      * @since   1.4
 526      */
 527     public Object readUnshared() throws IOException, ClassNotFoundException {
 528         ClassLoader prevClassLoader = null;
 529         boolean cached = false;
 530         if (curContext == null) {
 531             prevClassLoader = cachedLoader;
 532             cachedLoader = latestUserDefinedLoader();
 533             cached = true;
 534         }
 535         // if nested read, passHandle contains handle of enclosing object
 536         int outerHandle = passHandle;
 537         try {
 538             Object obj = readObject0(true);
 539             handles.markDependency(outerHandle, passHandle);
 540             ClassNotFoundException ex = handles.lookupException(passHandle);
 541             if (ex != null) {
 542                 throw ex;
 543             }
 544             if (depth == 0) {
 545                 vlist.doCallbacks();
 546                 freeze();
 547             }
 548             return obj;
 549         } finally {
 550             passHandle = outerHandle;
 551             if (cached) {
 552                 cachedLoader = prevClassLoader;
 553             }
 554             if (closed && depth == 0) {
 555                 clear();
 556             }
 557         }
 558     }
 559 
 560     /**
 561      * Read the non-static and non-transient fields of the current class from
 562      * this stream.  This may only be called from the readObject method of the
 563      * class being deserialized. It will throw the NotActiveException if it is
 564      * called otherwise.
 565      *
 566      * @throws  ClassNotFoundException if the class of a serialized object
 567      *          could not be found.
 568      * @throws  IOException if an I/O error occurs.
 569      * @throws  NotActiveException if the stream is not currently reading
 570      *          objects.
 571      */
 572     public void defaultReadObject()
 573         throws IOException, ClassNotFoundException


 689      * <code>ClassNotFoundException</code> and the name of the passed
 690      * <code>ObjectStreamClass</code> instance is the Java language keyword
 691      * for a primitive type or void, then the <code>Class</code> object
 692      * representing that primitive type or void will be returned
 693      * (e.g., an <code>ObjectStreamClass</code> with the name
 694      * <code>"int"</code> will be resolved to <code>Integer.TYPE</code>).
 695      * Otherwise, the <code>ClassNotFoundException</code> will be thrown to
 696      * the caller of this method.
 697      *
 698      * @param   desc an instance of class <code>ObjectStreamClass</code>
 699      * @return  a <code>Class</code> object corresponding to <code>desc</code>
 700      * @throws  IOException any of the usual Input/Output exceptions.
 701      * @throws  ClassNotFoundException if class of a serialized object cannot
 702      *          be found.
 703      */
 704     protected Class<?> resolveClass(ObjectStreamClass desc)
 705         throws IOException, ClassNotFoundException
 706     {
 707         String name = desc.getName();
 708         try {
 709             if (cachedLoader != null)
 710                 return Class.forName(name, false, cachedLoader);
 711             else
 712                 return Class.forName(name, false, latestUserDefinedLoader());
 713         } catch (ClassNotFoundException ex) {
 714             Class<?> cl = primClasses.get(name);
 715             if (cl != null) {
 716                 return cl;
 717             } else {
 718                 throw ex;
 719             }
 720         }
 721     }
 722 
 723     /**
 724      * Returns a proxy class that implements the interfaces named in a proxy
 725      * class descriptor; subclasses may implement this method to read custom
 726      * data from the stream along with the descriptors for dynamic proxy
 727      * classes, allowing them to use an alternate loading mechanism for the
 728      * interfaces and the proxy class.
 729      *
 730      * <p>This method is called exactly once for each unique proxy class
 731      * descriptor in the stream.


< prev index next >