View Javadoc

1   /*
2    * $Id: ByteArrayToHexString.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.simple;
12  
13  import org.mule.transformers.AbstractTransformer;
14  import org.mule.umo.transformer.TransformerException;
15  import org.mule.util.StringUtils;
16  
17  /**
18   * Converts a Byte array to a Hex String.
19   */
20  public class ByteArrayToHexString extends AbstractTransformer
21  {
22      private volatile boolean upperCase = false;
23  
24      public ByteArrayToHexString()
25      {
26          registerSourceType(byte[].class);
27          setReturnClass(String.class);
28      }
29  
30      public boolean getUpperCase()
31      {
32          return upperCase;
33      }
34  
35      public void setUpperCase(boolean value)
36      {
37          upperCase = value;
38      }
39  
40      protected Object doTransform(Object src, String encoding) throws TransformerException
41      {
42          if (src == null)
43          {
44              return StringUtils.EMPTY;
45          }
46  
47          try
48          {
49              return StringUtils.toHexString((byte[]) src, upperCase);
50          }
51          catch (Exception ex)
52          {
53              throw new TransformerException(this, ex);
54          }
55      }
56  
57  }