View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformer.simple;
8   
9   import org.mule.api.transformer.TransformerException;
10  import org.mule.transformer.AbstractTransformer;
11  import org.mule.transformer.types.DataTypeFactory;
12  import org.mule.util.ArrayUtils;
13  import org.mule.util.StringUtils;
14  
15  /**
16   * Converts a Hex String to a Byte array
17   */
18  public class HexStringToByteArray extends AbstractTransformer
19  {
20      public HexStringToByteArray()
21      {
22          registerSourceType(DataTypeFactory.STRING);
23          setReturnDataType(DataTypeFactory.BYTE_ARRAY);
24      }
25  
26      @Override
27      protected Object doTransform(Object src, String outputEncoding) throws TransformerException
28      {
29          if (src == null)
30          {
31              return ArrayUtils.EMPTY_BYTE_ARRAY;
32          }
33  
34          try
35          {
36              return StringUtils.hexStringToByteArray((String) src);
37          }
38          catch (Exception ex)
39          {
40              throw new TransformerException(this, ex);
41          }
42      }
43  
44  }