1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public class SftpFileArchiveInputStream extends FileInputStream implements ErrorOccurredDecorator |
25 | |
{ |
26 | |
|
27 | |
|
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 | |
|
36 | |
|
37 | |
|
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 | |
} |