1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transformer.compression; |
12 | |
|
13 | |
import org.mule.api.transformer.TransformerException; |
14 | |
import org.mule.util.IOUtils; |
15 | |
import org.mule.util.compression.GZipCompression; |
16 | |
|
17 | |
import java.io.IOException; |
18 | |
import java.io.InputStream; |
19 | |
import java.io.Serializable; |
20 | |
|
21 | |
import org.apache.commons.lang.SerializationUtils; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class GZipCompressTransformer extends AbstractCompressionTransformer |
28 | |
{ |
29 | |
|
30 | |
public GZipCompressTransformer() |
31 | |
{ |
32 | 20 | super(); |
33 | 20 | this.setStrategy(new GZipCompression()); |
34 | 20 | this.registerSourceType(Serializable.class); |
35 | 20 | this.registerSourceType(byte[].class); |
36 | 20 | this.registerSourceType(InputStream.class); |
37 | 20 | this.setReturnClass(byte[].class); |
38 | 20 | } |
39 | |
|
40 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
41 | |
{ |
42 | |
try |
43 | |
{ |
44 | 18 | byte[] data = null; |
45 | 18 | if (src instanceof byte[]) |
46 | |
{ |
47 | 4 | data = (byte[]) src; |
48 | |
} |
49 | 14 | else if (src instanceof InputStream) |
50 | |
{ |
51 | 2 | InputStream input = (InputStream)src; |
52 | |
try |
53 | |
{ |
54 | 2 | data = IOUtils.toByteArray(input); |
55 | |
} |
56 | |
finally |
57 | |
{ |
58 | 2 | input.close(); |
59 | 2 | } |
60 | 2 | } |
61 | |
else |
62 | |
{ |
63 | 12 | data = SerializationUtils.serialize((Serializable) src); |
64 | |
} |
65 | 18 | return this.getStrategy().compressByteArray(data); |
66 | |
} |
67 | 0 | catch (IOException ioex) |
68 | |
{ |
69 | 0 | throw new TransformerException(this, ioex); |
70 | |
} |
71 | |
} |
72 | |
|
73 | |
} |