Example usage for org.apache.commons.lang3 SerializationException SerializationException

List of usage examples for org.apache.commons.lang3 SerializationException SerializationException

Introduction

In this page you can find the example usage for org.apache.commons.lang3 SerializationException SerializationException.

Prototype

public SerializationException(final Throwable cause) 

Source Link

Document

Constructs a new SerializationException with specified nested Throwable .

Usage

From source file:net.dontdrinkandroot.utils.lang.SerializationUtils.java

@SuppressWarnings("unchecked")
public static <T extends Serializable> T fastClone(T object) {

    Object obj = null;/*w w w  .j  a v a 2 s.c  o  m*/
    try {

        /* Write the object out to a byte array */
        FastByteArrayOutputStream fbos = new FastByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(fbos);
        out.writeObject(object);
        out.flush();
        out.close();

        /* Retrieve an input stream from the byte array and read a copy of the object back in. */
        ObjectInputStream in = new ObjectInputStream(fbos.getInputStream());
        obj = in.readObject();

    } catch (IOException e) {
        throw new SerializationException(e);
    } catch (ClassNotFoundException cnfe) {
        throw new SerializationException(cnfe);
    }

    return (T) obj;
}

From source file:com.github.helenusdriver.commons.lang3.SerializationUtils.java

/**
 * Serializes and compresses a given object to the specified stream.
 * <p>/*from ww  w. ja  va2s  .c o m*/
 * The stream will be closed once the object is written.
 * This avoids the need for a finally clause, and maybe also exception
 * handling, in the application code.
 * <p>
 * The stream passed in is not buffered internally within this method.
 * This is the responsibility of your application if desired.
 *
 * @author paouelle
 *
 * @param  obj the object to serialize and compress
 * @param  outputStream the stream to write to
 * @throws SerializationException (runtime) if the serialization fails
 * @throws NullPointerException if <code>outputStream</code> is <code>null</code>
 */
public static void serializeAndCompress(Serializable obj, OutputStream outputStream) {
    org.apache.commons.lang3.Validate.notNull(outputStream, "invalid null outputStream");
    try {
        final GZIPOutputStream os = new GZIPOutputStream(outputStream);

        org.apache.commons.lang3.SerializationUtils.serialize(obj, os);
    } catch (IOException e) {
        throw new SerializationException(e);
    }
}

From source file:com.link_intersystems.lang.reflect.SerializablePackage.java

@Override
protected Package deserialize(Serializable restoreInfo) {
    String packageName = (String) restoreInfo;
    Package packageObject = Package.getPackage(packageName);
    if (packageObject == null) {
        throw new SerializationException("Unable to restore package " + packageName);
    }/*from www  .j  av a 2 s.  c  om*/
    return packageObject;
}

From source file:com.link_intersystems.lang.reflect.SerializableField.java

@SuppressWarnings("unchecked")
@Override//from w  ww  .  jav  a  2  s  . c om
protected Field deserialize(Serializable restoreInfo) throws NoSuchFieldException {
    MemberSerialization<Field> memberSerializationInfo = (MemberSerialization<Field>) restoreInfo;
    Class<?> declaringClass = memberSerializationInfo.getDeclaringClass();
    String fieldName = memberSerializationInfo.getMemberName();
    int modifiers = memberSerializationInfo.getModifiers();
    Field field = declaringClass.getDeclaredField(fieldName);
    int currentModifiers = getModifier(field);
    if (modifiers != currentModifiers) {
        throw new SerializationException("Unable to restore field " + fieldName + " declared at "
                + declaringClass + ". Modifiers changed since serialization. Expected modifiers are "
                + Modifier.toString(modifiers) + ", but current modifiers are "
                + Modifier.toString(currentModifiers));
    }
    return field;
}

From source file:com.github.helenusdriver.commons.lang3.SerializationUtils.java

/**
 * Decompresses and deserializes an object from the specified stream.
 * <p>//from w  w w .  j a va  2s. com
 * The stream will be closed once the object is written. This
 * avoids the need for a finally clause, and maybe also exception
 * handling, in the application code.
 * <p>
 * The stream passed in is not buffered internally within this method.
 * This is the responsibility of your application if desired.
 *
 * @author paouelle
 *
 * @param  inputStream the serialized object input stream
 * @return the corresponding object
 * @throws NullPointerException if <code>outputStream</code> is <code>null</code>
 * @throws SerializationException (runtime) if the serialization fails
 */
public static Object decompressAndDeserialize(InputStream inputStream) {
    org.apache.commons.lang3.Validate.notNull(inputStream, "invalid null inputStream");
    try {
        final GZIPInputStream is = new GZIPInputStream(inputStream);

        return org.apache.commons.lang3.SerializationUtils.deserialize(is);
    } catch (IOException e) {
        throw new SerializationException(e);
    }
}

From source file:com.discovery.darchrow.io.SerializableUtil.java

