Coverage Report - org.mule.transport.sftp.transformers.InputStreamToByteArray
 
Classes in this File Line Coverage Branch Coverage Complexity
InputStreamToByteArray
0%
0/16
0%
0/4
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.transport.sftp.transformers;
 8  
 
 9  
 import org.mule.api.transformer.TransformerException;
 10  
 import org.mule.config.i18n.MessageFactory;
 11  
 import org.mule.transformer.AbstractTransformer;
 12  
 import org.mule.transformer.types.DataTypeFactory;
 13  
 
 14  
 import java.io.ByteArrayOutputStream;
 15  
 import java.io.InputStream;
 16  
 
 17  
 import org.apache.commons.io.IOUtils;
 18  
 
 19  
 /**
 20  
  * TODO
 21  
  */
 22  
 public class InputStreamToByteArray extends AbstractTransformer
 23  
 {
 24  
     /**
 25  
      * Serial version
 26  
      */
 27  
     private static final long serialVersionUID = -7444711427779720031L;
 28  
 
 29  
     public InputStreamToByteArray()
 30  0
     {
 31  0
         registerSourceType(DataTypeFactory.INPUT_STREAM);
 32  0
         setReturnDataType(DataTypeFactory.BYTE_ARRAY);
 33  0
     }
 34  
 
 35  
     @Override
 36  
     public Object doTransform(Object msg, String outputEncoding) throws TransformerException
 37  
     {
 38  0
         if (msg instanceof InputStream)
 39  
         {
 40  0
             InputStream inputStream = null;
 41  
 
 42  
             try
 43  
             {
 44  0
                 inputStream = (InputStream) msg;
 45  
 
 46  0
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 47  
 
 48  0
                 byte[] buffer = new byte[1024];
 49  
                 int len;
 50  0
                 while ((len = inputStream.read(buffer)) > 0)
 51  
                 {
 52  0
                     baos.write(buffer, 0, len);
 53  
                 }
 54  
 
 55  0
                 return baos.toByteArray();
 56  
             }
 57  0
             catch (Exception e)
 58  
             {
 59  0
                 throw new TransformerException(this, e);
 60  
             }
 61  
             finally
 62  
             {
 63  0
                 IOUtils.closeQuietly(inputStream);
 64  
             }
 65  
         }
 66  
         else
 67  
         {
 68  0
             throw new TransformerException(
 69  
                 MessageFactory.createStaticMessage("Message is not an instance of java.io.InputStream"), this);
 70  
         }
 71  
     }
 72  
 
 73  
 }