KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: UUDecoder.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
18 import java.io.IOException JavaDoc;
19 import java.io.UnsupportedEncodingException JavaDoc;
20
21 /**
22  * <code>Base64Encoder</code> transforms strings or byte arrays into Base64 encoded
23  * string
24  *
25  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
26  * @version $Revision: 3798 $
27  */

28 public class UUDecoder extends AbstractTransformer
29 {
30     /**
31      * Serial version
32      */

33     private static final long serialVersionUID = -3570019318835185793L;
34
35     private sun.misc.UUDecoder decoder;
36
37     public UUDecoder()
38     {
39         registerSourceType(String JavaDoc.class);
40         registerSourceType(byte[].class);
41         setReturnClass(String JavaDoc.class);
42         decoder = new sun.misc.UUDecoder();
43     }
44
45     /*
46      * (non-Javadoc)
47      *
48      * @see org.mule.umo.transformer.UMOTransformer#transform(java.lang.Object)
49      */

50     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
51     {
52         String JavaDoc data = null;
53         if (src instanceof byte[])
54         {
55             if (encoding != null)
56             {
57                 try
58                 {
59                     data = new String JavaDoc((byte[])src, encoding);
60                 }
61                 catch (UnsupportedEncodingException JavaDoc ex)
62                 {
63                     data = new String JavaDoc((byte[])src);
64                 }
65             }
66             else
67             {
68                 data = new String JavaDoc((byte[])src);
69             }
70         }
71         else
72         {
73             data = (String JavaDoc)src;
74         }
75         try
76         {
77             byte[] result = decoder.decodeBuffer(data);
78             if (getReturnClass().equals(String JavaDoc.class))
79             {
80                 if (encoding != null)
81                 {
82                     return new String JavaDoc(result, encoding);
83                 }
84                 else
85                 {
86                     return new String JavaDoc(result);
87                 }
88             }
89             return result;
90         }
91         catch (IOException JavaDoc e)
92         {
93             throw new TransformerException(new Message(Messages.TRANSFORM_FAILED_FROM_X, "UU Encoding"),
94                 this, e);
95         }
96     }
97 }
98
Popular Tags