package org.jboss.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import org.jboss.util.stream.CustomObjectInputStreamWithClassloader;
import org.jboss.util.stream.CustomObjectOutputStream;
public class Conversion
{
public static byte[] toByteArray(Object obj)
{
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new CustomObjectOutputStream(os);
oos.writeObject(obj);
oos.flush();
byte[] a = os.toByteArray();
os.close();
return a;
}
catch (IOException ioe) {
throw new RuntimeException("Object id serialization error:\n" + ioe);
}
}
public static Object toObject(byte[] a, ClassLoader cl)
throws IOException, ClassNotFoundException
{
ByteArrayInputStream is = new ByteArrayInputStream(a);
ObjectInputStream ois =
new CustomObjectInputStreamWithClassloader(is, cl);
Object obj = ois.readObject();
is.close();
return obj;
}
public static Object toObject(byte[] a)
throws IOException, ClassNotFoundException
{
return toObject(a, Thread.currentThread().getContextClassLoader());
}
}