| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Object2ByteArrFieldConversion |
|
| 6.0;6 |
| 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 | /** | |
| 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.1 2007-08-24 22:17:31 ewestfal Exp $ | |
| 37 | */ | |
| 38 | public class Object2ByteArrFieldConversion implements FieldConversion | |
| 39 | { | |
| 40 | ||
| 41 | /* | |
| 42 | * @see FieldConversion#javaToSql(Object) | |
| 43 | */ | |
| 44 | public Object javaToSql(Object source) | |
| 45 | { | |
| 46 | if (source == null) | |
| 47 | return null; | |
| 48 | try | |
| 49 | { | |
| 50 | ByteArrayOutputStream bao = new ByteArrayOutputStream(); | |
| 51 | GZIPOutputStream gos = new GZIPOutputStream(bao); | |
| 52 | ObjectOutputStream oos = new ObjectOutputStream(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 t) | |
| 61 | { | |
| 62 | throw new ConversionException(t); | |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | /* | |
| 67 | * @see FieldConversion#sqlToJava(Object) | |
| 68 | */ | |
| 69 | public Object sqlToJava(Object source) | |
| 70 | { | |
| 71 | if (source == null) | |
| 72 | return null; | |
| 73 | try | |
| 74 | { | |
| 75 | ByteArrayInputStream bais = new ByteArrayInputStream((byte[]) source); | |
| 76 | GZIPInputStream gis = new GZIPInputStream(bais); | |
| 77 | ObjectInputStream ois = new ObjectInputStream(gis); | |
| 78 | Object result = ois.readObject(); | |
| 79 | ois.close(); | |
| 80 | gis.close(); | |
| 81 | bais.close(); | |
| 82 | return result; | |
| 83 | } | |
| 84 | catch (Throwable t) | |
| 85 | { | |
| 86 | throw new ConversionException(t); | |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | } |