View Javadoc

1   /*
2    * $Id: GZipCompressTransformer.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transformers.compression;
12  
13  import org.mule.umo.transformer.TransformerException;
14  import org.mule.util.compression.GZipCompression;
15  
16  import java.io.IOException;
17  import java.io.Serializable;
18  
19  import org.apache.commons.lang.SerializationUtils;
20  
21  /**
22   * <code>GZipCompressTransformer</code> is a transformer compressing objects into
23   * byte arrays.
24   */
25  public class GZipCompressTransformer extends AbstractCompressionTransformer
26  {
27  
28      public GZipCompressTransformer()
29      {
30          super();
31          this.setStrategy(new GZipCompression());
32          this.registerSourceType(Serializable.class);
33          this.registerSourceType(byte[].class);
34          this.setReturnClass(byte[].class);
35      }
36  
37      public Object doTransform(Object src, String encoding) throws TransformerException
38      {
39          try
40          {
41              byte[] data = null;
42              if (src instanceof byte[])
43              {
44                  data = (byte[]) src;
45              }
46              else
47              {
48                  data = SerializationUtils.serialize((Serializable) src);
49              }
50              return this.getStrategy().compressByteArray(data);
51          }
52          catch (IOException ioex)
53          {
54              throw new TransformerException(this, ioex);
55          }
56      }
57  
58  }