Here you can find the source of serialize(Object obj)
public static byte[] serialize(Object obj) throws IOException
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Main { public static byte[] serialize(Object obj) throws IOException { ByteArrayOutputStream baos = null; ObjectOutputStream os = null; try {//from w ww . jav a 2 s . co m baos = new ByteArrayOutputStream(512); os = new ObjectOutputStream(baos); } finally { if (baos != null) { baos.close(); } if (os != null) { os.close(); } } os.writeObject(obj); return baos.toByteArray(); } }