views:

45

answers:

1

Is there anything in apache commons to convert a Object to byte array, like the following method does?

public static byte[] toByteArray(Object obj) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.flush();
byte[] data = baos.toByteArray();
return data;
}

[try-finally block closing buffers were omitted to simplify]

+2  A: 

In commons lang:

SerializationUtils.serialize(Serializable obj)
Chris Lercher