1
2
3
4
5
6
7
8
9
10
11 package org.mule.util.compression;
12
13 import java.io.ByteArrayInputStream;
14 import java.io.IOException;
15 import java.util.zip.GZIPInputStream;
16 import java.util.zip.GZIPOutputStream;
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
25
26
27
28
29 public class GZipCompression implements CompressionStrategy
30 {
31 public static final int DEFAULT_BUFFER_SIZE = 32768;
32
33
34
35
36 private static final Log logger = LogFactory.getLog(GZipCompression.class);
37
38
39
40
41
42
43
44
45
46
47 public boolean isCompressed(byte[] bytes) throws IOException
48 {
49 if ((bytes == null) || (bytes.length < 2))
50 {
51 return false;
52 }
53 else
54 {
55 return ((bytes[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)));
56 }
57 }
58
59
60
61
62
63
64
65
66
67 public byte[] compressByteArray(byte[] bytes) throws IOException
68 {
69
70 if (bytes == null || isCompressed(bytes))
71 {
72
73 if (logger.isDebugEnabled())
74 {
75 logger.debug("Data already compressed; doing nothing");
76 }
77 return bytes;
78 }
79
80 if (logger.isDebugEnabled())
81 {
82 logger.debug("Compressing message of size: " + bytes.length);
83 }
84
85 ByteArrayOutputStream baos = null;
86 GZIPOutputStream gzos = null;
87
88 try
89 {
90 baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
91 gzos = new GZIPOutputStream(baos);
92
93 gzos.write(bytes, 0, bytes.length);
94 gzos.finish();
95 gzos.close();
96
97 byte[] compressedByteArray = baos.toByteArray();
98 baos.close();
99
100 if (logger.isDebugEnabled())
101 {
102 logger.debug("Compressed message to size: " + compressedByteArray.length);
103 }
104
105 return compressedByteArray;
106 }
107 catch (IOException ioex)
108 {
109 throw ioex;
110 }
111 finally
112 {
113 IOUtils.closeQuietly(gzos);
114 IOUtils.closeQuietly(baos);
115 }
116 }
117
118
119
120
121
122
123
124
125
126 public byte[] uncompressByteArray(byte[] bytes) throws IOException
127 {
128
129 if (!isCompressed(bytes))
130 {
131
132
133
134
135
136
137
138
139
140 if (logger.isDebugEnabled())
141 {
142 logger.debug("Data already uncompressed; doing nothing");
143 }
144 return bytes;
145 }
146
147 if (logger.isDebugEnabled())
148 {
149 logger.debug("Uncompressing message of size: " + bytes.length);
150 }
151
152 ByteArrayInputStream bais = null;
153 GZIPInputStream gzis = null;
154 ByteArrayOutputStream baos = null;
155
156 try
157 {
158 bais = new ByteArrayInputStream(bytes);
159 gzis = new GZIPInputStream(bais);
160 baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
161
162 IOUtils.copy(gzis, baos);
163 gzis.close();
164 bais.close();
165
166 byte[] uncompressedByteArray = baos.toByteArray();
167 baos.close();
168
169 if (logger.isDebugEnabled())
170 {
171 logger.debug("Uncompressed message to size: " + uncompressedByteArray.length);
172 }
173
174 return uncompressedByteArray;
175 }
176 catch (IOException ioex)
177 {
178 throw ioex;
179 }
180 finally
181 {
182 IOUtils.closeQuietly(gzis);
183 IOUtils.closeQuietly(bais);
184 IOUtils.closeQuietly(baos);
185 }
186 }
187
188 }