00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.common.stream;
00026
00027 import java.io.ByteArrayInputStream;
00028 import java.io.ByteArrayOutputStream;
00029 import java.io.IOException;
00030 import java.io.ObjectInputStream;
00031 import java.io.ObjectOutputStream;
00032 import java.util.zip.Deflater;
00033 import java.util.zip.DeflaterOutputStream;
00034 import java.util.zip.InflaterInputStream;
00035
00036
00037
00038
00039
00040
00041
00042 public class CJDBCStream
00043 {
00044
00045 public static final int CLEAN_INTERVAL = 10;
00046
00047
00048 public static final int OBJECT = 0;
00049
00050 public static final int NULL = 1;
00051
00052 public static final int STRING_OBJECT = 2;
00053
00054 public static final int INTEGER_OBJECT = 3;
00055
00056 public static final int LONG_OBJECT = 4;
00057
00058 public static final int BOOLEAN_OBJECT = 5;
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 public static final Object decompressObject(byte[] array) throws IOException,
00069 ClassNotFoundException
00070 {
00071 ByteArrayInputStream bos = new ByteArrayInputStream(array);
00072 InflaterInputStream zip = new InflaterInputStream(bos);
00073 ObjectInputStream oos = new ObjectInputStream(zip);
00074 Object obj = oos.readObject();
00075 oos.close();
00076 bos.close();
00077 return obj;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087 public static final byte[] compressObject(Object obj) throws IOException
00088 {
00089 ByteArrayOutputStream bos = new ByteArrayOutputStream();
00090 DeflaterOutputStream zip = new DeflaterOutputStream(bos, new Deflater(
00091 Deflater.BEST_COMPRESSION));
00092 ObjectOutputStream oos = new ObjectOutputStream(zip);
00093 oos.writeObject(obj);
00094 oos.flush();
00095 oos.close();
00096 bos.close();
00097 byte[] data = bos.toByteArray();
00098 return data;
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108 public static final int countBytes(Object obj) throws IOException
00109 {
00110 ByteArrayOutputStream bos = new ByteArrayOutputStream();
00111 ObjectOutputStream oos = new ObjectOutputStream(bos);
00112 oos.writeObject(obj);
00113 oos.flush();
00114 oos.close();
00115 bos.close();
00116 return bos.size();
00117 }
00118
00119 }