Coverage Report - org.apache.ojb.broker.accesslayer.conversions.Object2Base64StringFieldConversion
 
Classes in this File Line Coverage Branch Coverage Complexity
Object2Base64StringFieldConversion
N/A
N/A
4.5
 
 1  
 package org.apache.ojb.broker.accesslayer.conversions;
 2  
 
 3  
 /* Copyright 2002-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 import java.io.ByteArrayInputStream;
 19  
 import java.io.ByteArrayOutputStream;
 20  
 import java.io.ObjectInputStream;
 21  
 import java.io.ObjectOutputStream;
 22  
 import java.util.zip.GZIPInputStream;
 23  
 import java.util.zip.GZIPOutputStream;
 24  
 
 25  
 import org.apache.ojb.broker.util.Base64;
 26  
 
 27  
 /**
 28  
  * this implementation of the FieldConversion interface converts
 29  
  * between java.lang.Objects values and char[] values in the rdbms.
 30  
  * This conversion is useful to store serialized objects in database
 31  
  * columns. For an example have a look at the mapping of
 32  
  * org.apache.ojb.odmg.collections.DlistEntry.
 33  
  *
 34  
  * @author Thomas Mahler, Scott C. Gray
 35  
  * @version $Id: Object2Base64StringFieldConversion.java,v 1.1 2007-08-24 22:17:31 ewestfal Exp $
 36  
  */
 37  
 public class Object2Base64StringFieldConversion implements FieldConversion
 38  
 {
 39  
     private ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
 40  
     private Base64.OutputStream uuOut =
 41  
         new Base64.OutputStream(byteOut, Base64.ENCODE, false);
 42  
     private GZIPOutputStream gzipOut;
 43  
     private ObjectOutputStream objOut;
 44  
 
 45  
     /*
 46  
     ** @see FieldConversion#javaToSql(Object)
 47  
     */
 48  
     public Object javaToSql(Object source)
 49  
     {
 50  
         synchronized (byteOut)
 51  
         {
 52  
             try
 53  
             {
 54  
                 if (gzipOut == null)
 55  
                 {
 56  
                     gzipOut = new GZIPOutputStream(uuOut);
 57  
                     objOut = new ObjectOutputStream(gzipOut);
 58  
                 }
 59  
 
 60  
                 /*
 61  
                 ** Clear out the byte array
 62  
                 */
 63  
                 byteOut.reset();
 64  
 
 65  
                 objOut.writeObject(source);
 66  
                 objOut.flush();
 67  
                 gzipOut.finish();
 68  
                 gzipOut.flush();
 69  
 
 70  
                 return (byteOut.toString());
 71  
             }
 72  
             catch (Throwable t)
 73  
             {
 74  
                 throw new ConversionException(t);
 75  
             }
 76  
         }
 77  
     }
 78  
 
 79  
     /*
 80  
     ** @see FieldConversion#sqlToJava(Object)
 81  
     */
 82  
     public Object sqlToJava(Object source)
 83  
     {
 84  
         try
 85  
         {
 86  
             ByteArrayInputStream stringIn =
 87  
                 new ByteArrayInputStream(((String) source).getBytes());
 88  
             Base64.InputStream uuIn =
 89  
                 new Base64.InputStream(stringIn, Base64.DECODE, false);
 90  
             GZIPInputStream gzipIn = new GZIPInputStream(uuIn);
 91  
             ObjectInputStream objIn = new ObjectInputStream(gzipIn);
 92  
             Object result = objIn.readObject();
 93  
 
 94  
             objIn.close();
 95  
             return result;
 96  
         }
 97  
         catch (Throwable t)
 98  
         {
 99  
             throw new ConversionException(t);
 100  
         }
 101  
     }
 102  
 
 103  
 }