| 1 | |
package org.apache.ojb.broker.accesslayer.conversions; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
} |