KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > simple > ByteArrayToString


1 /*
2  * $Id: ByteArrayToString.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.simple;
12
13 import java.io.UnsupportedEncodingException JavaDoc;
14
15 import org.mule.config.i18n.Message;
16 import org.mule.transformers.AbstractTransformer;
17 import org.mule.umo.transformer.TransformerException;
18
19 /**
20  * <code>ByteArrayToString</code> converts a byte array into a String.
21  *
22  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
23  * @version $Revision: 3798 $
24  */

25
26 public class ByteArrayToString extends AbstractTransformer
27 {
28
29     private static final long serialVersionUID = -9033005899991305308L;
30
31     public ByteArrayToString()
32     {
33         registerSourceType(byte[].class);
34         registerSourceType(String JavaDoc.class);
35         setReturnClass(String JavaDoc.class);
36     }
37
38     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
39     {
40         if (src instanceof String JavaDoc)
41         {
42             return src;
43         }
44         else
45         {
46             try
47             {
48                 return new String JavaDoc((byte[])src, encoding);
49             }
50             catch (UnsupportedEncodingException JavaDoc e)
51             {
52                 throw new TransformerException(
53                     Message.createStaticMessage("Unable to convert byte[] to String."), e);
54             }
55         }
56     }
57 }
58
Popular Tags