/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer(s): ____________________________________.
 * Contributor(s): Vincent Trussart (vincent@linuxfreak.com)
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */


/**
 * Contains static methods to marshall/unmarshall objects to/from a byte[]
 *
 * Created: Thu Aug 12 16:49:04 1999
 */

package org.objectweb.jonas_ejb.lib;


import java.io.*;


public class MarshallTool  {

    /**
     * ByteArrayOutpuStream internal buffer size.  
     */   
    private static int _buffSize = 4096;

    /**
     * Converts an instance of java.io.Serializable to a serialized byte array
     *
     * @param o Object to serialize
     * @return byte array containing the serialization of the Serializable object
     */
    public static synchronized byte[] toBytes(Serializable o) throws IOException {

	ByteArrayOutputStream bas = new ByteArrayOutputStream(_buffSize);
	ObjectOutputStream oos = new ObjectOutputStream(bas);

	oos.writeObject(o);
	oos.close();

	return bas.toByteArray();
    }

    /**
     * Converts byte array to an instance of java.io.Serializable
     *
     * @param bytes A byte array containing the a serialized object
     * @return the unmarshalled object
     */
    public static synchronized Serializable fromBytes(byte[] bytes) throws IOException, ClassNotFoundException {

	if (bytes == null) return null;

	ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
	ObjectInputStream ois = new ObjectInputStream(bis);

	return (Serializable)ois.readObject();
    }
    
} // MarshallTool

