Java Object Serialize serialize(Object obj)

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

Description

Serializes a serializable object into a byte array.

License

Open Source License

Parameter

Parameter Description
obj the object to serialize

Exception

Parameter Description
IOException if an I/O error occurs.

Declaration

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

Method Source Code

//package com.java2s;
/*//w ww.  ja v a  2 s .co m
* Copyright (c) 2000 - 2005 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  
*
*/

import java.io.IOException;

import java.io.OutputStream;

import java.io.ObjectOutputStream;

import java.io.ByteArrayOutputStream;

public class Main {
    /**
     * Serializes a serializable object into a byte array.
     *
     * @param obj the object to serialize
     * @throws IOException if an I/O error occurs.
     */
    public static byte[] serialize(Object obj) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        serialize(out, obj);
        return out.toByteArray();
    }

    /**
     * Serializes a serializable object
     *
     * @param out the stream to write seralized data to
     * @param obj the object to serialize
     * @throws IOException if an I/O error occurs.
     */
    public static void serialize(OutputStream out, Object obj) throws IOException {
        ObjectOutputStream serializer = new ObjectOutputStream(out);
        serializer.writeObject(obj);
    }
}

Related

  1. serialize(Object obj)
  2. serialize(Object obj)
  3. serialize(Object obj)
  4. serialize(Object obj)
  5. serialize(Object obj)
  6. serialize(Object obj)
  7. serialize(Object obj, String file)
  8. serialize(Object obj, String fileName)
  9. serialize(Object obj, String fileName)