View Javadoc

1   /*
2    * $Id: ReceiverFileInputStream.java 23148 2011-10-11 17:24:05Z pablo.kraan $
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      private boolean streamProcessingError;
32  
33      public ReceiverFileInputStream(File currentFile, boolean deleteOnClose, File moveToOnClose)
34          throws FileNotFoundException
35      {
36          super(currentFile);
37          this.currentFile = currentFile;
38          this.deleteOnClose = deleteOnClose;
39          this.moveToOnClose = moveToOnClose;
40      }
41  
42      @Override
43      public void close() throws IOException
44      {
45          super.close();
46  
47          if (!streamProcessingError)
48          {
49              if (moveToOnClose != null)
50              {
51                  FileUtils.moveFileWithCopyFallback(currentFile, moveToOnClose);
52              }
53              else if (deleteOnClose)
54              {
55                  if (!currentFile.delete())
56                  {
57                      try
58                      {
59                          throw new DefaultMuleException(FileMessages.failedToDeleteFile(currentFile));
60                      }
61                      catch (DefaultMuleException e)
62                      {
63                          IOException e2 = new IOException();
64                          e2.initCause(e);
65                          throw e2;
66                      }
67                  }
68              }
69          }
70      }
71  
72      public File getCurrentFile()
73      {
74          return currentFile;
75      }
76  
77      public boolean isStreamProcessingError()
78      {
79          return streamProcessingError;
80      }
81  
82      public void setStreamProcessingError(boolean streamProcessingError)
83      {
84          this.streamProcessingError = streamProcessingError;
85      }
86  }