Java tutorial
//package com.java2s; import java.io.*; public class Main { /** * Serialize an object into bytes. * @param o Object to be serialized. * @return Serialized stream of bytes. * @throws IOException */ public static byte[] objectToBytes(Serializable o) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(out); oo.writeObject(o); oo.close(); return out.toByteArray(); } }