001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: Serialization.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.util.marshalling;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.IOException;
029: import java.io.ObjectOutputStream;
030: import java.io.Serializable;
031:
032: /**
033: * Allow to get the bytes of an object or to recreate an object from bytes.<br>
034: * It also used a custom reader when recreating object as it uses the context
035: * classloader in order to have an object with the right classloader.
036: * @author Florent Benoit
037: */
038: public final class Serialization {
039:
040: /**
041: * Utility class, no public constructor.
042: */
043: private Serialization() {
044:
045: }
046:
047: /**
048: * Gets an array of bytes corresponding to the given object.
049: * @param object the object to serialize
050: * @return an array of bytes.
051: * @throws IOException if the object can't be turned into an array of bytes.
052: */
053: public static byte[] storeObject(final Serializable object)
054: throws IOException {
055: ByteArrayOutputStream baos = new ByteArrayOutputStream();
056: ObjectOutputStream oos = null;
057: try {
058: oos = new ObjectOutputStream(baos);
059: oos.writeObject(object);
060: oos.flush();
061: return baos.toByteArray();
062: } finally {
063: if (oos != null) {
064: oos.close();
065: }
066: if (baos != null) {
067: baos.close();
068: }
069: }
070: }
071:
072: /**
073: * Transforms the given array of bytes into an object.
074: * @param bytes given array of bytes representing the object
075: * @return object which has been unmarshalled.
076: * @throws IOException if marshalling fails
077: * @throws ClassNotFoundException if the class required for loading the object is not found.
078: */
079: public static Object loadObject(final byte[] bytes)
080: throws IOException, ClassNotFoundException {
081: // Do nothing if there aren't any bytes.
082: if (bytes == null) {
083: return null;
084: }
085:
086: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
087: CtxClassLoaderObjectInputStream ctxClassLoaderObjectInputStream = new CtxClassLoaderObjectInputStream(
088: bais);
089: try {
090: return ctxClassLoaderObjectInputStream.readObject();
091: } finally {
092: if (ctxClassLoaderObjectInputStream != null) {
093: ctxClassLoaderObjectInputStream.close();
094: }
095: if (ctxClassLoaderObjectInputStream != null) {
096: bais.close();
097: }
098:
099: }
100:
101: }
102:
103: }
|