< prev index next >

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

Print this page




 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()
 456      * @see #readObject()
 457      * @since 1.2
 458      */


 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
 551     {
 552         SerialCallbackContext ctx = curContext;
 553         if (ctx == null) {


2372         }
2373         clear();
2374     }
2375 
2376     /**
2377      * Converts specified span of bytes into float values.
2378      */
2379     // REMIND: remove once hotspot inlines Float.intBitsToFloat
2380     private static native void bytesToFloats(byte[] src, int srcpos,
2381                                              float[] dst, int dstpos,
2382                                              int nfloats);
2383 
2384     /**
2385      * Converts specified span of bytes into double values.
2386      */
2387     // REMIND: remove once hotspot inlines Double.longBitsToDouble
2388     private static native void bytesToDoubles(byte[] src, int srcpos,
2389                                               double[] dst, int dstpos,
2390                                               int ndoubles);
2391 
















2392     /**
2393      * Returns the first non-null and non-platform class loader (not counting
2394      * class loaders of generated reflection implementation classes) up the
2395      * execution stack, or the platform class loader if only code from the
2396      * bootstrap and platform class loader is on the stack.
2397      */
2398     private static ClassLoader latestUserDefinedLoader() {
2399         return jdk.internal.misc.VM.latestUserDefinedLoader();




















2400     }
2401 
2402     /**
2403      * Default GetField implementation.
2404      */
2405     private class GetFieldImpl extends GetField {
2406 
2407         /** class descriptor describing serializable fields */
2408         private final ObjectStreamClass desc;
2409         /** primitive field values */
2410         private final byte[] primVals;
2411         /** object field values */
2412         private final Object[] objVals;
2413         /** object field value handles */
2414         private final int[] objHandles;
2415 
2416         /**
2417          * Creates GetFieldImpl object for reading fields defined in given
2418          * class descriptor.
2419          */




 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         // save current cachedLoader
 423         Object outerCL = cachedLoader;
 424 
 425         Thread thread  = Thread.currentThread();
 426         if (outerCL == null ||
 427             outerCL == thread ||
 428             (outerCL instanceof CachedLoader &&
 429              ((CachedLoader) outerCL).thread == thread)) {
 430             // place mark so that latestUserDefinedLoader() can cache loader
 431             cachedLoader = thread;
 432         }
 433         try {
 434             Object obj = readObject0(false);
 435             handles.markDependency(outerHandle, passHandle);
 436             ClassNotFoundException ex = handles.lookupException(passHandle);
 437             if (ex != null) {
 438                 throw ex;
 439             }
 440             if (depth == 0) {
 441                 vlist.doCallbacks();
 442                 freeze();
 443             }
 444             return obj;
 445         } finally {
 446             passHandle = outerHandle;
 447             if (closed && depth == 0) {
 448                 clear();
 449             }
 450             if (outerCL == null ||
 451                 outerCL == thread ||
 452                 (outerCL instanceof CachedLoader &&
 453                  ((CachedLoader) outerCL).thread == thread)) {
 454                 // restore/clear cachedLoader when in correct thread/non-nested call
 455                 cachedLoader = outerCL;
 456             }
 457         }
 458     }
 459 
 460     /**
 461      * This method is called by trusted subclasses of ObjectOutputStream that
 462      * constructed ObjectOutputStream using the protected no-arg constructor.
 463      * The subclass is expected to provide an override method with the modifier
 464      * "final".
 465      *
 466      * @return  the Object read from the stream.
 467      * @throws  ClassNotFoundException Class definition of a serialized object
 468      *          cannot be found.
 469      * @throws  OptionalDataException Primitive data was found in the stream
 470      *          instead of objects.
 471      * @throws  IOException if I/O errors occurred while reading from the
 472      *          underlying stream
 473      * @see #ObjectInputStream()
 474      * @see #readObject()
 475      * @since 1.2
 476      */


 515      * <p>ObjectInputStream subclasses which override this method can only be
 516      * constructed in security contexts possessing the
 517      * "enableSubclassImplementation" SerializablePermission; any attempt to
 518      * instantiate such a subclass without this permission will cause a
 519      * SecurityException to be thrown.
 520      *
 521      * @return  reference to deserialized object
 522      * @throws  ClassNotFoundException if class of an object to deserialize
 523      *          cannot be found
 524      * @throws  StreamCorruptedException if control information in the stream
 525      *          is inconsistent
 526      * @throws  ObjectStreamException if object to deserialize has already
 527      *          appeared in stream
 528      * @throws  OptionalDataException if primitive data is next in stream
 529      * @throws  IOException if an I/O error occurs during deserialization
 530      * @since   1.4
 531      */
 532     public Object readUnshared() throws IOException, ClassNotFoundException {
 533         // if nested read, passHandle contains handle of enclosing object
 534         int outerHandle = passHandle;
 535         // save current cachedLoader
 536         Object outerCL = cachedLoader;
 537 
 538         Thread thread  = Thread.currentThread();
 539         if (outerCL == null ||
 540              outerCL == thread ||
 541              (outerCL instanceof CachedLoader &&
 542               ((CachedLoader) outerCL).thread == thread)) {
 543             // place mark so that latestUserDefinedLoader() can cache loader
 544             cachedLoader = thread;
 545         }
 546         try {
 547             Object obj = readObject0(true);
 548             handles.markDependency(outerHandle, passHandle);
 549             ClassNotFoundException ex = handles.lookupException(passHandle);
 550             if (ex != null) {
 551                 throw ex;
 552             }
 553             if (depth == 0) {
 554                 vlist.doCallbacks();
 555                 freeze();
 556             }
 557             return obj;
 558         } finally {
 559             passHandle = outerHandle;
 560             if (closed && depth == 0) {
 561                 clear();
 562             }
 563             if (outerCL == null ||
 564                 outerCL == thread ||
 565                 (outerCL instanceof CachedLoader &&
 566                  ((CachedLoader) outerCL).thread == thread)) {
 567                 // restore/clear cachedLoader when in correct thread/non-nested call
 568                 cachedLoader = outerCL;
 569             }
 570         }
 571     }
 572 
 573     /**
 574      * Read the non-static and non-transient fields of the current class from
 575      * this stream.  This may only be called from the readObject method of the
 576      * class being deserialized. It will throw the NotActiveException if it is
 577      * called otherwise.
 578      *
 579      * @throws  ClassNotFoundException if the class of a serialized object
 580      *          could not be found.
 581      * @throws  IOException if an I/O error occurs.
 582      * @throws  NotActiveException if the stream is not currently reading
 583      *          objects.
 584      */
 585     public void defaultReadObject()
 586         throws IOException, ClassNotFoundException
 587     {
 588         SerialCallbackContext ctx = curContext;
 589         if (ctx == null) {


2408         }
2409         clear();
2410     }
2411 
2412     /**
2413      * Converts specified span of bytes into float values.
2414      */
2415     // REMIND: remove once hotspot inlines Float.intBitsToFloat
2416     private static native void bytesToFloats(byte[] src, int srcpos,
2417                                              float[] dst, int dstpos,
2418                                              int nfloats);
2419 
2420     /**
2421      * Converts specified span of bytes into double values.
2422      */
2423     // REMIND: remove once hotspot inlines Double.longBitsToDouble
2424     private static native void bytesToDoubles(byte[] src, int srcpos,
2425                                               double[] dst, int dstpos,
2426                                               int ndoubles);
2427 
2428     // cached latestUserDefinedLoader() result
2429     private static class CachedLoader {
2430         final ClassLoader loader;
2431         final Thread thread = Thread.currentThread();
2432 
2433         CachedLoader(ClassLoader loader) {
2434             this.loader = loader;
2435         }
2436     }
2437 
2438     // either null (when not called via public readObject() / readUnshared()), or
2439     // a Thread instance (marking the thread that entered public readObject() /
2440     // readUnshared() and is responsible for cleanup too), or
2441     // a CachedLoader instance with cached loader
2442     private Object cachedLoader;
2443 
2444     /**
2445      * Returns the first non-null and non-platform class loader (not counting
2446      * class loaders of generated reflection implementation classes) up the
2447      * execution stack, or the platform class loader if only code from the
2448      * bootstrap and platform class loader is on the stack.
2449      */
2450     private ClassLoader latestUserDefinedLoader() {
2451         Object cl = cachedLoader;
2452         ClassLoader loader;
2453         Thread thread = Thread.currentThread();
2454         if (cl == thread) {
2455             // entered via public readObject() / readUnshared()
2456             // so we must evaluate loader lazily and cache it
2457             loader = jdk.internal.misc.VM.latestUserDefinedLoader();
2458             cachedLoader = new CachedLoader(loader);
2459         } else if (cl instanceof CachedLoader &&
2460                    ((CachedLoader) cl).thread == thread) {
2461             // use cached value if correct thread
2462             loader = ((CachedLoader) cl).loader;
2463         } else {
2464             // not called via public readObject() / readUnshared():
2465             //   (cl == null) or
2466             // invalid multi threaded use:
2467             //   (cl != Thread.currentThread() && cl.thread != Thread.currentThread())
2468             // - don't cache
2469             loader = jdk.internal.misc.VM.latestUserDefinedLoader();
2470         }
2471         return loader;
2472     }
2473 
2474     /**
2475      * Default GetField implementation.
2476      */
2477     private class GetFieldImpl extends GetField {
2478 
2479         /** class descriptor describing serializable fields */
2480         private final ObjectStreamClass desc;
2481         /** primitive field values */
2482         private final byte[] primVals;
2483         /** object field values */
2484         private final Object[] objVals;
2485         /** object field value handles */
2486         private final int[] objHandles;
2487 
2488         /**
2489          * Creates GetFieldImpl object for reading fields defined in given
2490          * class descriptor.
2491          */


< prev index next >