001package org.apache.commons.jcs3.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 java.io.IOException;
023
024import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
025import org.apache.commons.jcs3.utils.zip.CompressionUtil;
026
027/**
028 * Performs default serialization and de-serialization. It gzips the value.
029 */
030public class CompressingSerializer extends StandardSerializer
031{
032    /** Wrapped serializer */
033    private final IElementSerializer serializer;
034
035
036    /**
037     * Default constructor
038     */
039    public CompressingSerializer()
040    {
041        this(new StandardSerializer());
042    }
043
044    /**
045     * Wrapper constructor
046     *
047     * @param serializer the wrapped serializer
048     * @since 3.1
049     */
050    public CompressingSerializer(IElementSerializer serializer)
051    {
052        this.serializer = serializer;
053    }
054
055    /**
056     * Serializes an object using default serialization. Compresses the byte array.
057     * <p>
058     * @param obj object
059     * @return byte[]
060     * @throws IOException on i/o problem
061     */
062    @Override
063    public <T> byte[] serialize( final T obj )
064        throws IOException
065    {
066        final byte[] uncompressed = serializer.serialize(obj);
067        return CompressionUtil.compressByteArray( uncompressed );
068    }
069
070    /**
071     * Uses default de-serialization to turn a byte array into an object. Decompresses the value
072     * first. All exceptions are converted into IOExceptions.
073     * <p>
074     * @param data data bytes
075     * @param loader class loader to use
076     * @return Object
077     * @throws IOException on i/o problem
078     * @throws ClassNotFoundException if class is not found during deserialization
079     */
080    @Override
081    public <T> T deSerialize( final byte[] data, final ClassLoader loader )
082        throws IOException, ClassNotFoundException
083    {
084        if ( data == null )
085        {
086            return null;
087        }
088
089        final byte[] decompressedByteArray = CompressionUtil.decompressByteArray( data );
090        return serializer.deSerialize(decompressedByteArray, loader);
091    }
092}