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 Base64Decoder extends AbstractTransformer 27 { 28 31 private static final long serialVersionUID = -3297461704379793293L; 32 33 public Base64Decoder() 34 { 35 registerSourceType(String .class); 36 registerSourceType(byte[].class); 37 setReturnClass(byte[].class); 38 } 39 40 45 public Object doTransform(Object src, String encoding) throws TransformerException 46 { 47 String data = null; 48 49 try 50 { 51 if (src instanceof byte[]) 52 { 53 data = new String ((byte[])src, encoding); 54 } 55 else 56 { 57 data = (String )src; 58 } 59 60 byte[] result = Base64.decode(data); 61 62 if (getReturnClass().equals(String .class)) 63 { 64 return new String (result, 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, "base64", 74 this.getReturnClass().getName()), this, ex); 75 } 76 } 77 78 } 79 | Popular Tags |