Search in sources :

Example 1 with ByteArrayIOStream

use of com.palantir.util.ByteArrayIOStream in project atlasdb by palantir.

the class SerializingUtils method copy.

@SuppressWarnings("unchecked")
public static <T> T copy(T orig, ObjectInputStreamFactory factory) {
    T obj = null;
    ObjectOutputStream out = null;
    ObjectInputStream in = null;
    try {
        // Write the object out to a byte array
        ByteArrayIOStream byteStream = new ByteArrayIOStream();
        out = new ObjectOutputStream(byteStream);
        out.writeObject(orig);
        out.close();
        // Make an input stream from the byte array and read
        // a copy of the object back in.
        in = factory.create(byteStream.getInputStream(), null);
        obj = (T) in.readObject();
    } catch (IOException e) {
        log.error("IO exception", e);
    } catch (ClassNotFoundException cnfe) {
        log.error("class not found exception", cnfe);
    } finally {
        closeQuietly(in);
        closeQuietly(out);
    }
    return obj;
}
Also used : IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayIOStream(com.palantir.util.ByteArrayIOStream) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ByteArrayIOStream (com.palantir.util.ByteArrayIOStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1