View Javadoc

1   /*
2    * $Id: ByteArrayToObject.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transformer.simple;
12  
13  import org.mule.api.transformer.TransformerException;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.util.IOUtils;
16  
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.io.ObjectStreamConstants;
20  import java.io.PushbackInputStream;
21  import java.io.UnsupportedEncodingException;
22  
23  /**
24   * <code>ByteArrayToObject</code> works in the same way as
25   * <code>ByteArrayToSerializable</code> but checks if the byte array is a
26   * serialised object and if not will return a String created from the bytes as the
27   * returnType on the transformer.
28   */
29  public class ByteArrayToObject extends ByteArrayToSerializable
30  {
31  
32      @Override
33      public Object doTransform(Object src, String encoding) throws TransformerException
34      {
35          if (src instanceof byte[])
36          {
37              byte[] bytes = (byte[])src;
38  
39              if (this.checkStreamHeader(bytes[0]))
40              {
41                  return super.doTransform(src, encoding);
42              }
43              else
44              {
45                  try
46                  {
47                      return new String(bytes, encoding);
48                  }
49                  catch (UnsupportedEncodingException e)
50                  {
51                      throw new TransformerException(this, e);
52                  }
53              }
54          }
55          else if (src instanceof InputStream)
56          {
57              try
58              {
59                  PushbackInputStream pushbackStream = new PushbackInputStream((InputStream)src);
60                  int firstByte = pushbackStream.read();
61                  pushbackStream.unread((byte)firstByte);
62                  
63                  if (this.checkStreamHeader((byte)firstByte))
64                  {
65                      return super.doTransform(pushbackStream, encoding);
66                  }
67                  else
68                  {
69                      try
70                      {
71                          return IOUtils.toString(pushbackStream, encoding);
72                      }
73                      finally
74                      {
75                          // this also closes the underlying stream that's stored in src
76                          pushbackStream.close();
77                      }
78                  }
79              }
80              catch (IOException iox)
81              {
82                  throw new TransformerException(this, iox);
83              }
84          }
85          else
86          {
87              throw new TransformerException(CoreMessages.transformOnObjectUnsupportedTypeOfEndpoint(
88                  this.getName(), src.getClass(), endpoint));
89          }
90      }
91  
92      private boolean checkStreamHeader(byte firstByte)
93      {
94          return (firstByte == (byte)((ObjectStreamConstants.STREAM_MAGIC >>> 8) & 0xFF));
95      }
96  }