Coverage Report - org.mule.transformers.simple.StringToByteArray
 
Classes in this File Line Coverage Branch Coverage Complexity
StringToByteArray
70%
7/10
75%
6/8
3.5
 
 1  
 /*
 2  
  * $Id: StringToByteArray.java 7963 2007-08-21 08:53:15Z 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.config.i18n.MessageFactory;
 14  
 import org.mule.transformers.AbstractTransformer;
 15  
 import org.mule.umo.transformer.TransformerException;
 16  
 
 17  
 import java.io.UnsupportedEncodingException;
 18  
 
 19  
 /**
 20  
  * <code>StringToByteArray</code> converts a String into a byte array.
 21  
  */
 22  
 
 23  
 public class StringToByteArray extends AbstractTransformer
 24  
 {
 25  
 
 26  
     public StringToByteArray()
 27  16
     {
 28  20
         registerSourceType(String.class);
 29  16
         registerSourceType(byte[].class);
 30  16
         setReturnClass(byte[].class);
 31  16
     }
 32  
 
 33  
     public Object doTransform(Object src, String encoding) throws TransformerException
 34  
     {
 35  10
         if (src instanceof byte[])
 36  
         {
 37  0
             return src;
 38  
         }
 39  
         else
 40  
         {
 41  
             try
 42  
             {
 43  10
                 return ((String) src).getBytes(encoding);
 44  
             }
 45  0
             catch (UnsupportedEncodingException e)
 46  
             {
 47  0
                 throw new TransformerException(MessageFactory
 48  
                     .createStaticMessage("Unable to convert String to byte[]."), e);
 49  
             }
 50  
         }
 51  
     }
 52  
 
 53  
 }