Coverage Report - org.mule.transport.sftp.SftpFileArchiveInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
SftpFileArchiveInputStream
0%
0/35
0%
0/14
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;
 8  
 
 9  
 import org.apache.commons.logging.Log;
 10  
 import org.apache.commons.logging.LogFactory;
 11  
 import org.mule.util.FileUtils;
 12  
 
 13  
 import java.io.File;
 14  
 import java.io.FileInputStream;
 15  
 import java.io.FileNotFoundException;
 16  
 import java.io.IOException;
 17  
 
 18  
 /**
 19  
  * Ensures that the file is moved to the archiveFile folder after a successful
 20  
  * consumption of the file
 21  
  * 
 22  
  * @author Magnus Larsson
 23  
  */
 24  
 public class SftpFileArchiveInputStream extends FileInputStream implements ErrorOccurredDecorator
 25  
 {
 26  
     /**
 27  
      * logger used by this class
 28  
      */
 29  0
     private static final Log logger = LogFactory.getLog(SftpFileArchiveInputStream.class);
 30  
 
 31  
     private File file;
 32  
     private File archiveFile;
 33  0
     private boolean errorOccured = false;
 34  
 
 35  
     // Log every 10 000 000 bytes read at debug-level
 36  
     // Good if really large files are transferred and you tend to get nervous by not
 37  
     // seeing any progress in the logfile...
 38  
     private static final int LOG_BYTE_INTERVAL = 10000000;
 39  0
     private long bytesRead = 0;
 40  0
     private long nextLevelToLogBytesRead = LOG_BYTE_INTERVAL;
 41  
 
 42  
     public SftpFileArchiveInputStream(File file) throws FileNotFoundException
 43  
     {
 44  0
         super(file);
 45  
 
 46  0
         this.file = file;
 47  0
         this.archiveFile = null;
 48  0
     }
 49  
 
 50  
     public SftpFileArchiveInputStream(File file, File archiveFile) throws FileNotFoundException
 51  
     {
 52  0
         super(file);
 53  
 
 54  0
         this.file = file;
 55  0
         this.archiveFile = archiveFile;
 56  0
     }
 57  
 
 58  
     @Override
 59  
     public int read() throws IOException
 60  
     {
 61  0
         logReadBytes(1);
 62  0
         return super.read();
 63  
     }
 64  
 
 65  
     @Override
 66  
     public int read(byte[] b, int off, int len) throws IOException
 67  
     {
 68  0
         logReadBytes(len);
 69  0
         return super.read(b, off, len);
 70  
     }
 71  
 
 72  
     @Override
 73  
     public int read(byte[] b) throws IOException
 74  
     {
 75  0
         logReadBytes(b.length);
 76  0
         return super.read(b);
 77  
     }
 78  
 
 79  
     public void close() throws IOException
 80  
     {
 81  0
         if (logger.isDebugEnabled())
 82  
         {
 83  0
             logger.debug("Closing the stream for the file " + file);
 84  
         }
 85  0
         super.close();
 86  
 
 87  0
         if (!errorOccured && archiveFile != null)
 88  
         {
 89  0
             if (logger.isInfoEnabled())
 90  
             {
 91  0
                 logger.info("Move archiveTmpSendingFile (" + file + ") to archiveFolder (" + archiveFile
 92  
                             + ")...");
 93  
             }
 94  0
             FileUtils.moveFile(file, archiveFile);
 95  
         }
 96  0
     }
 97  
 
 98  
     public void setErrorOccurred()
 99  
     {
 100  0
         if (logger.isDebugEnabled()) logger.debug("setErrorOccurred() called");
 101  0
         this.errorOccured = true;
 102  0
     }
 103  
 
 104  
     private void logReadBytes(int newBytesRead)
 105  
     {
 106  0
         if (!logger.isDebugEnabled()) return;
 107  
 
 108  0
         this.bytesRead += newBytesRead;
 109  0
         if (this.bytesRead >= nextLevelToLogBytesRead)
 110  
         {
 111  0
             logger.debug("Read " + this.bytesRead + " bytes and couting...");
 112  0
             nextLevelToLogBytesRead += LOG_BYTE_INTERVAL;
 113  
         }
 114  0
     }
 115  
 
 116  
 }