-
Notifications
You must be signed in to change notification settings - Fork 4
/
Utilities.java
510 lines (472 loc) · 18.5 KB
/
Utilities.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* JDigitalSimulator
* Copyright (C) 2017 Kristian Kraljic
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package lc.kra.jds;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.swing.JFileChooser;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLDecoder;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public final class Utilities {
public static enum TranslationType { TEXT, ALTERNATIVE, TITLE, EXTERNAL, TOOLTIP, DESCRIPTION; }
public static final String
CONFIGURATION_LOCALIZATION_LANGUAGE = "localization.language",
CONFIGURATION_LOOK_AND_FEEL_CLASS = "lookandfeel.class",
CONFIGURATION_LOOK_AND_FEEL_NAME = "lookandfeel.name",
CONFIGURATION_ANSI_SYMBOLS = "symbols.ansi",
CONFIGURATION_WINDOW_SIZE = "window.size",
CONFIGURATION_WINDOW_LOCATION = "window.location",
CONFIGURATION_WINDOW_MAXIMIZED = "window.maximized";
public static final Locale[] SUPPORTED_LOCALES = {Locale.ENGLISH, Locale.GERMAN};
private static Locale currentLocale = Locale.getDefault();
private static ResourceBundle translationBundle;
private static SimpleClassLoader simpleClassLoader;
private static Properties configuration = new Properties();
private static boolean useAnsiSymbols = false;
public static Object copy(Object object) throws CloneNotSupportedException { //deep clone using serilization
Object copy = null;
FastByteArrayOutputStream byteOutput = new FastByteArrayOutputStream();
try {
ObjectOutputStream objectOutput = new ObjectOutputStream(byteOutput);
try { objectOutput.writeObject(object); }
finally { objectOutput.close(); }
ObjectInputStream objectInput = new AlternateClassLoaderObjectInputStream(byteOutput.getInputStream(), Utilities.getSimpleClassLoader());
try { copy = objectInput.readObject(); }
finally { objectInput.close(); }
} catch(Exception e) { e.printStackTrace(); throw new CloneNotSupportedException(); }
return copy;
}
public static boolean isCopying() {
for(StackTraceElement element:Thread.currentThread().getStackTrace())
if(element.getClassName().equals(Utilities.class.getName())&&element.getMethodName().equals("copy"))
return true;
return false;
}
public static boolean isCloneable(Object object) { return isCloneable(object, false); }
public static boolean isCloneable(Object object, boolean deep) {
if(object==null) return false;
return isCloneable(object.getClass(), deep);
}
private static boolean isCloneable(Class<?> cls, boolean deep) {
if(cls==null) return false;
for(Class<?> interfce:cls.getInterfaces())
if(interfce.equals(Cloneable.class))
if(deep) {
Class<?> supercls = cls.getSuperclass();
if(supercls!=null&&!supercls.equals(Object.class))
return isCloneable(supercls, deep);
else return true;
} else return true;
return false;
}
public static Locale getCurrentLocale() { return Utilities.currentLocale; }
public static void setCurrentLocale(Locale locale) {
currentLocale = locale;
translationBundle = null;
Locale.setDefault(locale);
}
private static ResourceBundle getTranslationBundle() {
return translationBundle!=null?translationBundle:(translationBundle=
ResourceBundle.getBundle("lc/kra/jds/TranslationBundle", currentLocale));
}
public static String getTranslation(String key) { return getTranslation(key, TranslationType.TEXT); }
public static String getTranslation(String key, Object... variables) { return getTranslation(key, TranslationType.TEXT, variables); }
public static String getTranslation(String key, TranslationType type) { return getTranslation(key, type, (Object[])null); }
public static String getTranslation(String key, TranslationType type, Object... variables) {
ResourceBundle bundle = getTranslationBundle();
if(type!=TranslationType.TEXT&&type!=TranslationType.EXTERNAL)
key = new StringBuilder(key).append('.').append(type.toString().toLowerCase()).toString();
if(!bundle.containsKey(key))
bundle = ResourceBundle.getBundle("lc/kra/jds/TranslationBundle");
if(!bundle.containsKey(key))
switch(type) {
case TEXT: case TITLE: return new StringBuilder("text missing ("+key+")").toString();
case ALTERNATIVE: return getTranslation(key);
case EXTERNAL: return key;
default: return null; }
else return MessageFormat.format(bundle.getString(key), variables);
}
public static boolean useAnsiSymbols(){
return useAnsiSymbols;
}
public static void setUseAnsiSymbols(boolean value){
useAnsiSymbols = value;
}
public static URL getResource(String name) {
if(!name.startsWith("/"))
name = "/lc/kra/jds/"+name;
return Utilities.class.getResource(name);
}
public static <Type> Type getField(Class<?> cls, String fieldName) { return getField(cls, fieldName, null); }
@SuppressWarnings("unchecked") public static <Type> Type getField(Class<?> cls, String fieldName, Type dfault) {
try {
Field field = cls.getField(fieldName);
Object value = field.get(null);
if(value==null||(value instanceof String&&value.toString().isEmpty()))
return dfault;
return (Type) value;
} catch(Exception e) { return dfault; }
}
protected static class ByteArrayInputStream extends InputStream {
protected byte[] buffer = null;
protected int count = 0, position = 0;
public ByteArrayInputStream(byte[] buffer, int count) {
this.buffer = buffer;
this.count = count;
}
@Override public final int available() { return count - position; }
@Override public final int read() { return (position < count) ? (buffer[position++] & 0xff) : -1; }
@Override public final int read(byte[] bytes, int offset, int length) {
if(position>=count)
return -1;
if((position+length)>count)
length = count - position;
System.arraycopy(buffer, position, bytes, offset, length);
position += length;
return length;
}
@Override public final long skip(long skip) {
if((position+skip)>count)
skip = count - position;
if(skip<0)
return 0;
position += skip;
return skip;
}
}
protected static class ByteArrayOutputStream extends OutputStream {
protected byte[] buffer = null;
protected int size = 0;
public ByteArrayOutputStream() { this(5 * 1024); }
public ByteArrayOutputStream(int size) {
this.size = 0;
this.buffer = new byte[size];
}
public InputStream getInputStream() {
try {
this.flush(); this.close();
return new ByteArrayInputStream(buffer, size);
} catch (IOException e) { }
return null;
}
private void verifyBufferSize(int size) {
if(size<=buffer.length)
return;
byte[] buffer = this.buffer;
this.buffer = new byte[Math.max(size, 2*this.buffer.length)];
System.arraycopy(buffer, 0, this.buffer, 0, buffer.length);
buffer = null;
}
@Override public final void write(byte bytes[]) {
verifyBufferSize(size+bytes.length);
System.arraycopy(bytes, 0, this.buffer, size, bytes.length);
size += bytes.length;
}
@Override public final void write(byte bytes[], int off, int lenght) {
verifyBufferSize(size+lenght);
System.arraycopy(bytes, off, this.buffer, size, lenght);
size += lenght;
}
@Override public final void write(int bytes) {
verifyBufferSize(size+1);
this.buffer[size++] = (byte)bytes;
}
}
protected static class FastByteArrayOutputStream extends OutputStream {
protected byte[] buffer = null;
protected int size = 0;
public FastByteArrayOutputStream() { this(5*1024); }
public FastByteArrayOutputStream(int size) {
this.buffer = new byte[size];
this.size = 0;
}
private void verifyBufferSize(int size) {
if(size>buffer.length) {
byte[] old = buffer;
buffer = new byte[Math.max(size, 2*buffer.length)];
System.arraycopy(old, 0, buffer, 0, old.length);
old = null;
}
}
public int getSize() { return size; }
public byte[] getByteArray() { return buffer; }
@Override public final void write(byte buffer[]) {
verifyBufferSize(size+buffer.length);
System.arraycopy(buffer, 0, this.buffer, size, buffer.length);
size += buffer.length;
}
@Override public final void write(byte buffer[], int offset, int length) {
verifyBufferSize(size+length);
System.arraycopy(buffer, offset, this.buffer, size, length);
size += length;
}
@Override public final void write(int bit) {
verifyBufferSize(size+1);
this.buffer[size++] = (byte)bit;
}
public void reset() { size = 0; }
public InputStream getInputStream() {
return new FastByteArrayInputStream(buffer, size);
}
}
protected static class FastByteArrayInputStream extends InputStream {
protected byte[] buffer = null;
protected int count=0, position=0;
public FastByteArrayInputStream(byte[] buffer, int count) {
this.buffer = buffer;
this.count = count;
}
@Override public final int available() {
return count-position;
}
@Override public final int read() {
return (position<count)?(buffer[position++]&0xff):-1;
}
@Override public final int read(byte[] buffer, int offset, int length) {
if(position>=count)
return -1;
if((position+length)>count)
length = (count-position);
System.arraycopy(this.buffer, position, buffer, offset, length);
position += length;
return length;
}
@Override public final long skip(long number) {
if((position+number)>count)
number = count-position;
if(number<0)
return 0;
position += number;
return number;
}
}
protected static class DebuggingObjectOutputStream extends ObjectOutputStream {
private static final Field DEPTH_FIELD;
static {
try {
DEPTH_FIELD = ObjectOutputStream.class.getDeclaredField("depth");
DEPTH_FIELD.setAccessible(true);
} catch(NoSuchFieldException e) { throw new AssertionError(e); }
}
public DebuggingObjectOutputStream(OutputStream out) throws IOException {
super(out);
enableReplaceObject(true);
}
@Override
protected Object replaceObject(Object object) throws IOException {
int currentDepth = currentDepth();
for(int ident=0;ident<currentDepth;ident++)
System.out.print("-");
System.out.println(object);
return super.replaceObject(object);
}
private int currentDepth() {
try { return ((Integer)DEPTH_FIELD.get(this))-1; }
catch(IllegalAccessException e) {
throw new AssertionError(e);
}
}
}
public static Cipher createCipher(String hash, int mode) throws GeneralSecurityException {
DESKeySpec keySpecification = new DESKeySpec(hash!=null?hash.getBytes():new byte[0]);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpecification);
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(mode, key);
return cipher;
}
public static String computeHash(String text) {
if(text==null||text.isEmpty())
return null;
try {
byte[] digits = MessageDigest.getInstance("SHA").digest(text.getBytes());
StringBuilder hash = new StringBuilder();
for(byte digit:digits)
hash.append(Integer.toHexString(digit&0xff));
return hash.toString();
} catch (NoSuchAlgorithmException e) { return Integer.toString(text.hashCode()); }
}
public static boolean isNumeric(String number) { try { Integer.parseInt(number); return true; } catch(NumberFormatException e) { return false; }}
public static boolean isOdd(int number) { return (number&1)==1; } //last bit is 1
public static boolean isEven(int number) { return (number&1)==0; } //last bit is 0
private static File getConfigurationFile() {
try {
File file = new File(getLocalPath().toURI()).getParentFile().listFiles(new FilenameFilter() {
@Override public boolean accept(File dir, String name) {
return name.equals("JDigitalSimulator.properties");
}
})[0];
if(!file.exists()||file.isDirectory()||!file.canRead())
throw new FileNotFoundException();
return file;
} catch(Exception e) { //URISyntaxException, NullPointerException, ArrayIndexOutOfBoundsException, FileNotFoundException
return new File(System.getProperty("java.io.tmpdir")+"JDigitalSimulator.properties");
}
}
private static boolean loadConfiguration() {
if(!configuration.isEmpty()) return true;
File file = getConfigurationFile();
if(!file.exists()) return false;
try {
configuration.load(new FileInputStream(file));
} catch (Exception e) { return false; }
return true;
}
private static boolean writeConfiguration() {
if(configuration.isEmpty()) return true;
File file = getConfigurationFile();
try {
if(! file.exists()) file.createNewFile();
configuration.store(new BufferedOutputStream(new FileOutputStream(file)), new String());
} catch (IOException e) { return false; }
return true;
}
public static boolean hasConfiguration(String key) {
return (loadConfiguration()&&configuration.containsKey(key));
}
public static String getConfiguration(String key) {
if(!loadConfiguration()||!configuration.containsKey(key)) return null;
return configuration.getProperty(key);
}
public static boolean setConfiguration(String key, String value) {
configuration.setProperty(key, value);
return writeConfiguration();
}
public static SimpleClassLoader getSimpleClassLoader() {
if(simpleClassLoader==null)
simpleClassLoader = new SimpleClassLoader();
return simpleClassLoader;
}
public static class SimpleClassLoader extends ClassLoader {
public Class<?> loadClass(File file) throws FileNotFoundException, IOException {
byte[] bytes = new byte[(int)file.length()]; BufferedInputStream input = null;
try { (input=new BufferedInputStream(new FileInputStream(file))).read(bytes); }
finally { input.close(); }
return loadClass(bytes);
}
public Class<?> loadClass(ZipFile file, ZipEntry entry) throws IOException {
byte[] bytes = new byte[(int)entry.getSize()];
file.getInputStream(entry).read(bytes);
return loadClass(bytes);
}
private Class<?> loadClass(byte[] bytes) {
Class<?> cls = null;
cls = defineClass(null, bytes, 0, bytes.length);
resolveClass(cls);
return cls;
}
}
public static class AlternateClassLoaderObjectInputStream extends ObjectInputStream {
protected final ClassLoader alternateClassLoader;
public AlternateClassLoaderObjectInputStream(InputStream in, ClassLoader alternateClassLoader) throws IOException {
super(in); this.alternateClassLoader = alternateClassLoader; }
@Override protected Class<?> resolveClass(ObjectStreamClass descriptor) throws IOException, ClassNotFoundException{
try{ return Class.forName(descriptor.getName(), false, alternateClassLoader); }
catch(ClassNotFoundException e){
return super.resolveClass(descriptor);
}
}
}
public static class LegacyObjectInputStream extends AlternateClassLoaderObjectInputStream {
public static final String LEGACY_PACKAGE_PREFIX = "de.ksquared.", PACKAGE_PREFIX = "lc.kra.";
public LegacyObjectInputStream(InputStream in) throws IOException {
super(in, getSimpleClassLoader());
}
@Override protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
ObjectStreamClass descriptor = super.readClassDescriptor(); String name = descriptor.getName();
if(name.contains(LEGACY_PACKAGE_PREFIX)) {
String newName = replaceLegacyPackage(name);
try {
// do not return a new ObjectStreamClass looked up from new class definitions, but modify the descriptor read from the stream, otherwise some flags could be wrong, like the flag "hasWriteObjectData" resulting in an exception when trying to read the object
Field nameField = ObjectStreamClass.class.getDeclaredField("name");
nameField.setAccessible(true);
nameField.set(descriptor, newName);
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
return descriptor;
}
public static String replaceLegacyPackage(String name) {
return name.replaceFirst("^(\\[L)?"+Pattern.quote(LEGACY_PACKAGE_PREFIX), "$1"+Matcher.quoteReplacement(PACKAGE_PREFIX));
}
}
public static URL getLocalPath() {
URL path = Utilities.class.getProtectionDomain().getCodeSource().getLocation();
try { return new URL(URLDecoder.decode(path.toString(), "UTF-8")); }
catch(Exception e) { return path; }
}
public static File getFile(URL ressource) {
StringBuilder file = new StringBuilder();
String host = ressource.getHost(), path = ressource.getPath();
if(host!=null&&!host.isEmpty())
file.append(File.separatorChar).append(File.separatorChar).append(host);
return new File(file.append(path.split("!", 2)[0]).toString());
}
public static boolean isWindows() { return System.getProperty("os.name").startsWith("Windows"); }
public static String cropString(String text, int length) {
if(text.length()<=length)
return text;
return text.substring(0, length)+"...";
}
public static class RememberFileChooser extends JFileChooser {
private static final long serialVersionUID = 1l;
public RememberFileChooser() { super(getLastDirectory()); }
private static File getLastDirectory() {
String directory = getConfiguration("directory");
if(directory!=null) {
File file = new File(directory);
if(file.exists()&&file.isDirectory())
return file;
else return null;
} else return null;
}
@Override public void approveSelection() {
super.approveSelection();
File selected = getSelectedFile();
if(!selected.isDirectory())
setConfiguration("directory", selected.getParentFile().toString());
}
}
}