Here you can find the source of serialize(Serializable obj)
public static byte[] serialize(Serializable obj) throws Exception
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Main { public static byte[] serialize(Serializable obj) throws Exception { ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); ObjectOutputStream output = new ObjectOutputStream(byteOutput); output.writeObject(obj);//w w w .j a v a 2 s . c o m byte[] bytes = byteOutput.toByteArray(); byteOutput.close(); output.close(); return bytes; } }