Here you can find the source of serialize(Serializable obj)
public static byte[] serialize(Serializable obj)
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; public class Main { public static byte[] serialize(Serializable obj) { ByteArrayOutputStream baos = new ByteArrayOutputStream(512); ObjectOutputStream out = null; try {/* w ww . ja v a 2 s. c om*/ out = new ObjectOutputStream(baos); out.writeObject(obj); } catch (IOException ex) { throw new RuntimeException(ex); } finally { try { if (out != null) { out.close(); } } catch (IOException ex) { // ignore close exception } } return baos.toByteArray(); } }