Coverage Report - org.mule.transformers.codec.Base64Decoder
 
Classes in this File Line Coverage Branch Coverage Complexity
Base64Decoder
79%
11/14
75%
9/12
4
 
 1  
 /*
 2  
  * $Id: Base64Decoder.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.codec;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.transformers.AbstractTransformer;
 15  
 import org.mule.umo.transformer.TransformerException;
 16  
 import org.mule.util.Base64;
 17  
 
 18  
 /**
 19  
  * <code>Base64Encoder</code> transforms Base64 encoded data into strings or byte
 20  
  * arrays.
 21  
  */
 22  
 public class Base64Decoder extends AbstractTransformer
 23  
 {
 24  
 
 25  
     public Base64Decoder()
 26  14
     {
 27  18
         registerSourceType(String.class);
 28  14
         registerSourceType(byte[].class);
 29  14
         setReturnClass(byte[].class);
 30  14
     }
 31  
 
 32  
     public Object doTransform(Object src, String encoding) throws TransformerException
 33  
     {
 34  
         try
 35  
         {
 36  
             String data;
 37  
 
 38  6
             if (src instanceof byte[])
 39  
             {
 40  0
                 data = new String((byte[]) src, encoding);
 41  
             }
 42  
             else
 43  
             {
 44  6
                 data = (String) src;
 45  
             }
 46  
 
 47  6
             byte[] result = Base64.decode(data);
 48  
 
 49  6
             if (getReturnClass().equals(String.class))
 50  
             {
 51  4
                 return new String(result, encoding);
 52  
             }
 53  
             else
 54  
             {
 55  2
                 return result;
 56  
             }
 57  
         }
 58  0
         catch (Exception ex)
 59  
         {
 60  0
             throw new TransformerException(
 61  
                 CoreMessages.transformFailed("base64", this.getReturnClass().getName()), this, ex);
 62  
         }
 63  
     }
 64  
 
 65  
 }