forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJF.java
More file actions
executable file
·1483 lines (1354 loc) · 39.7 KB
/
Copy pathJF.java
File metadata and controls
executable file
·1483 lines (1354 loc) · 39.7 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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package javaforce;
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.lang.reflect.*;
import javax.net.ssl.*;
import java.security.*;
import java.security.cert.*;
/**
* A collection of static methods. Many methods help reduce try/catch clutter by
* returning error codes.
*
* @author Peter Quiring
*/
public class JF {
public static String getVersion() {
return "33.1";
}
public static final boolean isGraal = Boolean.getBoolean("java.graal");
public static final boolean isGraal() {
return isGraal;
}
public static void sleep(int milli) {
try {
Thread.sleep(milli);
} catch (InterruptedException e) {
JFLog.log(e);
}
}
public static void msg(String msg) {
java.lang.System.out.println(msg);
}
public static URL createURL(String url) {
try {
return new URL(url);
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public static String readURL(String url) {
String str = "";
byte data[] = new byte[1024];
int read;
String purl = ""; //percent URL
char ch;
for (int p = 0; p < url.length(); p++) {
ch = url.charAt(p);
switch (ch) {
case ' ':
purl += "%20";
break;
case '%':
purl += "%25";
break;
case '<':
purl += "%3c";
break;
case '>':
purl += "%3e";
break;
default:
purl += ch;
break;
}
}
try {
URL u = new URL(purl);
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
huc.connect();
if (huc.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "huc error : " + huc.getResponseCode(); //ohoh
}
InputStream is = huc.getInputStream();
while (true) {
read = is.read(data);
if (read <= 0) {
break;
}
str += new String(data, 0, read);
}
is.close();
huc.disconnect();
return str;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public static int atoi(String str) {
if (str.length() == 0) {
return 0;
}
try {
if (str.charAt(0) == '+') {
return Integer.parseInt(str.substring(1));
}
return Integer.parseInt(str);
} catch (Exception e) {
JFLog.log(e);
return -1;
}
}
public static float atof(String str) {
if (str.length() == 0) {
return 0;
}
try {
return Float.parseFloat(str);
} catch (Exception e) {
JFLog.log(e);
return -1;
}
}
public static int atox(String str) {
if (str.length() == 0) {
return 0;
}
try {
return Integer.parseInt(str, 16);
} catch (Exception e) {
JFLog.log(e);
return -1;
}
}
/** Checks if system is Windows only. */
public static boolean isWindows() {
return (File.separatorChar == '\\');
}
/** Checks if system is Unix based (includes : Unix, Linux, Mac, jfLinux) */
public static boolean isUnix() {
return (File.separatorChar == '/');
}
/** Checks if system is Mac only. */
public static boolean isMac() {
if (!isUnix()) return false;
String osName = System.getProperty("os.name");
return osName.startsWith("Mac") || osName.startsWith("Darwin");
}
/** Checks if system is jfLinux only. */
public static boolean isJFLinux() {
if (!isUnix()) return false;
return new File("/usr/sbin/jlogon").exists();
}
public static String getUserPath() {
return System.getProperty("user.home");
}
public static String getCurrentUser() {
return System.getProperty("user.name");
}
public static String getTempPath() {
if (JF.isWindows()) {
return System.getenv("TEMP");
} else {
return "/tmp";
}
}
private static String user_dir = null;
public static synchronized String getCurrentPath() {
if (user_dir == null) {
user_dir = System.getProperty("user.dir");
}
return user_dir;
}
/**
* This just changes the internal value returned from getCurrentPath()
*/
public static void setCurrentPath(String new_dir) {
user_dir = new_dir;
}
/** Returns system config path for system services. */
public static String getConfigPath() {
if (JF.isWindows()) {
String path = System.getenv("PROGRAMDATA"); //Win Vista/7/8/10
if (path == null) {
path = System.getenv("ALLUSERSPROFILE"); //WinXP
}
return path;
} else {
return "/etc";
}
}
public static String getLogPath() {
if (JF.isWindows()) {
return System.getenv("windir") + "/Logs";
} else {
return "/var/log";
}
}
//file IO helper functions (these are little-endian format!!!)
public static boolean eof(InputStream f) {
try {
return (!(f.available() > 0));
} catch (Exception e) {
JFLog.log(e);
return true;
}
}
public static int filelength(InputStream is) {
try {
return is.available();
} catch (Exception e) {
JFLog.log(e);
return -1;
}
}
public static boolean fileskip(InputStream is, int length) {
try {
is.skip(length);
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static boolean fileflush(BufferedOutputStream bos) {
try {
bos.flush();
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static FileInputStream fileopen(String name) {
try {
return new FileInputStream(name);
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public static FileOutputStream filecreate(String name) {
try {
return new FileOutputStream(name);
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public static FileOutputStream filecreateappend(String name) {
try {
return new FileOutputStream(name, true);
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public static boolean fileclose(InputStream is) {
try {
is.close();
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static boolean fileclose(OutputStream os) {
try {
os.close();
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static byte[] read(InputStream in, int max) {
byte ret[] = new byte[max];
try {
int read = in.read(ret);
if (read == max) {
return ret;
}
if (read == 0) {
return null;
}
byte ret2[] = new byte[read];
System.arraycopy(ret, 0, ret2, 0, read);
return ret2;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public static byte[] readAll(InputStream in) {
try {
int len = in.available();
byte ret[] = new byte[len];
int pos = 0;
while (pos < len) {
int read = in.read(ret, pos, len - pos);
if (read <= 0) {
return null;
}
pos += read;
}
return ret;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public static byte[] readAll(InputStream in, int len) {
try {
byte ret[] = new byte[len];
int pos = 0;
while (pos < len) {
int read = in.read(ret, pos, len - pos);
if (read <= 0) {
return null;
}
pos += read;
}
return ret;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public static boolean readAll(InputStream in, byte buf[], int pos, int len) {
int end = pos + len;
try {
while (pos < end) {
int read = in.read(buf, pos, len);
if (read <= 0) {
return false;
}
pos += read;
len -= read;
}
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static boolean readAll(RandomAccessFile in, byte buf[], int pos, int len) {
int end = pos + len;
try {
while (pos < end) {
int read = in.read(buf, pos, len);
if (read <= 0) {
return false;
}
pos += read;
len -= read;
}
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static byte[] readAll(InputStream in, Socket s) {
try {
byte tmp[] = new byte[1500];
byte ret[] = new byte[0];
while (in.available() > 0 || s.isConnected()) {
int read = in.read(tmp);
if (read <= 0) {
return ret;
}
int pos = ret.length;
ret = Arrays.copyOf(ret, pos + read);
System.arraycopy(tmp, 0, ret, pos, read);
}
return ret;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public static int read(InputStream in, byte[] buf, int pos, int len) {
try {
return in.read(buf, pos, len);
} catch (Exception e) {
JFLog.log(e);
return -1;
}
}
public static int read(InputStream in) {
try {
return in.read();
} catch (Exception e) {
JFLog.log(e);
return -1;
}
}
public static char readchar(InputStream in) {
try {
return (char) in.read();
} catch (Exception e) {
JFLog.log(e);
return 0;
}
}
public static int readuint8(InputStream in) {
byte data[] = new byte[1];
try {
if (in.read(data) != 1) {
return -1;
}
} catch (Exception e) {
JFLog.log(e);
return -1;
}
return (int) data[0] & 0xff;
}
public static int readuint16(InputStream in) {
byte data[] = new byte[2];
try {
if (in.read(data) != 2) {
return -1;
}
} catch (Exception e) {
JFLog.log(e);
return -1;
}
int ret;
ret = (int) data[0] & 0xff;
ret += ((int) data[1] & 0xff) << 8;
return ret;
}
public static int readuint32(InputStream in) {
byte data[] = new byte[4];
try {
if (in.read(data) != 4) {
return -1;
}
} catch (Exception e) {
JFLog.log(e);
return -1;
}
int ret;
ret = (int) data[0] & 0xff;
ret += ((int) data[1] & 0xff) << 8;
ret += ((int) data[2] & 0xff) << 16;
ret += ((int) data[3] & 0xff) << 24;
return ret;
}
public static long readuint64(InputStream in) {
byte data[] = new byte[8];
try {
if (in.read(data) != 8) {
return -1;
}
} catch (Exception e) {
JFLog.log(e);
return -1;
}
long ret;
ret = (long) data[0] & 0xff;
ret += ((long) data[1] & 0xff) << 8;
ret += ((long) data[2] & 0xff) << 16;
ret += ((long) data[3] & 0xff) << 24;
ret += ((long) data[4] & 0xff) << 32;
ret += ((long) data[5] & 0xff) << 40;
ret += ((long) data[6] & 0xff) << 48;
ret += ((long) data[7] & 0xff) << 56;
return ret;
}
public static float readfloat(InputStream in) {
int bits = JF.readuint32(in);
return Float.intBitsToFloat(bits);
}
public static double readdouble(InputStream in) {
long bits = JF.readuint64(in);
return Double.longBitsToDouble(bits);
}
public static boolean write(OutputStream out, byte data[]) {
try {
out.write(data);
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static boolean write(OutputStream out, String str) {
return write(out, str.getBytes());
}
public static boolean write(OutputStream out, byte data[], int offset, int length) {
try {
out.write(data, offset, length);
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static boolean writeuint8(OutputStream out, int data) {
try {
out.write(data);
} catch (Exception e) {
JFLog.log(e);
return false;
}
return true;
}
public static boolean writeuint16(OutputStream out, int data) {
try {
out.write(data & 0xff);
data >>= 8;
out.write(data & 0xff);
} catch (Exception e) {
JFLog.log(e);
return false;
}
return true;
}
public static boolean writeuint32(OutputStream out, int data) {
try {
out.write(data & 0xff);
data >>= 8;
out.write(data & 0xff);
data >>= 8;
out.write(data & 0xff);
data >>= 8;
out.write(data & 0xff);
} catch (Exception e) {
JFLog.log(e);
return false;
}
return true;
}
public static boolean writeuint64(OutputStream out, long data) {
try {
for (int a = 0; a < 8; a++) {
out.write((int) (data & 0xff));
data >>= 8;
}
} catch (Exception e) {
JFLog.log(e);
return false;
}
return true;
}
public static boolean writefloat(OutputStream out, float data) {
int bits = Float.floatToIntBits(data);
return writeuint32(out, bits);
}
public static boolean writedouble(OutputStream out, double data) {
long bits = Double.doubleToLongBits(data);
return writeuint64(out, bits);
}
public static boolean copyAll(InputStream is, OutputStream os) {
try {
byte buf[] = new byte[1024];
while (is.available() > 0) {
int read = is.read(buf);
if (read == 0) {
continue;
}
if (read == -1) {
return false;
}
os.write(buf, 0, read);
}
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static boolean copyAll(String src, String dst) {
try {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dst);
boolean success = copyAll(fis, fos);
fis.close();
fos.close();
return success;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static boolean copyAllAppend(String src, String dst) {
try {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dst, true);
boolean success = copyAll(fis, fos);
fis.close();
fos.close();
return success;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static boolean echoAppend(String str, String dst) {
try {
FileOutputStream fos = new FileOutputStream(dst, true);
fos.write(str.getBytes());
fos.close();
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static boolean copyAll(InputStream is, OutputStream os, long length) {
try {
byte buf[] = new byte[1024];
int copied = 0;
while (copied < length) {
int read = is.read(buf);
if (read == 0) {
continue;
}
if (read == -1) {
return false;
}
os.write(buf, 0, read);
copied += read;
}
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public static boolean isWildcard(String wcstr) {
if (wcstr.indexOf("*") != -1) return true;
if (wcstr.indexOf("?") != -1) return true;
return false;
}
public static boolean wildcardCompare(String fnstr, String wcstr, boolean caseSensitive) {
//wc = "*.txt"
//fn = "filename.txt"
char fn[] = fnstr.toCharArray();
char wc[] = wcstr.toCharArray();
int fnp = 0, wcp = 0;
char wcc, fnc;
int sl, a;
while ((fnp < fn.length) && (wcp < wc.length)) {
if (wc[wcp] == '*') {
sl = fn.length - fnp;
//try 0-sl chars of fn
wcp++;
for (a = 0; a <= sl; a++) {
if (wildcardCompare(
new String(fn, fnp, fn.length - fnp),
new String(fn, fnp, a) + new String(wc, wcp, wc.length - wcp),
caseSensitive)) {
return true;
}
}
return false;
}
if (wc[wcp] == '?') {
//try 0-1 chars of fn
wcp++;
for (a = 0; a <= 1; a++) {
if (wildcardCompare(
new String(fn, fnp, fn.length - fnp),
new String(fn, fnp, a) + new String(wc, wcp, wc.length - wcp),
caseSensitive)) {
return true;
}
}
return false;
}
if (caseSensitive) {
fnc = fn[fnp];
wcc = wc[wcp];
} else {
fnc = Character.toUpperCase(fn[fnp]);
wcc = Character.toUpperCase(wc[wcp]);
}
if (fnc != wcc) {
break; //no match
}
fnp++;
wcp++;
};
while ((wcp < wc.length) && ((wc[wcp] == '*') || (wc[wcp] == '?'))) {
wcp++;
}
if (wcp < wc.length) {
return false; //wc not used up
}
if (fnp < fn.length) {
return false; //fn not used up
}
return true;
}
//String char[] functions (StringBuffer is awkward) (these are NOT null terminating)
public static char[] createstr(String str) {
return str.toCharArray();
}
public static char[] truncstr(char str[], int newlength) {
if (newlength == 0) {
return null;
}
char ret[] = new char[newlength];
if (newlength <= str.length) {
System.arraycopy(str, 0, ret, 0, ret.length);
} else {
System.arraycopy(str, 0, ret, 0, str.length);
for (int a = str.length; a < ret.length; a++) {
ret[a] = 0;
}
}
return ret;
}
public static char[] strcpy(char str[]) {
char ret[] = new char[str.length];
System.arraycopy(str, 0, ret, 0, str.length);
return ret;
}
public static char[] strcat(char s1[], char s2[]) {
if (s1 == null) {
s1 = new char[0];
}
char ret[] = new char[s1.length + s2.length];
System.arraycopy(s1, 0, ret, 0, s1.length);
System.arraycopy(s2, 0, ret, s1.length, s2.length);
return ret;
}
public static char[] strcat(char str[], char ch) {
char ret[];
if (str == null) {
ret = new char[1];
ret[0] = ch;
return ret;
}
ret = new char[str.length + 1];
System.arraycopy(str, 0, ret, 0, str.length);
ret[str.length] = ch;
return ret;
}
public static String createString(char str[]) {
return new String(str);
}
public static boolean strcmp(String s1, String s2) {
return s1.equals(s2);
}
public static boolean stricmp(String s1, String s2) {
return s1.equalsIgnoreCase(s2);
}
public static boolean strcmp(char s1[], char s2[]) {
return strcmp(new String(s1), new String(s2));
}
public static boolean stricmp(char s1[], char s2[]) {
return stricmp(new String(s1), new String(s2));
}
public static void memcpy(Object src[], int srcpos, Object dest[], int destpos, int len) {
System.arraycopy(src, srcpos, dest, destpos, len);
}
public static boolean memcmp(byte m1[], int m1pos, byte[] m2, int m2pos, int len) {
for (int a = 0; a < len; a++) {
if (m1[m1pos + a] != m2[m2pos + a]) {
return false;
}
}
return true;
}
public static boolean memicmp(byte m1[], int m1pos, byte[] m2, int m2pos, int len) {
char c1, c2;
for (int a = 0; a < len; a++) {
c1 = Character.toUpperCase((char) m1[m1pos + a]);
c2 = Character.toUpperCase((char) m2[m2pos + a]);
if (c1 != c2) {
return false;
}
}
return true;
}
//executable JAR functions
public static String getJARPath() {
//this will equal your executable JAR filename (like argv[0] in C++)
return System.getProperty("java.class.path");
}
//misc functions
public static void randomize(Random random) {
random.setSeed(System.currentTimeMillis());
}
//flip endian functions
public static short endian(short x) {
short ret = (short) (x << 8);
ret += x >>> 8;
return ret;
}
public static int endian(int x) {
return (x << 24)
| ((x << 8) & 0xff0000)
| ((x >> 8) & 0xff00)
| (x >>> 24);
}
public static long endian(long x) {
return (x << 56)
| ((x << 40) & 0xff000000000000L)
| ((x << 24) & 0xff0000000000L)
| ((x << 8) & 0xff00000000L)
| ((x >> 8) & 0xff000000L)
| ((x >> 24) & 0xff0000L)
| ((x >> 40) & 0xff00L)
| (x >>> 56);
}
public static int getPID() {
return (int)ProcessHandle.current().pid();
}
//copies arrays excluding one element (something Arrays is missing)
public static Object[] copyOfExcluding(Object[] array, int idx) {
Class<?> cls = array.getClass().getComponentType();
Object newArray[] = (Object[]) Array.newInstance(cls, array.length - 1);
System.arraycopy(array, 0, newArray, 0, idx);
System.arraycopy(array, idx + 1, newArray, idx, array.length - idx - 1);
return newArray;
}
public static boolean[] copyOfExcluding(boolean[] array, int idx) {
boolean newArray[] = new boolean[array.length - 1];
System.arraycopy(array, 0, newArray, 0, idx);
System.arraycopy(array, idx + 1, newArray, idx, array.length - idx - 1);
return newArray;
}
public static byte[] copyOfExcluding(byte[] array, int idx) {
byte newArray[] = new byte[array.length - 1];
System.arraycopy(array, 0, newArray, 0, idx);
System.arraycopy(array, idx + 1, newArray, idx, array.length - idx - 1);
return newArray;
}
public static short[] copyOfExcluding(short[] array, int idx) {
short newArray[] = new short[array.length - 1];
System.arraycopy(array, 0, newArray, 0, idx);
System.arraycopy(array, idx + 1, newArray, idx, array.length - idx - 1);
return newArray;
}
public static int[] copyOfExcluding(int[] array, int idx) {
int newArray[] = new int[array.length - 1];
System.arraycopy(array, 0, newArray, 0, idx);
System.arraycopy(array, idx + 1, newArray, idx, array.length - idx - 1);
return newArray;
}
public static float[] copyOfExcluding(float[] array, int idx) {
float newArray[] = new float[array.length - 1];
System.arraycopy(array, 0, newArray, 0, idx);
System.arraycopy(array, idx + 1, newArray, idx, array.length - idx - 1);
return newArray;
}
public static double[] copyOfExcluding(double[] array, int idx) {
double newArray[] = new double[array.length - 1];
System.arraycopy(array, 0, newArray, 0, idx);
System.arraycopy(array, idx + 1, newArray, idx, array.length - idx - 1);
return newArray;
}
public static char[] copyOfExcluding(char[] array, int idx) {
char newArray[] = new char[array.length - 1];
System.arraycopy(array, 0, newArray, 0, idx);
System.arraycopy(array, idx + 1, newArray, idx, array.length - idx - 1);
return newArray;
}
private static boolean initedHttps = false;
/** This allows connections to untrusted hosts when using https:// with URLConnection. */
public static void initHttps() {
if (initedHttps) return;
initedHttps = true;
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
}
};
// Let us create the factory where we can set some parameters for the connection
try {
SSLContext ctx = SSLContext.getInstance("SSL");
ctx.init(null, trustAllCerts, new SecureRandom());
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) ctx.getSocketFactory(); //this method will work with untrusted certs
HttpsURLConnection.setDefaultSSLSocketFactory(sslsocketfactory);
} catch (Exception e) {
JFLog.log(e);
}
//trust any hostname
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) {
System.out.println("Warning: URL host '" + urlHostName + "' is different to SSLSession host '" + session.getPeerHost() + "'.");
}
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
private static final String[] protocols = new String[] {"TLSv1.3"};
private static final String[] cipher_suites = new String[] {"TLS_AES_128_GCM_SHA256"};
public static Socket connectSSL(String host, int port, KeyMgmt keys) {
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
}
};