IOUtil.java
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.aemfd.watchfolder.job;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IOUtil {
public static void copyFile(File aSource, File aDestination) throws IOException {
if (!aSource.exists()) {
throw new IOException("File: " + aSource.getAbsolutePath() + " does not exist.");
}
if (aSource.isFile() && aDestination.isFile() && aDestination.exists() && !aDestination.delete()) {
throw new IOException("File: " + aDestination.getAbsolutePath() + " already exists and cannot be deleted.");
}
if (aDestination.isDirectory() && !aDestination.exists() && !aDestination.mkdir()) {
throw new IOException("Cannot create directory: " + aDestination.getAbsolutePath());
}
if (aSource.isDirectory()) {
if (!aDestination.isDirectory()) {
throw new IOException("Connot copy directory: " + aSource.getAbsolutePath() + " to file: " + aDestination.getAbsolutePath());
}
File[] _files = aSource.listFiles();
for (int i = 0; i < _files.length; ++i) {
File _dest = new File(aDestination, _files[i].getName());
if (_files[i].isDirectory() && !_dest.mkdir()) {
throw new IOException("Cannot create directory: " + _dest.getAbsolutePath());
}
IOUtil.copyFile(_files[i], _dest);
}
return;
}
File _dest = null;
_dest = aDestination.isDirectory() ? new File(aDestination, aSource.getName()) : aDestination;
FileInputStream _fin = new FileInputStream(aSource);
FileOutputStream _fout = new FileOutputStream(_dest);
IOUtil.writeInput2OutputStream(_fin, _fout, 10000, 1);
_fout.close();
_fin.close();
}
public static void writeInput2OutputStream(InputStream aInputStream, OutputStream aOutputStream, int aBufferSize, int aAutoFlushInterval) throws IOException {
byte[] _buf = new byte[aBufferSize];
int x = 0;
int i = 1;
while ((x = aInputStream.read(_buf)) > 0) {
aOutputStream.write(_buf, 0, x);
_buf = new byte[aBufferSize];
if (aAutoFlushInterval > 0 && i % aAutoFlushInterval > 0) {
aOutputStream.flush();
}
++i;
}
if (aAutoFlushInterval > 0 && (i - 1) % aAutoFlushInterval == 0) {
aOutputStream.flush();
}
}
}