/
Util.java
400 lines (364 loc) · 11.7 KB
/
Util.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package net.ME1312.SubServers.Bungee.Library;
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
* SubServers Utility Class
*/
public final class Util {
private Util(){}
public interface ExceptionReturnRunnable<R> {
R run() throws Throwable;
}
public interface ExceptionRunnable {
void run() throws Throwable;
}
public interface ReturnRunnable<R> {
R run();
}
/**
* Checks values to make sure they're not null
*
* @param values Values to check
* @return If any are null
*/
public static boolean isNull(Object... values) {
boolean ret = false;
for (Object value : values) {
if (value == null) ret = true;
}
return ret;
}
/**
* Get keys by value from map
*
* @param map Map to search
* @param value Value to search for
* @param <K> Key
* @param <V> Value
* @return Search results
*/
public static <K, V> List<K> getBackwards(Map<K, V> map, V value) {
List<K> values = new ArrayList<K>();
for (K key : map.keySet()) {
if (map.get(key).equals(value)) {
values.add(key);
}
}
return values;
}
/**
* Get an item from a map ignoring case
*
* @param map Map to search
* @param key Key to search with
* @param <V> Value
* @return Search Result
*/
public static <V> V getCaseInsensitively(Map<String, V> map, String key) {
HashMap<String, String> insensitivity = new HashMap<String, String>();
for (String item : map.keySet()) if (!insensitivity.keySet().contains(item.toLowerCase())) insensitivity.put(item.toLowerCase(), item);
if (insensitivity.keySet().contains(key.toLowerCase())) {
return map.get(insensitivity.get(key.toLowerCase()));
} else {
return null;
}
}
/**
* Gets a new Variable that doesn't match the existing Variables
*
* @param existing Existing Variables
* @param generator Variable Generator
* @param <V> Variable Type
* @return Variable
*/
public static <V> V getNew(Collection<? extends V> existing, ReturnRunnable<V> generator) {
V result = null;
while (result == null) {
V tmp = generator.run();
if (!existing.contains(tmp)) result = tmp;
}
return result;
}
/**
* Read Everything from Reader
*
* @param rd Reader
* @return Reader Contents
* @throws IOException
*/
public static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
/**
* Copy from the Class Loader
*
* @param loader ClassLoader
* @param resource Location From
* @param destination Location To
*/
public static void copyFromJar(ClassLoader loader, String resource, String destination) {
InputStream resStreamIn = loader.getResourceAsStream(resource);
File resDestFile = new File(destination);
try {
OutputStream resStreamOut = new FileOutputStream(resDestFile);
int readBytes;
byte[] buffer = new byte[4096];
while ((readBytes = resStreamIn.read(buffer)) > 0) {
resStreamOut.write(buffer, 0, readBytes);
}
resStreamOut.close();
resStreamIn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Get a variable from a method which may throw an exception
*
* @param runnable Runnable
* @param def Default value when an exception is thrown
* @param <R> Variable Type
* @return Returns value or default depending on if an exception is thrown
*/
public static <R> R getDespiteException(ExceptionReturnRunnable<R> runnable, R def) {
try {
return runnable.run();
} catch (Throwable e) {
return def;
}
}
/**
* Determines if an Exception will occur
*
* @param runnable Runnable
* @return If an Exception occured
*/
public static boolean isException(ExceptionRunnable runnable) {
try {
runnable.run();
return false;
} catch (Throwable e) {
return true;
}
}
/**
* Delete Directory
*
* @param folder Location
*/
public static void deleteDirectory(File folder) {
File[] files = folder.listFiles();
if(files!=null) {
for(File f: files) {
if(f.isDirectory()) {
deleteDirectory(f);
} else {
f.delete();
}
}
}
folder.delete();
}
/**
* Copy a Directory
*
* @param from Source
* @param to Destination
*/
public static void copyDirectory(File from, File to) {
if (from.isDirectory()) {
if (!to.exists()) {
to.mkdirs();
}
String files[] = from.list();
for (String file : files) {
File srcFile = new File(from, file);
File destFile = new File(to, file);
copyDirectory(srcFile, destFile);
}
} else {
if (to.exists()) {
to.delete();
}
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(from);
out = new FileOutputStream(to, false);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
} catch (Exception e) {
try {
if (in != null) in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
if (out != null) out.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
private static List<String> zipsearch(File origin, File file) {
List<String> list = new LinkedList<String>();
if (file.isFile()) {
list.add(file.getAbsoluteFile().toString().substring(origin.getAbsoluteFile().toString().length()+1, file.getAbsoluteFile().toString().length()));
}
if (file.isDirectory()) for (File next : file.listFiles()) {
list.addAll(zipsearch(origin, next));
}
return list;
}
public static void zip(File file, OutputStream zip) {
byte[] buffer = new byte[1024];
try{
ZipOutputStream zos = new ZipOutputStream(zip);
for(String next : zipsearch(file, file)){
ZipEntry ze= new ZipEntry(next);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(file.getAbsolutePath() + File.separator + next);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
zos.close();
} catch(IOException ex){
ex.printStackTrace();
}
}
public static void unzip(InputStream zip, File dir) {
byte[] buffer = new byte[1024];
try{
ZipInputStream zis = new ZipInputStream(zip);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
File newFile = new File(dir + File.separator + ze.getName());
if (newFile.exists()) {
if (newFile.isDirectory()) {
Util.deleteDirectory(newFile);
} else {
newFile.delete();
}
}
if (ze.isDirectory()) {
newFile.mkdirs();
continue;
} else if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
zis.closeEntry();
zis.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
/**
* Get a Random Integer
*
* @param min Minimum Value
* @param max Maximum Value
* @return Random Integer
*/
public static int random(int min, int max) {
return new Random().nextInt((max - min) + 1) + min;
}
/**
* Parse escapes in a Java String
*
* @param str String
* @return Unescaped String
*/
public static String unescapeJavaString(String str) {
StringBuilder sb = new StringBuilder(str.length());
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == '\\') {
char nextChar = (i == str.length() - 1) ? '\\' : str
.charAt(i + 1);
// Octal escape?
if (nextChar >= '0' && nextChar <= '7') {
String code = "" + nextChar;
i++;
if ((i < str.length() - 1) && str.charAt(i + 1) >= '0'
&& str.charAt(i + 1) <= '7') {
code += str.charAt(i + 1);
i++;
if ((i < str.length() - 1) && str.charAt(i + 1) >= '0'
&& str.charAt(i + 1) <= '7') {
code += str.charAt(i + 1);
i++;
}
}
sb.append((char) Integer.parseInt(code, 8));
continue;
}
switch (nextChar) {
case '\\':
ch = '\\';
break;
case 'b':
ch = '\b';
break;
case 'f':
ch = '\f';
break;
case 'n':
ch = '\n';
break;
case 'r':
ch = '\r';
break;
case 't':
ch = '\t';
break;
case '\"':
ch = '\"';
break;
case '\'':
ch = '\'';
break;
// Hex Unicode: u????
case 'u':
if (i >= str.length() - 5) {
ch = 'u';
break;
}
int code = Integer.parseInt(
"" + str.charAt(i + 2) + str.charAt(i + 3)
+ str.charAt(i + 4) + str.charAt(i + 5), 16);
sb.append(Character.toChars(code));
i += 5;
continue;
}
i++;
}
sb.append(ch);
}
return sb.toString();
}
}