001    package org.apache.ojb.broker.accesslayer.conversions;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import java.io.ByteArrayInputStream;
019    import java.io.ByteArrayOutputStream;
020    import java.io.ObjectInputStream;
021    import java.io.ObjectOutputStream;
022    import java.util.zip.GZIPInputStream;
023    import java.util.zip.GZIPOutputStream;
024    
025    /**
026     * This implementation of the FieldConversion interface converts
027     * between java.lang.Objects values and byte[] values in the rdbms.
028     * This conversion is useful to store serialized objects in database
029     * columns.
030     * <p/>
031     * NOTE: This implementation use {@link java.util.zip.GZIPOutputStream} and
032     * {@link java.util.zip.GZIPInputStream} to compress data.
033     *
034     * @see Object2ByteArrUncompressedFieldConversion
035     * @author Thomas Mahler
036     * @version $Id: Object2ByteArrFieldConversion.java,v 1.1 2007-08-24 22:17:31 ewestfal Exp $
037     */
038    public class Object2ByteArrFieldConversion implements FieldConversion
039    {
040    
041        /*
042         * @see FieldConversion#javaToSql(Object)
043         */
044        public Object javaToSql(Object source)
045        {
046            if (source == null)
047                return null;
048            try
049            {
050                ByteArrayOutputStream bao = new ByteArrayOutputStream();
051                GZIPOutputStream gos = new GZIPOutputStream(bao);
052                ObjectOutputStream oos = new ObjectOutputStream(gos);
053                oos.writeObject(source);
054                oos.close();
055                gos.close();
056                bao.close();
057                byte[] result = bao.toByteArray();
058                return result;
059            }
060            catch (Throwable t)
061            {
062                throw new ConversionException(t);
063            }
064        }
065    
066        /*
067         * @see FieldConversion#sqlToJava(Object)
068         */
069        public Object sqlToJava(Object source)
070        {
071             if (source == null)
072                return null;
073            try
074            {
075                ByteArrayInputStream bais = new ByteArrayInputStream((byte[]) source);
076                GZIPInputStream gis = new GZIPInputStream(bais);
077                ObjectInputStream ois = new ObjectInputStream(gis);
078                Object result = ois.readObject();
079                ois.close();
080                gis.close();
081                bais.close();
082                return result;
083            }
084            catch (Throwable t)
085            {
086                throw new ConversionException(t);
087            }
088        }
089    
090    }