01: /**
02: * Sequoia: Database clustering technology.
03: * Copyright (C) 2002-2004 French National Institute For Research In Computer
04: * Science And Control (INRIA).
05: * Contact: sequoia@continuent.org
06: *
07: * Licensed under the Apache License, Version 2.0 (the "License");
08: * you may not use this file except in compliance with the License.
09: * 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: */package org.continuent.sequoia.common.stream.encoding;
22:
23: import java.io.ByteArrayInputStream;
24: import java.io.ByteArrayOutputStream;
25: import java.io.IOException;
26: import java.util.zip.Deflater;
27: import java.util.zip.DeflaterOutputStream;
28: import java.util.zip.Inflater;
29: import java.util.zip.InflaterInputStream;
30:
31: /**
32: * This class defines ZipEncoding/Decoding methods
33: *
34: * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
35: * @version 1.0
36: */
37: public class ZipEncoding {
38: /**
39: * Encode data using ZIP compression
40: *
41: * @param data byte array to compress
42: * @return <code>byte[]</code> of zip encoded data
43: * @throws IOException if fails reading/writing streams
44: */
45: public static final byte[] encode(byte[] data) throws IOException {
46: ByteArrayInputStream bais = new ByteArrayInputStream(data);
47: ByteArrayOutputStream baos = new ByteArrayOutputStream();
48: //GZIPOutputStream zipOutputStream = new GZIPOutputStream(baos);
49: DeflaterOutputStream zipOutputStream = new DeflaterOutputStream(
50: baos, new Deflater(Deflater.BEST_COMPRESSION, true));
51:
52: //BufferedOutputStream bos = new BufferedOutputStream(zipOutputStream);
53: byte[] bdata = new byte[1024];
54: int byteCount;
55: while ((byteCount = bais.read(bdata, 0, 1024)) > -1) {
56: zipOutputStream.write(bdata, 0, byteCount);
57: }
58: zipOutputStream.flush();
59: zipOutputStream.finish();
60: zipOutputStream.close();
61: return baos.toByteArray();
62: }
63:
64: /**
65: * Decode data using ZIP Decompression
66: *
67: * @param data the encoded data
68: * @return <code>byte[]</code> of decoded data
69: * @throws IOException if fails
70: */
71: public static final byte[] decode(byte[] data) throws IOException {
72: InflaterInputStream input = new InflaterInputStream(
73: new ByteArrayInputStream(data), new Inflater(true));
74: ByteArrayOutputStream baos = new ByteArrayOutputStream();
75:
76: byte[] bdata = new byte[1024];
77: int byteCount;
78: while ((byteCount = input.read(bdata, 0, 1024)) > -1)
79: baos.write(bdata, 0, byteCount);
80: baos.flush();
81: baos.close();
82:
83: return baos.toByteArray();
84: }
85: }
|