View Javadoc

1   /*
2    * $Id: ByteArrayToObject.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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 CPAL v1.0
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.transformer.TransformerException;
14  
15  import java.io.ObjectStreamConstants;
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  public class ByteArrayToObject extends ByteArrayToSerializable
24  {
25  
26      // @Override
27      public Object doTransform(Object src, String encoding) throws TransformerException
28      {
29          byte[] bytes = (byte[]) src;
30  
31          if (bytes[0] == (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 8) & 0xFF))
32          {
33              return super.doTransform(src, encoding);
34          }
35          else
36          {
37              try
38              {
39                  return new String(bytes, encoding);
40              }
41              catch (Exception e)
42              {
43                  throw new TransformerException(this, e);
44              }
45          }
46      }
47  
48  }