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