KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > stream > encoding > ZipEncoding


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.common.stream.encoding;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.zip.Deflater JavaDoc;
31 import java.util.zip.DeflaterOutputStream JavaDoc;
32 import java.util.zip.Inflater JavaDoc;
33 import java.util.zip.InflaterInputStream JavaDoc;
34
35 /**
36  * This class defines ZipEncoding/Decoding methods
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
39  * @version 1.0
40  */

41 public class ZipEncoding
42 {
43   /**
44    * Encode data using ZIP compression
45    *
46    * @param data byte array to compress
47    * @return <code>byte[]</code> of zip encoded data
48    * @throws IOException if fails reading/writing streams
49    */

50   public static final byte[] encode(byte[] data) throws IOException JavaDoc
51   {
52     ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(data);
53     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
54     //GZIPOutputStream zipOutputStream = new GZIPOutputStream(baos);
55
DeflaterOutputStream JavaDoc zipOutputStream = new DeflaterOutputStream JavaDoc(baos,
56         new Deflater JavaDoc(Deflater.BEST_COMPRESSION, true));
57
58     //BufferedOutputStream bos = new BufferedOutputStream(zipOutputStream);
59
byte[] bdata = new byte[1024];
60     int byteCount;
61     while ((byteCount = bais.read(bdata, 0, 1024)) > -1)
62     {
63       zipOutputStream.write(bdata, 0, byteCount);
64     }
65     zipOutputStream.flush();
66     zipOutputStream.finish();
67     zipOutputStream.close();
68     return baos.toByteArray();
69   }
70
71   /**
72    * Decode data using ZIP Decompression
73    *
74    * @param data the encoded data
75    * @return <code>byte[]</code> of decoded data
76    * @throws IOException if fails
77    */

78   public static final byte[] decode(byte[] data) throws IOException JavaDoc
79   {
80     InflaterInputStream JavaDoc input = new InflaterInputStream JavaDoc(
81         new ByteArrayInputStream JavaDoc(data), new Inflater JavaDoc(true));
82     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
83
84     byte[] bdata = new byte[1024];
85     int byteCount;
86     while ((byteCount = input.read(bdata, 0, 1024)) > -1)
87       baos.write(bdata, 0, byteCount);
88     baos.flush();
89     baos.close();
90
91     return baos.toByteArray();
92   }
93 }
Popular Tags