KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > accesslayer > conversions > Object2ByteArrFieldConversion


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 JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.util.zip.GZIPInputStream JavaDoc;
23 import java.util.zip.GZIPOutputStream JavaDoc;
24
25 /**
26  * This implementation of the FieldConversion interface converts
27  * between java.lang.Objects values and byte[] values in the rdbms.
28  * This conversion is useful to store serialized objects in database
29  * columns.
30  * <p/>
31  * NOTE: This implementation use {@link java.util.zip.GZIPOutputStream} and
32  * {@link java.util.zip.GZIPInputStream} to compress data.
33  *
34  * @see Object2ByteArrUncompressedFieldConversion
35  * @author Thomas Mahler
36  * @version $Id: Object2ByteArrFieldConversion.java,v 1.6.2.3 2005/12/21 22:23:38 tomdz Exp $
37  */

38 public class Object2ByteArrFieldConversion implements FieldConversion
39 {
40
41     /*
42      * @see FieldConversion#javaToSql(Object)
43      */

44     public Object JavaDoc javaToSql(Object JavaDoc source)
45     {
46         if (source == null)
47             return null;
48         try
49         {
50             ByteArrayOutputStream JavaDoc bao = new ByteArrayOutputStream JavaDoc();
51             GZIPOutputStream JavaDoc gos = new GZIPOutputStream JavaDoc(bao);
52             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(gos);
53             oos.writeObject(source);
54             oos.close();
55             gos.close();
56             bao.close();
57             byte[] result = bao.toByteArray();
58             return result;
59         }
60         catch (Throwable JavaDoc t)
61         {
62             throw new ConversionException(t);
63         }
64     }
65
66     /*
67      * @see FieldConversion#sqlToJava(Object)
68      */

69     public Object JavaDoc sqlToJava(Object JavaDoc source)
70     {
71          if (source == null)
72             return null;
73         try
74         {
75             ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc((byte[]) source);
76             GZIPInputStream JavaDoc gis = new GZIPInputStream JavaDoc(bais);
77             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(gis);
78             Object JavaDoc result = ois.readObject();
79             ois.close();
80             gis.close();
81             bais.close();
82             return result;
83         }
84         catch (Throwable JavaDoc t)
85         {
86             throw new ConversionException(t);
87         }
88     }
89
90 }
91
Popular Tags