KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > compression > GZipCompression


1 /*
2  * $Id: GZipCompression.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util.compression;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.zip.GZIPInputStream JavaDoc;
16 import java.util.zip.GZIPOutputStream JavaDoc;
17
18 import org.apache.commons.io.IOUtils;
19 import org.apache.commons.io.output.ByteArrayOutputStream;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 /**
24  * <code>GZipCompression</code> is a CompressionStrategy implementation using the
25  * GZip library included in the JDK java.util.zip. This is the default
26  * CompressionStrategy used by the CompressionHelper discovery when no other
27  * implementation is discovered.
28  */

29 public class GZipCompression implements CompressionStrategy
30 {
31     /**
32      * The logger for this class
33      */

34     private static final Log logger = LogFactory.getLog(GZipCompression.class);
35
36     /**
37      * Determines if a byte array is compressed. The java.util.zip GZip
38      * implementaiton does not expose the GZip header so it is difficult to determine
39      * if a string is compressed.
40      *
41      * @param bytes an array of bytes
42      * @return true if the array is compressed or false otherwise
43      * @throws java.io.IOException if the byte array couldn't be read
44      */

45     public boolean isCompressed(byte[] bytes) throws IOException JavaDoc
46     {
47         if ((bytes == null) || (bytes.length < 2))
48         {
49             return false;
50         }
51         else
52         {
53             return ((bytes[0] == (byte)(GZIPInputStream.GZIP_MAGIC)) && (bytes[1] == (byte)(GZIPInputStream.GZIP_MAGIC >> 8)));
54         }
55     }
56
57     /**
58      * Used for compressing a byte array into a new byte array using GZIP
59      *
60      * @param bytes An array of bytes to compress
61      * @return a compressed byte array
62      * @throws java.io.IOException if it fails to write to a GZIPOutputStream
63      * @see java.util.zip.GZIPOutputStream
64      */

65     public byte[] compressByteArray(byte[] bytes) throws IOException JavaDoc
66     {
67         // TODO add strict behaviour as option
68
if (bytes == null || isCompressed(bytes))
69         {
70             // nothing to compress
71
if (logger.isDebugEnabled())
72             {
73                 logger.debug("Data already compressed; doing nothing");
74             }
75             return bytes;
76         }
77
78         if (logger.isDebugEnabled())
79         {
80             logger.debug("Compressing message of size: " + bytes.length);
81         }
82
83         ByteArrayOutputStream baos = null;
84         GZIPOutputStream JavaDoc gzos = null;
85
86         try
87         {
88             baos = new ByteArrayOutputStream(32768);
89             gzos = new GZIPOutputStream JavaDoc(baos);
90
91             gzos.write(bytes, 0, bytes.length);
92             gzos.finish();
93             gzos.close();
94
95             byte[] compressedByteArray = baos.toByteArray();
96             baos.close();
97
98             if (logger.isDebugEnabled())
99             {
100                 logger.debug("Compressed message to size: " + compressedByteArray.length);
101             }
102
103             return compressedByteArray;
104         }
105         catch (IOException JavaDoc ioex)
106         {
107             throw ioex;
108         }
109         finally
110         {
111             IOUtils.closeQuietly(gzos);
112             IOUtils.closeQuietly(baos);
113         }
114     }
115
116     /**
117      * Used for uncompressing a byte array into a uncompressed byte array using GZIP
118      *
119      * @param bytes An array of bytes to uncompress
120      * @return an uncompressed byte array
121      * @throws java.io.IOException if it fails to read from a GZIPInputStream
122      * @see java.util.zip.GZIPInputStream
123      */

124     public byte[] uncompressByteArray(byte[] bytes) throws IOException JavaDoc
125     {
126         // TODO add strict behaviour as option
127
if (!isCompressed(bytes))
128         {
129             /*
130              * if (strict) { // throw a specific exception here to allow users of
131              * this method to // diffientiate between general IOExceptions and an
132              * invalid format logger.warn("Data is not of type GZIP compressed." + "
133              * The data may not have been compressed in the first place."); throw new
134              * CompressionException("Not in GZIP format"); }
135              */

136
137             // nothing to uncompress
138
if (logger.isDebugEnabled())
139             {
140                 logger.debug("Data already uncompressed; doing nothing");
141             }
142             return bytes;
143         }
144
145         if (logger.isDebugEnabled())
146         {
147             logger.debug("Uncompressing message of size: " + bytes.length);
148         }
149
150         ByteArrayInputStream JavaDoc bais = null;
151         GZIPInputStream JavaDoc gzis = null;
152         ByteArrayOutputStream baos = null;
153
154         try
155         {
156             bais = new ByteArrayInputStream JavaDoc(bytes);
157             gzis = new GZIPInputStream JavaDoc(bais);
158             baos = new ByteArrayOutputStream(32768);
159
160             IOUtils.copy(gzis, baos);
161             gzis.close();
162             bais.close();
163
164             byte[] uncompressedByteArray = baos.toByteArray();
165             baos.close();
166
167             if (logger.isDebugEnabled())
168             {
169                 logger.debug("Uncompressed message to size: " + uncompressedByteArray.length);
170             }
171
172             return uncompressedByteArray;
173         }
174         catch (IOException JavaDoc ioex)
175         {
176             throw ioex;
177         }
178         finally
179         {
180             IOUtils.closeQuietly(gzis);
181             IOUtils.closeQuietly(bais);
182             IOUtils.closeQuietly(baos);
183         }
184     }
185
186 }
187
Popular Tags