KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > stream > encoding > ZipEncoding


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Nicolas Modrzyk
20  * Contributor(s): ______________________.
21  */

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

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

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

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