Search code examples
javaapache-commons

Java Apache Commons users


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]


Solution

  • In commons lang:

    SerializationUtils.serialize(Serializable obj)