Here you can find the source of serialize(Object obj)
public static byte[] serialize(Object obj)
//package com.java2s; /*//w w w. j av a 2s .c om * Copyright 1999-2101 Alibaba.com. All rights reserved. * This software is the confidential and proprietary information of Alibaba.com ("Confidential Information"). * You shall not disclose such Confidential Information and shall use it only in accordance with the terms of * the license agreement you entered into with Alibaba.com. */ import java.io.*; public class Main { public static byte[] serialize(Object obj) { ByteArrayOutputStream baos = null; ObjectOutputStream oos = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(obj); return baos.toByteArray(); } catch (RuntimeException re) { throw re; } catch (Exception e) { throw new RuntimeException(e); } finally { closeQuietly(baos); closeQuietly(oos); } } private static void closeQuietly(Closeable c) { if (c == null) return; try { c.close(); } catch (IOException e) { } } }