KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.8.2.1 2005/12/21 22:23:38 tomdz Exp $
36  */

37 public class Object2Base64StringFieldConversion implements FieldConversion
38 {
39     private ByteArrayOutputStream JavaDoc byteOut = new ByteArrayOutputStream JavaDoc();
40     private Base64.OutputStream uuOut =
41         new Base64.OutputStream(byteOut, Base64.ENCODE, false);
42     private GZIPOutputStream JavaDoc gzipOut;
43     private ObjectOutputStream JavaDoc objOut;
44
45     /*
46     ** @see FieldConversion#javaToSql(Object)
47     */

48     public Object JavaDoc javaToSql(Object JavaDoc source)
49     {
50         synchronized (byteOut)
51         {
52             try
53             {
54                 if (gzipOut == null)
55                 {
56                     gzipOut = new GZIPOutputStream JavaDoc(uuOut);
57                     objOut = new ObjectOutputStream JavaDoc(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 JavaDoc t)
73             {
74                 throw new ConversionException(t);
75             }
76         }
77     }
78
79     /*
80     ** @see FieldConversion#sqlToJava(Object)
81     */

82     public Object JavaDoc sqlToJava(Object JavaDoc source)
83     {
84         try
85         {
86             ByteArrayInputStream JavaDoc stringIn =
87                 new ByteArrayInputStream JavaDoc(((String JavaDoc) source).getBytes());
88             Base64.InputStream uuIn =
89                 new Base64.InputStream(stringIn, Base64.DECODE, false);
90             GZIPInputStream JavaDoc gzipIn = new GZIPInputStream JavaDoc(uuIn);
91             ObjectInputStream JavaDoc objIn = new ObjectInputStream JavaDoc(gzipIn);
92             Object JavaDoc result = objIn.readObject();
93
94             objIn.close();
95             return result;
96         }
97         catch (Throwable JavaDoc t)
98         {
99             throw new ConversionException(t);
100         }
101     }
102
103 }
104
Popular Tags