Search in sources :

Example 1 with SerializationException

use of krati.io.SerializationException in project camel by apache.

the class KratiDefaultSerializer method deserialize.

/**
     * Deserialize an object from its raw bytes generated by {{@link #serialize(Object)}.
     *
     * @param binary - the raw bytes from which an object is constructed.
     * @return an object constructed from the raw bytes.
     * @throws SerializationException if the object cannot be constructed from the raw bytes.
     */
@SuppressWarnings("unchecked")
public T deserialize(byte[] binary) throws SerializationException {
    T result = null;
    ObjectInputStream ois = null;
    if (binary == null) {
        return null;
    }
    // TODO: should use Camel's ClassResolver for classloading
    ByteArrayInputStream bais = new ByteArrayInputStream(binary);
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {
        ois = new ObjectInputStream(bais) {

            @Override
            public Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
                try {
                    return classLoader.loadClass(desc.getName());
                } catch (Exception e) {
                }
                return super.resolveClass(desc);
            }
        };
        result = (T) ois.readObject();
    } catch (IOException e) {
        LOG.warn("Error while deserializing object. Null will be used.", e);
    } catch (ClassNotFoundException e) {
        LOG.warn("Could not find class while deserializing object. Null will be used.", e);
    } finally {
        IOHelper.close(ois, bais);
    }
    return result;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectStreamClass(java.io.ObjectStreamClass) IOException(java.io.IOException) ObjectStreamClass(java.io.ObjectStreamClass) SerializationException(krati.io.SerializationException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectStreamClass (java.io.ObjectStreamClass)1 SerializationException (krati.io.SerializationException)1