1 10 11 package org.mule.transformers.codec; 12 13 import org.mule.config.i18n.Message; 14 import org.mule.config.i18n.Messages; 15 import org.mule.transformers.AbstractTransformer; 16 import org.mule.umo.transformer.TransformerException; 17 import org.mule.util.Base64; 18 19 26 public class Base64Encoder extends AbstractTransformer 27 { 28 31 private static final long serialVersionUID = 7742396053840854720L; 32 33 public Base64Encoder() 34 { 35 registerSourceType(String .class); 36 registerSourceType(byte[].class); 37 setReturnClass(String .class); 38 } 39 40 45 public Object doTransform(Object src, String encoding) throws TransformerException 46 { 47 byte[] buf; 48 49 try 50 { 51 if (src instanceof String ) 52 { 53 buf = ((String )src).getBytes(encoding); 54 } 55 else 56 { 57 buf = (byte[])src; 58 } 59 60 String result = Base64.encodeBytes(buf, Base64.DONT_BREAK_LINES); 61 62 if (getReturnClass().equals(byte[].class)) 63 { 64 return result.getBytes(encoding); 65 } 66 else 67 { 68 return result; 69 } 70 } 71 catch (Exception ex) 72 { 73 throw new TransformerException(new Message(Messages.TRANSFORM_FAILED_FROM_X_TO_X, src.getClass() 74 .getName(), "base64"), this, ex); 75 } 76 } 77 78 } 79 | Popular Tags |