KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ByteArrayToObject.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.ObjectStreamConstants JavaDoc;
14
15 import org.mule.umo.transformer.TransformerException;
16
17 /**
18  * <code>ByteArrayToObject</code> works in the same way as
19  * <code>ByteArrayToSerializable</code> but checks if th byte array is a serialised
20  * object and if not will return a String created from the bytes is the returnType on
21  * the transformer.
22  *
23  * @author Ross Mason
24  */

25 public class ByteArrayToObject extends ByteArrayToSerializable
26 {
27
28     /**
29      * Serial version
30      */

31     private static final long serialVersionUID = 2105641786358330597L;
32
33     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
34     {
35
36         byte[] bytes = (byte[])src;
37         if (bytes[0] == (byte)((ObjectStreamConstants.STREAM_MAGIC >>> 8) & 0xFF))
38         {
39             return super.doTransform(src, encoding);
40         }
41         else
42         {
43             try
44             {
45                 return new String JavaDoc(bytes, encoding);
46             }
47             catch (Exception JavaDoc e)
48             {
49                 throw new TransformerException(this, e);
50             }
51         }
52     }
53
54 }
55
Popular Tags