Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

CJDBCStream.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2005 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk
00022  * Contributor(s): Emmanuel Cecchet.
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  * This is a useful stream class to compress and decompress object to a stream.
00038  * 
00039  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00040  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00041  */
00042 public class CJDBCStream
00043 {
00044   /** Clean the stream after CLEAN_INTERVAL call to writeUnshared */
00045   public static final int CLEAN_INTERVAL = 10;
00046 
00047   /** Identifier of an object on the stream */
00048   public static final int OBJECT         = 0;
00049   /** Identifier of <code>null</code> on the stream */
00050   public static final int NULL           = 1;
00051   /** Identifier of a <code>String</code> object on the stream */
00052   public static final int STRING_OBJECT  = 2;
00053   /** Identifier of an <code>Integer</code> object on the stream */
00054   public static final int INTEGER_OBJECT = 3;
00055   /** Identifier of a <code>Long</code> object on the stream */
00056   public static final int LONG_OBJECT    = 4;
00057   /** Identifier of a <code>Boolean</code> object on the stream */
00058   public static final int BOOLEAN_OBJECT = 5;
00059 
00060   /**
00061    * Decompress an object from its byte-compressed form to java object
00062    * 
00063    * @param array of bytes
00064    * @return the object if it can be retrieved
00065    * @throws IOException if fails to read
00066    * @throws ClassNotFoundException if class of object does not exist
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    * Compress an object from its java form to byte array
00082    * 
00083    * @param obj to compress
00084    * @return byte array
00085    * @throws IOException if fails to read
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    * Statistic method to count the number of bytes of a class.
00103    * 
00104    * @param obj to count bytes
00105    * @return the number of bytes
00106    * @throws IOException if fails to read
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 }

Generated on Mon Apr 11 22:01:30 2005 for C-JDBC by  doxygen 1.3.9.1