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 | 70 | public class GZipCompression implements CompressionStrategy |
30 | |
{ |
31 | |
public static final int DEFAULT_BUFFER_SIZE = 32768; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 4 | 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 | 56 | if ((bytes == null) || (bytes.length < 2)) |
50 | |
{ |
51 | 10 | return false; |
52 | |
} |
53 | |
else |
54 | |
{ |
55 | 46 | 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 | 32 | if (bytes == null || isCompressed(bytes)) |
71 | |
{ |
72 | |
|
73 | 2 | if (logger.isDebugEnabled()) |
74 | |
{ |
75 | 0 | logger.debug("Data already compressed; doing nothing"); |
76 | |
} |
77 | 2 | return bytes; |
78 | |
} |
79 | |
|
80 | 30 | if (logger.isDebugEnabled()) |
81 | |
{ |
82 | 0 | logger.debug("Compressing message of size: " + bytes.length); |
83 | |
} |
84 | |
|
85 | 30 | ByteArrayOutputStream baos = null; |
86 | 30 | GZIPOutputStream gzos = null; |
87 | |
|
88 | |
try |
89 | |
{ |
90 | 30 | baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE); |
91 | 30 | gzos = new GZIPOutputStream(baos); |
92 | |
|
93 | 30 | gzos.write(bytes, 0, bytes.length); |
94 | 30 | gzos.finish(); |
95 | 30 | gzos.close(); |
96 | |
|
97 | 30 | byte[] compressedByteArray = baos.toByteArray(); |
98 | 30 | baos.close(); |
99 | |
|
100 | 30 | if (logger.isDebugEnabled()) |
101 | |
{ |
102 | 0 | logger.debug("Compressed message to size: " + compressedByteArray.length); |
103 | |
} |
104 | |
|
105 | 30 | return compressedByteArray; |
106 | |
} |
107 | 0 | catch (IOException ioex) |
108 | |
{ |
109 | 0 | throw ioex; |
110 | |
} |
111 | |
finally |
112 | |
{ |
113 | 30 | IOUtils.closeQuietly(gzos); |
114 | 30 | 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 | 18 | if (!isCompressed(bytes)) |
130 | |
{ |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 2 | if (logger.isDebugEnabled()) |
141 | |
{ |
142 | 0 | logger.debug("Data already uncompressed; doing nothing"); |
143 | |
} |
144 | 2 | return bytes; |
145 | |
} |
146 | |
|
147 | 16 | if (logger.isDebugEnabled()) |
148 | |
{ |
149 | 0 | logger.debug("Uncompressing message of size: " + bytes.length); |
150 | |
} |
151 | |
|
152 | 16 | ByteArrayInputStream bais = null; |
153 | 16 | GZIPInputStream gzis = null; |
154 | 16 | ByteArrayOutputStream baos = null; |
155 | |
|
156 | |
try |
157 | |
{ |
158 | 16 | bais = new ByteArrayInputStream(bytes); |
159 | 16 | gzis = new GZIPInputStream(bais); |
160 | 16 | baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE); |
161 | |
|
162 | 16 | IOUtils.copy(gzis, baos); |
163 | 16 | gzis.close(); |
164 | 16 | bais.close(); |
165 | |
|
166 | 16 | byte[] uncompressedByteArray = baos.toByteArray(); |
167 | 16 | baos.close(); |
168 | |
|
169 | 16 | if (logger.isDebugEnabled()) |
170 | |
{ |
171 | 0 | logger.debug("Uncompressed message to size: " + uncompressedByteArray.length); |
172 | |
} |
173 | |
|
174 | 16 | return uncompressedByteArray; |
175 | |
} |
176 | 0 | catch (IOException ioex) |
177 | |
{ |
178 | 0 | throw ioex; |
179 | |
} |
180 | |
finally |
181 | |
{ |
182 | 16 | IOUtils.closeQuietly(gzis); |
183 | 16 | IOUtils.closeQuietly(bais); |
184 | 16 | IOUtils.closeQuietly(baos); |
185 | |
} |
186 | |
} |
187 | |
|
188 | |
} |