KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > codec > Base64Decoder


1 /*
2  * $Id: Base64Decoder.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.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 /**
20  * <code>Base64Encoder</code> transforms strings or byte arrays into Base64 encoded
21  * string
22  *
23  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
24  * @version $Revision: 3798 $
25  */

26 public class Base64Decoder extends AbstractTransformer
27 {
28     /**
29      * Serial version
30      */

31     private static final long serialVersionUID = -3297461704379793293L;
32
33     public Base64Decoder()
34     {
35         registerSourceType(String JavaDoc.class);
36         registerSourceType(byte[].class);
37         setReturnClass(byte[].class);
38     }
39
40     /*
41      * (non-Javadoc)
42      *
43      * @see org.mule.umo.transformer.UMOTransformer#transform(java.lang.Object)
44      */

45     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
46     {
47         String JavaDoc data = null;
48
49         try
50         {
51             if (src instanceof byte[])
52             {
53                 data = new String JavaDoc((byte[])src, encoding);
54             }
55             else
56             {
57                 data = (String JavaDoc)src;
58             }
59
60             byte[] result = Base64.decode(data);
61
62             if (getReturnClass().equals(String JavaDoc.class))
63             {
64                 return new String JavaDoc(result, encoding);
65             }
66             else
67             {
68                 return result;
69             }
70         }
71         catch (Exception JavaDoc 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