Here you can find the source of objectToByte(Object obj)
public static byte[] objectToByte(Object obj) throws Exception
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; public class Main { public static byte[] objectToByte(Object obj) throws Exception { ObjectOutputStream oos = null; try {//w ww . j a va 2 s. c o m ByteArrayOutputStream bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(obj); return bos.toByteArray(); } finally { if (oos != null) oos.close(); } } }