-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.java
More file actions
78 lines (70 loc) · 2.4 KB
/
FileUtil.java
File metadata and controls
78 lines (70 loc) · 2.4 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package me.david.sploty4.util;
import me.david.sploty4.Sploty;
import org.apache.commons.io.IOUtils;
import java.io.*;
public final class FileUtil {
public static void toFile(InputStream is, File file, boolean close) throws IOException {
if(!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
OutputStream outStream = new FileOutputStream(file);
byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1)
outStream.write(buffer, 0, bytesRead);
IOUtils.closeQuietly(outStream);
if(close) IOUtils.closeQuietly(is);
}
public interface Updater {
void onUpdate(long done);
void onFinished();
}
public static void toFile(Updater listener, InputStream is, File file, boolean close, int chuckSize) throws IOException {
if(!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
OutputStream outStream = new FileOutputStream(file);
byte[] buffer = new byte[chuckSize];
int bytesRead;
long totalread = 0;
while ((bytesRead = is.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
totalread += bytesRead;
listener.onUpdate(totalread);
}
listener.onFinished();
IOUtils.closeQuietly(outStream);
if(close) IOUtils.closeQuietly(is);
}
public static boolean isValidFileName(final String fileName) {
File file = new File(fileName);
try {
return file.getCanonicalFile().getName().equals(fileName);
} catch (IOException ex) {
return false;
}
}
public static File createFile(File file){
file.getParentFile().mkdirs();
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
Sploty.getLogger().exception(e, "Failed creating File...");
}
return file;
}
int i = 0;
while (new File(file.getPath() + "(" + i + ")").exists())
i++;
file = new File(file.getPath() + "(" + i + ")");
try {
file.createNewFile();
} catch (IOException e) {
Sploty.getLogger().exception(e, "Failed creating File...");
}
return file;
}
}