Newer
Older
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
package utility;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.time.temporal.Temporal;
import org.json.JSONException;
import org.json.JSONObject;
public class Helper {
public static boolean compareText(String a, String b){
return a.compareToIgnoreCase(b) == 0;
}
public static String readBody(InputStream requestBody) {
int req;
StringBuffer sb = new StringBuffer();
try {
while((req = requestBody.read()) != -1)
sb.append(Character.toString((char)req));
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public static boolean checkJSON(String body) {
if(!body.startsWith("{") || !body.endsWith("}"))
return false;
try {
new JSONObject(body);
return true;
} catch (JSONException e) {
return false;
}
}
public static String leggiFile(String path) throws IOException {
String line;
StringBuffer answer = new StringBuffer();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(path));
while((line = bufferedReader.readLine()) != null) {
answer.append(line).append("\n");
}
} catch (FileNotFoundException ex) {
// System.out.println("Unable to open file '" + path + "'");
// scriviFile("sono qui",path);
// System.out.println("file creato al path corrispondente");
} finally {
if(bufferedReader != null)
bufferedReader.close();
}
return answer.toString();
}
public synchronized static void scriviFile(String text, String path) throws IOException {
BufferedWriter b = new BufferedWriter(new FileWriter(path));
String[] lines = text.split("\n");
for(String l : lines) {
b.write(l);
b.newLine();
}
b.close();
}
public synchronized static void scriviFile(JSONObject job, String path) throws IOException {
scriviFile(convertiJSON(job),path);
}
private static String convertiJSON(JSONObject job) throws IOException {
String[] strings = job.toString().split(",");
StringBuffer text = new StringBuffer();
int index = 1;
for(String s : strings)
if(strings.length != index) {
text.append(s).append(",\n");
index++;
} else text.append(s);
return text.toString();
}
public static void appendiFile(String text, String path) throws IOException {
String fileContents = leggiFile(path);
StringBuffer s = new StringBuffer();
if(!fileContents.isBlank())
s.append(fileContents).append("\n");
s.append(text);
scriviFile(s.toString(),path);
}
public static void appendiFile(JSONObject job, String path, boolean splitLines) throws IOException {
appendiFile(splitLines ? convertiJSON(job) : job.toString() ,path);
}
public static void appendiFile(JSONObject job, String path) throws IOException {
appendiFile(job,path,true);
}
public static long timeDifference(Temporal older, Temporal newer) {
return Duration.between(older, newer).toSeconds();
}
}