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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package cz.crcs.ectester.common.util;
import cz.crcs.ectester.common.output.TeeOutputStream;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.LinkedList;
import java.util.List;
/**
* @author Jan Jancar johny@neuromancer.sk
*/
public class FileUtil {
private static Path appData = null;
public static String LIB_RESOURCE_DIR = "/cz/crcs/ectester/standalone/libs/jni/";
public static OutputStream openStream(String[] files) throws FileNotFoundException {
if (files == null) {
return null;
}
List<OutputStream> outs = new LinkedList<>();
for (String fileOut : files) {
outs.add(new FileOutputStream(fileOut));
}
return new TeeOutputStream(outs.toArray(new OutputStream[0]));
}
public static OutputStreamWriter openFiles(String[] files) throws FileNotFoundException {
if (files == null) {
return null;
}
return new OutputStreamWriter(openStream(files));
}
public static Path getAppData() {
if (appData != null) {
return appData;
}
if (System.getProperty("os.name").startsWith("Windows")) {
appData = Paths.get(System.getenv("AppData"));
return appData;
} else {
if (System.getProperty("os.name").startsWith("Linux")) {
String dataHome = System.getenv("XDG_DATA_HOME");
if (dataHome != null) {
appData = Paths.get(dataHome);
return appData;
}
}
String userHome = System.getProperty("user.home");
if (userHome != null && !userHome.equals("?")) {
appData = Paths.get(userHome, ".local", "share");
} else {
appData = Paths.get(System.getenv("HOME"), ".local", "share");
}
}
return appData;
}
public static boolean isNewer(URLConnection jarConn, Path realPath) throws IOException {
if (realPath.toFile().isFile()) {
long jarModified = jarConn.getLastModified();
long realModified = Files.getLastModifiedTime(realPath).toMillis();
return jarModified > realModified;
}
return true;
}
public static boolean writeNewer(String resourcePath, Path outPath) throws IOException {
URL reqURL = FileUtil.class.getResource(resourcePath);
if (reqURL == null) {
return false;
}
URLConnection reqConn = reqURL.openConnection();
if (isNewer(reqConn, outPath)) {
Files.copy(reqConn.getInputStream(), outPath, StandardCopyOption.REPLACE_EXISTING);
}
reqConn.getInputStream().close();
return true;
}
public static boolean write(String resourcePath, Path outPath) throws IOException {
URL reqURL = FileUtil.class.getResource(resourcePath);
if (reqURL == null) {
return false;
}
URLConnection reqConn = reqURL.openConnection();
Files.copy(reqConn.getInputStream(), outPath, StandardCopyOption.REPLACE_EXISTING);
reqConn.getInputStream().close();
return true;
}
public static Path getLibDir() {
return getAppData().resolve("ECTesterStandalone");
}
public static Path getRequirementsDir() {
return getLibDir().resolve("lib");
}
public static String getLibSuffix() {
if (System.getProperty("os.name").startsWith("Windows")) {
return "dll";
} else {
return "so";
}
}
}
|