001package org.apache.commons.jcs.utils.serialization;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.jcs.engine.behavior.IElementSerializer;
023import org.apache.commons.jcs.io.ObjectInputStreamClassLoaderAware;
024
025import java.io.BufferedInputStream;
026import java.io.ByteArrayInputStream;
027import java.io.ByteArrayOutputStream;
028import java.io.IOException;
029import java.io.ObjectInputStream;
030import java.io.ObjectOutputStream;
031
032/**
033 * Performs default serialization and de-serialization.
034 * <p>
035 * @author Aaron Smuts
036 */
037public class StandardSerializer
038    implements IElementSerializer
039{
040    /**
041     * Serializes an object using default serialization.
042     * <p>
043     * @param obj
044     * @return byte[]
045     * @throws IOException
046     */
047    @Override
048    public <T> byte[] serialize( T obj )
049        throws IOException
050    {
051        ByteArrayOutputStream baos = new ByteArrayOutputStream();
052        ObjectOutputStream oos = new ObjectOutputStream( baos );
053        try
054        {
055            oos.writeObject( obj );
056        }
057        finally
058        {
059            oos.close();
060        }
061        return baos.toByteArray();
062    }
063
064    /**
065     * Uses default de-serialization to turn a byte array into an object. All exceptions are
066     * converted into IOExceptions.
067     * <p>
068     * @param data
069     * @return Object
070     * @throws IOException
071     * @throws ClassNotFoundException
072     */
073    @Override
074    public <T> T deSerialize( byte[] data, ClassLoader loader )
075        throws IOException, ClassNotFoundException
076    {
077        ByteArrayInputStream bais = new ByteArrayInputStream( data );
078        BufferedInputStream bis = new BufferedInputStream( bais );
079        ObjectInputStream ois = new ObjectInputStreamClassLoaderAware( bis, loader );
080        try
081        {
082            @SuppressWarnings("unchecked") // Need to cast from Object
083            T readObject = (T) ois.readObject();
084            return readObject;
085        }
086        finally
087        {
088            ois.close();
089        }
090    }
091}