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