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