Java Object Serialize serialize(Object o)

Here you can find the source of serialize(Object o)

Description

Serializes an object.

License

LGPL

Parameter

Parameter Description
o The object or null

Exception

Parameter Description
IOException In case of a writing or serialization error

Return

The bytes

Declaration

public static byte[] serialize(Object o) throws IOException 

Method Source Code

//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();
    }
}

Related

  1. serialize(final String path, final Object object)
  2. serialize(final String pFilename, final T pObject)
  3. serialize(java.io.Serializable obj)
  4. serialize(Object c, ScriptableObject scope)
  5. serialize(Object o)
  6. serialize(Object obj)
  7. serialize(Object obj)
  8. serialize(Object obj)
  9. serialize(Object obj)