Class SerializationUtils.ClassLoaderAwareObjectInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.DataInput, java.io.ObjectInput, java.io.ObjectStreamConstants, java.lang.AutoCloseable
    Enclosing class:
    SerializationUtils

    static class SerializationUtils.ClassLoaderAwareObjectInputStream
    extends java.io.ObjectInputStream

    Custom specialization of the standard JDK ObjectInputStream that uses a custom ClassLoader to resolve a class. If the specified ClassLoader is not able to resolve the class, the context classloader of the current thread will be used. This way, the standard deserialization work also in web-application containers and application servers, no matter in which of the ClassLoader the particular class that encapsulates serialization/deserialization lives.

    For more in-depth information about the problem for which this class here is a workaround, see the JIRA issue LANG-626.

    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.io.ObjectInputStream

        java.io.ObjectInputStream.GetField
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.ClassLoader classLoader  
      private static java.util.Map<java.lang.String,​java.lang.Class<?>> primitiveTypes  
      • Fields inherited from interface java.io.ObjectStreamConstants

        baseWireHandle, PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, SC_BLOCK_DATA, SC_ENUM, SC_EXTERNALIZABLE, SC_SERIALIZABLE, SC_WRITE_METHOD, SERIAL_FILTER_PERMISSION, STREAM_MAGIC, STREAM_VERSION, SUBCLASS_IMPLEMENTATION_PERMISSION, SUBSTITUTION_PERMISSION, TC_ARRAY, TC_BASE, TC_BLOCKDATA, TC_BLOCKDATALONG, TC_CLASS, TC_CLASSDESC, TC_ENDBLOCKDATA, TC_ENUM, TC_EXCEPTION, TC_LONGSTRING, TC_MAX, TC_NULL, TC_OBJECT, TC_PROXYCLASSDESC, TC_REFERENCE, TC_RESET, TC_STRING
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassLoaderAwareObjectInputStream​(java.io.InputStream in, java.lang.ClassLoader classLoader)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.Class<?> resolveClass​(java.io.ObjectStreamClass desc)
      Overridden version that uses the parameterized ClassLoader or the ClassLoader of the current Thread to resolve the class.
      • Methods inherited from class java.io.ObjectInputStream

        available, close, defaultReadObject, enableResolveObject, getObjectInputFilter, read, read, readBoolean, readByte, readChar, readClassDescriptor, readDouble, readFields, readFloat, readFully, readFully, readInt, readLine, readLong, readObject, readObjectOverride, readShort, readStreamHeader, readUnshared, readUnsignedByte, readUnsignedShort, readUTF, registerValidation, resolveObject, resolveProxyClass, setObjectInputFilter, skipBytes
      • Methods inherited from class java.io.InputStream

        mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.io.ObjectInput

        read, skip
    • Field Detail

      • primitiveTypes

        private static final java.util.Map<java.lang.String,​java.lang.Class<?>> primitiveTypes
      • classLoader

        private final java.lang.ClassLoader classLoader
    • Constructor Detail

      • ClassLoaderAwareObjectInputStream

        ClassLoaderAwareObjectInputStream​(java.io.InputStream in,
                                          java.lang.ClassLoader classLoader)
                                   throws java.io.IOException
        Constructor.
        Parameters:
        in - The InputStream.
        classLoader - classloader to use
        Throws:
        java.io.IOException - if an I/O error occurs while reading stream header.
        See Also:
        ObjectInputStream
    • Method Detail

      • resolveClass

        protected java.lang.Class<?> resolveClass​(java.io.ObjectStreamClass desc)
                                           throws java.io.IOException,
                                                  java.lang.ClassNotFoundException
        Overridden version that uses the parameterized ClassLoader or the ClassLoader of the current Thread to resolve the class.
        Overrides:
        resolveClass in class java.io.ObjectInputStream
        Parameters:
        desc - An instance of class ObjectStreamClass.
        Returns:
        A Class object corresponding to desc.
        Throws:
        java.io.IOException - Any of the usual Input/Output exceptions.
        java.lang.ClassNotFoundException - If class of a serialized object cannot be found.