Coverage Report - org.mule.transformer.simple.ObjectToByteArray
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectToByteArray
0%
0/25
0%
0/6
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transformer.simple;
 8  
 
 9  
 import org.mule.RequestContext;
 10  
 import org.mule.api.transformer.TransformerException;
 11  
 import org.mule.api.transport.OutputHandler;
 12  
 import org.mule.transformer.types.DataTypeFactory;
 13  
 import org.mule.util.IOUtils;
 14  
 
 15  
 import java.io.ByteArrayOutputStream;
 16  
 import java.io.IOException;
 17  
 import java.io.InputStream;
 18  
 
 19  
 /**
 20  
  * <code>ObjectToByteArray</code> converts serilaizable object to a byte array but
 21  
  * treats <code>java.lang.String</code> differently by converting to bytes using
 22  
  * the <code>String.getBytrs()</code> method.
 23  
  */
 24  
 public class ObjectToByteArray extends SerializableToByteArray
 25  
 {
 26  
     public ObjectToByteArray()
 27  0
     {
 28  0
         this.registerSourceType(DataTypeFactory.INPUT_STREAM);
 29  0
         this.registerSourceType(DataTypeFactory.STRING);
 30  0
         this.registerSourceType(DataTypeFactory.create(OutputHandler.class));
 31  0
         setReturnDataType(DataTypeFactory.BYTE_ARRAY);
 32  0
     }
 33  
 
 34  
     @Override
 35  
     public Object doTransform(Object src, String outputEncoding) throws TransformerException
 36  
     {
 37  
         try
 38  
         {
 39  0
             if (src instanceof String)
 40  
             {
 41  0
                 return src.toString().getBytes(outputEncoding);
 42  
             }
 43  0
             else if (src instanceof InputStream)
 44  
             {
 45  0
                 InputStream is = (InputStream) src;
 46  0
                 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
 47  
                 try
 48  
                 {
 49  0
                     IOUtils.copyLarge(is, byteOut);
 50  
                 }
 51  
                 finally
 52  
                 {
 53  0
                     is.close();
 54  0
                 }
 55  0
                 return byteOut.toByteArray();
 56  
             }
 57  0
             else if (src instanceof OutputHandler)
 58  
             {
 59  0
                 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
 60  
                 
 61  
                 try
 62  
                 {
 63  0
                     ((OutputHandler) src).write(RequestContext.getEvent(), bytes);
 64  
                     
 65  0
                     return bytes.toByteArray();
 66  
                 }
 67  0
                 catch (IOException e)
 68  
                 {
 69  0
                     throw new TransformerException(this, e);
 70  
                 }
 71  
             }
 72  
         }
 73  0
         catch (Exception e)
 74  
         {
 75  0
             throw new TransformerException(this, e);
 76  0
         }
 77  
 
 78  0
         return super.doTransform(src, outputEncoding);
 79  
     }
 80  
 }