View Javadoc

1   /*
2    * $Id: ReceiverFileInputStream.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.transport.file;
12  
13  import org.mule.api.DefaultMuleException;
14  import org.mule.transport.file.i18n.FileMessages;
15  import org.mule.util.FileUtils;
16  
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  
22  /**
23   * This implementation is used when streaming and will move or delete the source file
24   * when the stream is closed.
25   */
26  class ReceiverFileInputStream extends FileInputStream
27  {
28      private File currentFile;
29      private boolean deleteOnClose;
30      private File moveToOnClose;
31  
32      public ReceiverFileInputStream(File currentFile, boolean deleteOnClose, File moveToOnClose)
33          throws FileNotFoundException
34      {
35          super(currentFile);
36          this.currentFile = currentFile;
37          this.deleteOnClose = deleteOnClose;
38          this.moveToOnClose = moveToOnClose;
39      }
40  
41      @Override
42      public void close() throws IOException
43      {
44          super.close();
45  
46          if (moveToOnClose != null)
47          {
48              FileUtils.moveFileWithCopyFallback(currentFile, moveToOnClose);
49          }
50          else if (deleteOnClose)
51          {
52              if (!currentFile.delete())
53              {
54                  try
55                  {
56                      throw new DefaultMuleException(FileMessages.failedToDeleteFile(currentFile));
57                  }
58                  catch (DefaultMuleException e)
59                  {
60                      IOException e2 = new IOException();
61                      e2.initCause(e);
62                      throw e2;
63                  }
64              }
65          }
66      }
67  
68      public File getCurrentFile()
69      {
70          return currentFile;
71      }
72  }