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

ZipEncoding.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 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): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.common.stream.encoding;
00026 
00027 import java.io.ByteArrayInputStream;
00028 import java.io.ByteArrayOutputStream;
00029 import java.io.IOException;
00030 import java.util.zip.Deflater;
00031 import java.util.zip.DeflaterOutputStream;
00032 import java.util.zip.Inflater;
00033 import java.util.zip.InflaterInputStream;
00034 
00035 /**
00036  * This class defines ZipEncoding/Decoding methods
00037  * 
00038  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00039  * @version 1.0
00040  */
00041 public class ZipEncoding
00042 {
00043   /**
00044    * Encode data using ZIP compression
00045    * 
00046    * @param data byte array to compress
00047    * @return <code>byte[]</code> of zip encoded data
00048    * @throws IOException if fails reading/writing streams
00049    */
00050   public static final byte[] encode(byte[] data) throws IOException
00051   {
00052     ByteArrayInputStream bais = new ByteArrayInputStream(data);
00053     ByteArrayOutputStream baos = new ByteArrayOutputStream();
00054     //GZIPOutputStream zipOutputStream = new GZIPOutputStream(baos);
00055     DeflaterOutputStream zipOutputStream = new DeflaterOutputStream(baos,
00056         new Deflater(Deflater.BEST_COMPRESSION, true));
00057 
00058     //BufferedOutputStream bos = new BufferedOutputStream(zipOutputStream);
00059     byte[] bdata = new byte[1024];
00060     int byteCount;
00061     while ((byteCount = bais.read(bdata, 0, 1024)) > -1)
00062     {
00063       zipOutputStream.write(bdata, 0, byteCount);
00064     }
00065     zipOutputStream.flush();
00066     zipOutputStream.finish();
00067     zipOutputStream.close();
00068     return baos.toByteArray();
00069   }
00070 
00071   /**
00072    * Decode data using ZIP Decompression
00073    * 
00074    * @param data the encoded data
00075    * @return <code>byte[]</code> of decoded data
00076    * @throws IOException if fails
00077    */
00078   public static final byte[] decode(byte[] data) throws IOException
00079   {
00080     InflaterInputStream input = new InflaterInputStream(
00081         new ByteArrayInputStream(data), new Inflater(true));
00082     ByteArrayOutputStream baos = new ByteArrayOutputStream();
00083 
00084     byte[] bdata = new byte[1024];
00085     int byteCount;
00086     while ((byteCount = input.read(bdata, 0, 1024)) > -1)
00087       baos.write(bdata, 0, byteCount);
00088     baos.flush();
00089     baos.close();
00090 
00091     return baos.toByteArray();
00092   }
00093 }

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