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