/**
 * To byte array output stream.//from w  ww  .  j av a  2  s. co m
 *
 * @param serializable
 *            the serializable
 * @return the byte array output stream
 * @see java.io.ObjectOutputStream#ObjectOutputStream(OutputStream)
 * @see java.io.ObjectOutputStream#writeObject(Object)
 * @see org.apache.commons.lang3.SerializationUtils#serialize(Serializable, OutputStream)
 */
private static ByteArrayOutputStream toByteArrayOutputStream(Serializable serializable) {
    ObjectOutputStream objectOutputStream = null;
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(serializable);

        return byteArrayOutputStream;
    } catch (IOException e) {
        LOGGER.error("", e);
        throw new SerializationException(e);
    } finally {
        IOUtils.closeQuietly(objectOutputStream);
    }
}

From source file:com.jxtech.util.SerializationUtils.java

/**
 * <p>Serializes an <code>Object</code> to the specified stream.</p>
 *
 * <p>The stream will be closed once the object is written.
 * This avoids the need for a finally clause, and maybe also exception
 * handling, in the application code.</p>
 * //from  www  . ja v a  2s .co m
 * <p>The stream passed in is not buffered internally within this method.
 * This is the responsibility of your application if desired.</p>
 *
 * @param obj  the object to serialize to bytes, may be null
 * @param outputStream  the stream to write to, must not be null
 * @throws IllegalArgumentException if <code>outputStream</code> is <code>null</code>
 * @throws SerializationException (runtime) if the serialization fails
 */
public static void serialize(Serializable obj, OutputStream outputStream) {
    if (outputStream == null) {
        throw new IllegalArgumentException("The OutputStream must not be null");
    }
    HessianOutput out = null;
    try {
        // stream closed in the finally
        out = new HessianOutput(outputStream);
        out.writeObject(obj);
    } catch (IOException ex) {
        throw new SerializationException(ex);
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
}

From source file:com.discovery.darchrow.io.SerializableUtil.java

/**
 * To string.//  w w  w. j  av  a2s  .  c  om
 *
 * @param serializable
 *            the serializable
 * @return the string
 * @see #toByteArrayOutputStream(Serializable)
 * @deprecated 
 */
//TODO
@Deprecated
public static String toString(Serializable serializable) {
    ByteArrayOutputStream byteArrayOutputStream = null;
    try {
        byteArrayOutputStream = toByteArrayOutputStream(serializable);

        String serializableString = byteArrayOutputStream.toString(CharsetType.ISO_8859_1);
        serializableString = java.net.URLEncoder.encode(serializableString, CharsetType.UTF8);

        return serializableString;
    } catch (IOException e) {
        LOGGER.error("", e);
        throw new SerializationException(e);
    } finally {
        IOUtils.closeQuietly(byteArrayOutputStream);
    }
}

From source file:com.discovery.darchrow.io.SerializableUtil.java

/**
 * To object./*w  w  w . java 2  s.  c om*/
 *
 * @param <T>
 *            the generic type
 * @param serializableString
 *            the serializable string
 * @return the t
 * @deprecated 
 */
//TODO
@Deprecated
public static <T> T toObject(String serializableString) {
    ByteArrayInputStream byteArrayInputStream = null;
    ObjectInputStream objectInputStream = null;
    try {
        String decodeString = java.net.URLDecoder.decode(serializableString, CharsetType.UTF8);
        byteArrayInputStream = new ByteArrayInputStream(decodeString.getBytes(CharsetType.ISO_8859_1));

        return org.apache.commons.lang3.SerializationUtils.deserialize(byteArrayInputStream);
    } catch (IOException e) {
        LOGGER.error("", e);
        throw new SerializationException(e);
    } finally {
        IOUtils.closeQuietly(byteArrayInputStream);
        IOUtils.closeQuietly(objectInputStream);
    }
}

From source file:com.jxtech.util.SerializationUtils.java

/**
 * <p>Deserializes an <code>Object</code> from the specified stream.</p>
 *
 * <p>The stream will be closed once the object is written. This
 * avoids the need for a finally clause, and maybe also exception
 * handling, in the application code.</p>
 * /*from w w w.j  a  v a2 s  .c  o  m*/
 * <p>The stream passed in is not buffered internally within this method.
 * This is the responsibility of your application if desired.</p>
 *
 * @param inputStream  the serialized object input stream, must not be null
 * @return the deserialized object
 * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
 * @throws SerializationException (runtime) if the serialization fails
 */
public static Object deserialize(InputStream inputStream) {
    if (inputStream == null) {
        throw new IllegalArgumentException("The InputStream must not be null");
    }
    HessianInput in = null;
    try {
        // stream closed in the finally
        in = new HessianInput(inputStream);
        return in.readObject();
    } catch (IOException ex) {
        throw new SerializationException(ex);
    } finally {
        if (in != null) {
            in.close();
        }
    }
}