public static byte[] serializeAsByteArray(Object b) throws IOException {
  ByteArrayOutputStream bos=new ByteArrayOutputStream();
  ObjectOutputStream out=new ObjectOutputStream(bos);
  try {
    out.writeObject(b);
  }
  finally {
    out.close();
  }
  return bos.toByteArray();
}

public static byte [ ] serializeAsByteArray ( Object b ) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ; ObjectOutputStream out = new ObjectOutputStream ( bos ) ; try { out . writeObject ( b ) ; } finally { out . close ( ) ; } return bos . toByteArray ( ) ; }
serialize an object as a raw byte array
