KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ObjectToByteArray.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 org.mule.umo.UMOEventContext;
14 import org.mule.umo.transformer.TransformerException;
15
16 /**
17  * <code>ObjectToByteArray</code> converts serilaizable object to a byte array but
18  * treats <code>java.lang.String</code> differently by converting to bytes using
19  * the <code>String.getBytrs()</code> method.
20  *
21  * @author Ross Mason
22  */

23 public class ObjectToByteArray extends SerializableToByteArray
24 {
25
26     /**
27      * Serial version
28      */

29     private static final long serialVersionUID = 8111970112989435191L;
30
31     public ObjectToByteArray()
32     {
33         registerSourceType(Object JavaDoc.class);
34     }
35
36     public Object JavaDoc transform(Object JavaDoc src, String JavaDoc encoding, UMOEventContext context) throws TransformerException
37     {
38         if (src instanceof String JavaDoc)
39         {
40             try
41             {
42                 return src.toString().getBytes(encoding);
43             }
44             catch (Exception JavaDoc e)
45             {
46                 throw new TransformerException(this, e);
47             }
48         }
49         else
50         {
51             return super.transform(src, encoding, context);
52         }
53     }
54
55 }
56
Popular Tags