-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathJF.java
More file actions
executable file
·2179 lines (1969 loc) · 59.7 KB
/
Copy pathJF.java
File metadata and controls
executable file
·2179 lines (1969 loc) · 59.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.*;
import java.nio.file.Files;
import java.nio.file.*;
import java.lang.management.*;
import java.security.cert.Certificate;
import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import javaforce.ffm.*;
/**
* A collection of useful static methods.
*
* @author Peter Quiring
*/
public class JF {
/** Returns JavaForce version as a String. */
public static String getVersion() {
return "116.0";
}
/** Returns JavaForce min native version as a String. */
public static String getMinNativeVersion() {
return "116.0";
}
/** Console app that displays JavaForce version. */
public static void main(String[] args) {
String lib_ver_str = getVersion();
float lib_ver = Float.valueOf(lib_ver_str);
System.out.println("javaforce/" + lib_ver_str);
String nat_ver_str = FFM.getInstance().getVersion();
float nat_ver = Float.valueOf(nat_ver_str);
String min_ver_str = getMinNativeVersion();
float min_ver = Float.valueOf(min_ver_str);
System.out.println("javaforce.native/" + nat_ver_str + (nat_ver < min_ver ? " (incompatible)" : ""));
}
/** End-of-line character. Each OS has preferred style. */
public static final String eol = System.getProperty("line.separator");
/** Is this JVM created by Graal AOT compiler (native-image). */
public static final boolean isGraal = Boolean.getBoolean("java.graal");
/** Returns true if this JVM was created by the Graal AOT compiler (native-image). */
public static final boolean isGraal() {
return isGraal;
}
/** Is the JVM created by JavaForce loader. */
public static final boolean isJavaForceLoader = Boolean.getBoolean("javaforce.loader");
/** Returns true if this JVM was created from JavaForce loaders.
* This also implies that JNI methods are available.
*/
public static boolean isJavaForceLoader() {
return isJavaForceLoader;
}
/** Returns true if JavaForce native API is available (JNI or FFM).
*/
public static boolean hasNativeSupport() {
return isJavaForceLoader || FFM.enabled();
}
/** Delays current thread for specified milliseconds. */
public static void sleep(int milli) {
try {
Thread.sleep(milli);
} catch (InterruptedException e) {
JFLog.log(e);
}
}
/** Print msg to console. */
public static void msg(String msg) {
java.lang.System.out.println(msg);
}
//URL functions
/** Create URL from String. */
public static URL createURL(String url) {
try {
return new URI(url).toURL();
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
/** Removes user info from any URL. */
public static String cleanURL(String url) {
//need to remove user:pass from url
//proto://user:pass@host:port/path?opt1=val1&opt2=val2
int idx = url.indexOf('@');
if (idx == -1) return url; //no user info
int i2 = url.indexOf("://");
if (i2 == -1) return null; //invalid URL
return url.substring(0 ,i2 + 3) + url.substring(idx + 1);
}
/** Reads a URL (http) and returns the contents. */
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 URI(purl).toURL();
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;
}
}
/** Encodes a String into URL format. */
public static String encodeURL(String url) {
try {
return URLEncoder.encode(url, "UTF-8");
} catch (Exception e) {
return null;
}
}
/** Decodes a URL format into a String. */
public static String decodeURL(String url) {
try {
return URLDecoder.decode(url, "UTF-8");
} catch (Exception e) {
return null;
}
}
/** Encode text to display in HTML.
* Escapes: & < >
*/
public static String encodeHTML(String txt) {
return txt.replace("&", "&").replace("<", "<").replace(">", ">");
}
/** String to integer. Returns -1 on error. */
public static int atoi(String str) {
if (str.length() == 0) {
return 0;
}
try {
if (str.charAt(0) == '+') {
str = str.substring(1);
}
return Integer.parseInt(str);
} catch (Exception e) {
JFLog.log(e);
return -1;
}
}
/** String to long. Returns -1 on error. */
public static long atol(String str) {
if (str.length() == 0) {
return 0;
}
try {
if (str.charAt(0) == '+') {
str = str.substring(1);
}
return Long.parseLong(str);
} catch (Exception e) {
JFLog.log(e);
return -1;
}
}
/** String to float. Returns -1 on error. */
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;
}
}
/** String to double. Returns -1 on error. */
public static double atod(String str) {
if (str.length() == 0) {
return 0;
}
try {
return Double.parseDouble(str);
} catch (Exception e) {
JFLog.log(e);
return -1;
}
}
/** String to integer (hex format). Returns -1 on error. */
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 Linux. */
public static boolean isLinux() {
return isUnix() && !isMac();
}
/** Checks if system is jfLinux only. */
public static boolean isJFLinux() {
if (!isLinux()) return false;
return new File("/usr/bin/jfsystem").exists();
}
/** Returns user home folder. */
public static String getUserPath() {
return System.getProperty("user.home");
}
/** Returns current user. */
public static String getCurrentUser() {
return System.getProperty("user.name");
}
/** Returns OS temp path. */
public static String getTempPath() {
if (JF.isWindows()) {
return System.getenv("TEMP");
} else {
return "/tmp";
}
}
private static String user_dir = null;
/** Returns current path.
* NOTE : It is not possible to change current path in Java.
*/
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()
* It is not possible to actually change current path in Java.
*/
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";
}
}
/** Returns system logs path. */
public static String getLogPath() {
if (JF.isWindows()) {
return System.getenv("windir") + "/Logs";
} else {
return "/var/log";
}
}
/** Returns common folder where jar files are stored. */
public static String getClassFolder() {
if (JF.isWindows()) {
return System.getenv("ProgramData") + "/JavaForce";
} else {
return "/usr/share/java";
}
}
/** Returns common folder where jar files are stored for specified app name. */
public static String getClassFolder(String appName) {
if (JF.isWindows()) {
String folder = System.getenv("ProgramFiles");
String folder_appName = folder + "/" + appName;
if (new File(folder_appName).exists()) {
return folder_appName;
}
return getClassFolder();
} else {
return "/usr/share/java";
}
}
/** Returns classpath specified for this JVM (list of jar files). */
public static String getClassPath() {
return System.getProperty("java.class.path");
}
/** Returns system data path. */
public static String getDataPath() {
if (JF.isWindows()) {
return getConfigPath();
} else {
return "/var";
}
}
/** Returns system executable file extension. */
public static String getExecExt() {
if (isWindows()) {
return ".exe";
} else {
return "";
}
}
/** Returns system script file extension. */
public static String getScriptExt() {
if (isWindows()) {
return ".bat";
} else {
return ".sh";
}
}
/** Returns system hostname. */
public static String getHostname() {
if (JF.isWindows()) {
return System.getenv("COMPUTERNAME");
} else {
try {
FileInputStream fis = new FileInputStream("/etc/hostname");
byte[] data = fis.readAllBytes();
fis.close();
return new String(data);
} catch (Exception e) {
JFLog.log(e);
return "localhost";
}
}
}
/** Returns current epoch adjusted for current timezone. */
public static long currentTimeMillis() {
long now = System.currentTimeMillis();
int offset = TimeZone.getDefault().getOffset(now);
return now + offset;
}
/** Returns system environment as array of strings. */
public static String[] getEnvironment() {
ArrayList<String> strs = new ArrayList<>();
Map<String, String> env = System.getenv();
env.forEach((k, v) -> strs.add(k + "=" + v));
return strs.toArray(StringArrayType);
}
/** Prints system environment to console. */
public static void printEnvironment() {
String[] env = getEnvironment();
for(String e : env) {
System.out.println(e);
}
}
/** Returns Java Properties as array of strings. */
public static String[] getSystemProperties() {
ArrayList<String> strs = new ArrayList<>();
Properties p = System.getProperties();
Enumeration keys = p.keys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
String value = (String)p.get(key);
strs.add(key + "=" + value);
}
return strs.toArray(StringArrayType);
}
/** Prints Java Properties to console. */
public static void printSystemProperties() {
String[] env = getSystemProperties();
for(String e : env) {
System.out.println(e);
}
}
//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 byte[] readFile(String file) {
try {
FileInputStream fis = new FileInputStream(file);
byte[] data = fis.readAllBytes();
fis.close();
return data;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public static byte[] readResource(String name) {
try {
InputStream is = JF.class.getResourceAsStream(name);
byte[] data = is.readAllBytes();
is.close();
return data;
} 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 writeFile(String file, byte[] data) {
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
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;
}