Coverage Report - org.mule.transport.file.transformers.FileToByteArray
 
Classes in this File Line Coverage Branch Coverage Complexity
FileToByteArray
0%
0/28
0%
0/12
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.file.transformers;
 8  
 
 9  
 import org.mule.api.transformer.TransformerException;
 10  
 import org.mule.transformer.simple.ObjectToByteArray;
 11  
 import org.mule.transformer.types.DataTypeFactory;
 12  
 import org.mule.util.ArrayUtils;
 13  
 
 14  
 import java.io.File;
 15  
 import java.io.FileInputStream;
 16  
 import java.io.FileNotFoundException;
 17  
 import java.io.IOException;
 18  
 import java.io.InputStream;
 19  
 
 20  
 import org.apache.commons.io.IOUtils;
 21  
 
 22  
 /**
 23  
  * <code>FileToByteArray</code> reads the contents of a file as a byte array.
 24  
  */
 25  
 public class FileToByteArray extends ObjectToByteArray
 26  
 {
 27  
     public FileToByteArray()
 28  
     {
 29  0
         super();
 30  0
         registerSourceType(DataTypeFactory.create(File.class));
 31  0
         registerSourceType(DataTypeFactory.BYTE_ARRAY);
 32  0
     }
 33  
 
 34  
     @Override
 35  
     public Object doTransform(Object src, String outputEncoding) throws TransformerException
 36  
     {
 37  
         // Support other payload types so that this transformer can be used
 38  
         // transparently both when streaming is on and off
 39  0
         if (src instanceof byte[])
 40  
         {
 41  0
             return src;
 42  
         }
 43  
         
 44  0
         if (src instanceof InputStream || src instanceof String)
 45  
         {
 46  0
             return super.doTransform(src, outputEncoding);
 47  
         }
 48  
         else
 49  
         {
 50  0
             File file = (File) src;
 51  
 
 52  0
             if (file == null)
 53  
             {
 54  0
                 throw new TransformerException(this, new IllegalArgumentException("null file"));
 55  
             }
 56  
 
 57  0
             if (!file.exists())
 58  
             {
 59  0
                 throw new TransformerException(this, new FileNotFoundException(file.getPath()));
 60  
             }
 61  
 
 62  0
             if (file.length() == 0)
 63  
             {
 64  0
                 logger.warn("File is empty: " + file.getAbsolutePath());
 65  0
                 return ArrayUtils.EMPTY_BYTE_ARRAY;
 66  
             }
 67  
 
 68  0
             FileInputStream fis = null;
 69  0
             byte[] bytes = null;
 70  
 
 71  
             try
 72  
             {
 73  0
                 fis = new FileInputStream(file);
 74  
                 // TODO Attention: arbitrary 4GB limit & also a great way to reap
 75  
                 // OOMs
 76  0
                 int length = new Long(file.length()).intValue();
 77  0
                 bytes = new byte[length];
 78  0
                 fis.read(bytes);
 79  0
                 return bytes;
 80  
             }
 81  
             // at least try..
 82  0
             catch (OutOfMemoryError oom)
 83  
             {
 84  0
                 throw new TransformerException(this, oom);
 85  
             }
 86  0
             catch (IOException e)
 87  
             {
 88  0
                 throw new TransformerException(this, e);
 89  
             }
 90  
             finally
 91  
             {
 92  0
                 IOUtils.closeQuietly(fis);
 93  
             }
 94  
         }
 95  
     }
 96  
 }