Java Object Serialize serialize(final Serializable object)

Here you can find the source of serialize(final Serializable object)

Description

Serialize an object.

License

Apache License

Parameter

Parameter Description
object The object to be serialized

Return

the +byte[]+ containing the object

Declaration

public static byte[] serialize(final Serializable object) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class Main {
    /**//  w w  w.ja  va 2s .co  m
     * Serialize an object.
     * @param object The object to be serialized
     * @return the +byte[]+ containing the object
     * @since 5.0.0
     */
    public static byte[] serialize(final Serializable object) {
        final ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
        serialize(object, outBytes);
        return outBytes.toByteArray();
    }

    /**
     * Serialize an object.
     * @param object The object to be serialized
     * @param outputStream The stream to receive the object
     * @since 5.0.0
     */
    public static void serialize(final Serializable object, final OutputStream outputStream) {
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(outputStream);
            out.writeObject(object);
        } catch (final IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (final IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

Related

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