Here you can find the source of serializeToBytes(Serializable object)
public static byte[] serializeToBytes(Serializable object) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; public class Main { public static byte[] serializeToBytes(Serializable object) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = null;/*from ww w .ja v a2 s. c o m*/ try { out = new ObjectOutputStream(bos); out.writeObject(object); return bos.toByteArray(); } finally { try { if (out != null) { out.close(); } } catch (IOException ex) { // ignore close exception } try { bos.close(); } catch (IOException ex) { // ignore close exception } } } }