Here you can find the source of serialize(Object object)
Parameter | Description |
---|---|
object | the object to serialize |
public static byte[] serialize(Object object)
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Main { /**/*from w ww .j a va 2 s .com*/ * Serialize the given object to a byte array. * * @param object * the object to serialize * @return an array of bytes representing the object in a portable fashion */ public static byte[] serialize(Object object) { if (object == null) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(object); oos.flush(); } catch (IOException ex) { throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex); } return baos.toByteArray(); } }