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