Here you can find the source of serializeToByteArray(Object obj)
public static byte[] serializeToByteArray(Object obj) throws NotSerializableException
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.NotSerializableException; import java.io.ObjectOutputStream; public class Main { public static byte[] serializeToByteArray(Object obj) throws NotSerializableException { try {/*from ww w .java 2s . c o m*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(obj); return baos.toByteArray(); } catch (NotSerializableException e) { e.fillInStackTrace(); throw e; } catch (IOException e) { throw new Error("IOException writing to a byte array!"); } } public static byte[] toByteArray(Object obj) throws NotSerializableException { return serializeToByteArray(obj); } }