Here you can find the source of serialize(Object o)
Parameter | Description |
---|---|
o | The object or null |
Parameter | Description |
---|---|
IOException | In case of a writing or serialization error |
public static byte[] serialize(Object o) throws IOException
//package com.java2s; /**/*from www . j a va 2 s . c o m*/ * Copyright 2009-2016 Three Crickets LLC. * <p> * The contents of this file are subject to the terms of the LGPL version 3.0: * http://www.gnu.org/copyleft/lesser.html * <p> * Alternatively, you can obtain a royalty free commercial license with less * limitations, transferable or non-transferable, directly from Three Crickets * at http://threecrickets.com/ */ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Main { /** * Serializes an object. * * @param o * The object or null * @return The bytes * @throws IOException * In case of a writing or serialization error */ public static byte[] serialize(Object o) throws IOException { if (o == null) return new byte[0]; ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); try { ObjectOutputStream stream = new ObjectOutputStream(byteStream); try { stream.writeObject(o); } finally { stream.close(); } } finally { byteStream.close(); } return byteStream.toByteArray(); } }