-
Notifications
You must be signed in to change notification settings - Fork 2
/
U.java
1160 lines (1037 loc) · 28 KB
/
U.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
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 qgb;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Properties;
import java.util.Set;
import qgb.os.win.Win;
import qgb.text.Regex;
/** QGB's java basic utils **/
public final class U {
public static String gstTestPath = "";
public final static String gstEclipseA = "._abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public final static String gsOneLineTryCatch = "try {prep.close();} catch (SQLException e) {e.printStackTrace();}";
static {
if (OS.isWin()) {
gstTestPath = "D:/test/";
} else if (OS.isAndroid()) {
gstTestPath = "/sdcard/test/";
}
}
/****/
private U() {
throw new Error("Don't let anyone instantiate this class!");
}
// /////////////////////////////////////////////////////////
public static void main(String[] args) throws Exception {
U.print(objToHex(U.class));
U.write("u.c", objSerialize(U.class));
//main(null);
}
public static byte[] objSerialize(Object ao) {
byte[] yb;
try {
// FileOutputStream fos = new FileOutputStream(U.autoPath("u.obj"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(ao);
oos.close();
return bos.toByteArray();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static Object objDeserialize(byte[] ayb) {
Object o;
try {
// FileOutputStream fos = new FileOutputStream(U.autoPath("u.obj"));
ObjectInputStream ois=new ObjectInputStream(U.BytesToInputStream(ayb));
o= ois.readObject();
ois.close();
return o;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static String objToHex(Object ao) {
return F.bytesToHex(objSerialize(ao));
}
public static Object HexToObj(String as) {
return objDeserialize(F.hexToBytes(as));
}
///////////////////////////////////////////////////////////////////
public static String getPublicClassName(String asJavaCode) {
String st = Regex.getFirst(Regex.GS_PubClassName, asJavaCode);
// st=T.replaceAll(st, " ", " ");
int i = st.lastIndexOf("class") + 5;
// U.print(i);
if (i < 1)
return "";
st = st.substring(i, st.length());
return st.trim();
}
public static void runJavaCode(String ast,Object... args) {
}
public static String getClassFolder(Class ac) {
String st = ac.getName().replace(".", "/");
String stpath = ac.getClassLoader().getResource("").getPath();
int i = st.lastIndexOf("/");
if (i < 0) {
return stpath;
}
return stpath + st.substring(0, i + 1);
}
public static String getClassPath(Class ac) {
return getClassFolder(ac) + ac.getSimpleName() + ".class";
}
// ///////////////////////////////////////////////////////
private static long glTimer = -1;
/**
* unit: ms</br> return true:Success! if the method is called firstly or you
* had been call endKeepTime() recently.</br> return false:Fail!.
* **/
public static boolean beginKeepTime() {
if (glTimer < 0) {
glTimer = System.currentTimeMillis();
return true;
}
return false;
}
public static long endKeepTime() {
long lr = System.currentTimeMillis() - glTimer;
glTimer = -1;
return lr;
}
public static long endKeepTime(boolean abPrint) {
long lr = System.currentTimeMillis() - glTimer;
glTimer = -1;
if (abPrint) {
print("Taking %s ms|%s", lr, getCurrentMethod().getName());
}
return lr;
}
/**
* Current class fixed at 2014-09-24 19:36:50
**/
public static Class<?> getCurrentClass() {
StackTraceElement[] yste = Thread.currentThread().getStackTrace();
if (yste.length < 2) {
return null;
}
/** getCurrentClass **/
String str = "";
for (int i = 0; i < yste.length; i++) {
if (yste[i].getMethodName().equals("getCurrentClass")) {
try {
return Class.forName(yste[i + 1].getClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
return null;
}
// public static String getCurrentClassName() {
// return getCurrentClass().getName();
// }
/** TODO:鐫嚖鑳わ拷 */
public static Method getCurrentMethod() {
StackTraceElement[] yste = Thread.currentThread().getStackTrace();
if (yste.length < 2) {
return null;
}
/** getMethodName **/
String str = "";
for (int i = 0; i < yste.length; i++) {
if (yste[i].getMethodName().equals("getCurrentMethod")) {
Class<?> cC = null;
try {
cC = Class.forName(yste[i + 1].getClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method[] ym = cC.getMethods();
str = yste[i + 1].toString();
str = str.substring(0, str.lastIndexOf('('));
// try {
// U.print(cC.getMethod(str, null));
// } catch (Exception e) { e.printStackTrace(); }
// U.print(str);
for (int j = 0; j < ym.length; j++) {
if (str.endsWith(ym[j].getName())) {
return ym[j];
// U.msgbox(str);
}
// U.print(ym[j].getName());
// U.print(ym[i].);
}
}
}
return null;
}
/**
* Thread safe</p>
* **/
public static synchronized String argsError(Object... args)
throws IllegalArgumentException {
StackTraceElement[] yste = Thread.currentThread().getStackTrace();
if (yste.length < 2) {
return null;
}
/** 璇欙拷 */
String str = yste[yste.length - 1 - 1].toString();
// print(yste);
StringBuilder sb = new StringBuilder("\n");
sb.append(str.substring(0, str.lastIndexOf('(') + 1));
int im = args.length;
if (im > 0) {
for (int i = 0; i < im - 1; i++) {
sb.append(args[i] + ",");
}
sb.append(args[im - 1]);
}
sb.append(")|"
+ str.substring(str.lastIndexOf('(') + 1, str.lastIndexOf(')')));
// U.write("ste.txt", str);
// U.print();
throw new IllegalArgumentException(T.lineWrap(sb, 76));
}
// //////////////////////////////////////////////////////
public static void sleep(long alms) {
try {
java.lang.Thread.sleep(alms);
} catch (InterruptedException e) {
error(e, "U.sleep(" + alms + ") Interrupted!");
}
}
// /////////////////////////////////////////////////////
public static void printProperties() {
Properties pes = System.getProperties();
Set<String> set = pes.stringPropertyNames();
for (String str : set) {
System.out.printf("%s=%s\n", T.pad(str, 29, " "),
pes.getProperty(str));
}
}
public static void printEclipse() {
print("Max console line char length=96");
}
// /////////////////////////////////////////////////////////
public static String getCurrentThreadName() {
return Thread.currentThread().getName();
}
public static void thread(int ai) {
U.print("%d:%s", ai, Thread.currentThread().getName());
}
// ///////////////////////////////////////////////////////
public static String time() {
return qgb.time.TimeConvert.toString(System.currentTimeMillis());
}
// ///////////////////////////////////////////////////////
public static Process run(String ast) {
Runtime rt = Runtime.getRuntime();
try {
return rt.exec(ast);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// /////////////////////////////////////////////////////////
public static String msgbox(Object message, Object initialSelectionValue) {
if (OS.isWin()) {
return Win.msgbox(message, initialSelectionValue);
}
return "";
}
/**
* Shows a question-message dialog requesting input from the user. The
* dialog uses the default frame, which usually means it is centered on the
* screen.
*
* @param message
* the Object to display
* @return the <code>String</code> property
* @see java.awt.GraphicsEnvironment.isHeadless
*/
public static String msgbox(Object message) {
return msgbox(message, message);
}
public static String msgbox(String format, Object... args) {
return msgbox(T.format(format, args));
}
public static void msgbox() {
msgbox("pause","pause");
}
// /////////////////////////////////
public static void notify(String ast_text) {
Throwable taa = new Throwable();
StackTraceElement[] yste = taa.getStackTrace();
for (int i = 0; i < yste.length - 1; i++) {
U.print((i + 1) + ":" + yste[yste.length - 1 - i]);
}
U.print(ast_text + "|" + getCurrentTime());
}
// ////////////////////////////////////////////////////////
public static String getCurrentTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 鑺傞潻寮�
// System.out.println(df.format(new Date()));// new
// Date()涓哄彇鍓嶇郴缁熸椂
return df.format(new Date());
}
// ///////// Print /////////////////////////////
public static boolean printInnerSt(Object ae) {
Field[] yf = ae.getClass().getDeclaredFields();
boolean bool = false;
for (int i = 0; i < yf.length; i++) {
// U.msgbox(yf[i].toString());
yf[i].setAccessible(true);
if (String.class == yf[i].getType()) {
String sta = null;
try {
sta = (String) yf[i].get(ae);
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
if (sta != null) {
U.print("%s=\"%s\"", "o." + yf[i].getName(), sta);
bool = true;
}
}
}
return bool;
}
public static boolean printDetaills(Object ae, String astName) {
Field[] yf = ae.getClass().getDeclaredFields();
boolean bool = false;
for (int i = 0; i < yf.length; i++) {
// U.msgbox(yf[i].toString());
yf[i].setAccessible(true);
Object object = null;
try {
object = yf[i].get(ae);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
continue;
}
if (object == null) {
continue;
}
if (String.class == yf[i].getType()) {
String sta = null;
try {
sta = (String) yf[i].get(ae);
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
if (sta != null) {
U.print("%s=\"%s\"", astName + "." + yf[i].getName(), sta);
bool = true;
}
/** TODO 鏃犳硶杈撳嚭null鍊�**/
// else {
// U.print("%s=null", astName + "." + yf[i].getName());
// }
continue;
} else {
// if (Entry.class == yf[i].getType()) {
// continue;
// }
if (printDetaills(object, astName + "." + yf[i].getName()) == false) {
String stmp = object.toString();
// if (stmp.startsWith("mh.struct.")) {
// continue;
// }
U.print("%s.toString()=\"%s\"",
astName + "." + yf[i].getName(), stmp);
}
}
}
return bool;
}
/** Print, NO breakline */
public static void pt(Object aoa) {
System.out.print(aoa);
}
/** Print Thread,Class,Time **/
public static void print() {
print(getCurrentThreadName());
print(getCurrentClass().getName());
print(getCurrentTime());
print(getCmdToRun());
}
public static void print(Object aoa) {
System.out.println(aoa);
}
public static void print(Collection<?> ac) {
if (ac == null) {
return;
}
String stFormat = "[%-" + get_intDigit(ac.size()) + "s]=%s";
int i=0;
String sT="?";
for (Object o : ac) {
U.print(stFormat, i++, o.toString());
if (i==1) {
sT=o.getClass().getName();
}
}
U.print("Collection<%s>.size()=%d",sT, i);
}
public static void print(int[] ayi) {
if (ayi == null) {
return;
}
if (ayi.length > 0) {
U.print("%s[%d]", "int", ayi.length);
}
String stFormat = "[%-" + get_intDigit(ayi.length) + "s]=%s";
for (int i = 0; i < ayi.length; i++) {
print(stFormat, i, ayi[i]);
}
}
public static void print(byte[] ay) {
if (ay == null) {
return;
}
if (ay.length > 0) {
pt(T.format( "%s[%d]", "byte", ay.length));
}
String stFormat = "%-" + get_intDigit(ay.length/16) + "s|";
final int im=ay.length;
int iline=1;
for (int i = 0; i <im ; i++) {
if(i%16==0){
pt("\n");
pt(T.format(stFormat,iline++));
}
pt(F.byteToHex(ay[i])+" ");
// print(stFormat, i, ay[i]);
}
pt("\n"+T.repeat(64,"-")+"\n");
pt(T.format(stFormat,0));
byte[] yb={1,2,3,4,5,6,7,8,9,0x10,0x11,0x12,0x13,0x14,0x15,0x16};
for (int i = 0; i < 16; i++) {
pt(F.byteToHex(yb[i])+" ");
}
pt("\n");
}
public static void print(char[] ayi) {
if (ayi == null) {
return;
}
if (ayi.length > 0) {
U.print("%s[%d]", "char", ayi.length);
}
String stFormat = "[%-" + get_intDigit(ayi.length) + "s]=%s";
for (int i = 0; i < ayi.length; i++) {
print(stFormat, i, ayi[i]);
}
}
/**
* modified at 2014-10-21 20:38:31
* **/
public static void print(Object ayo[]) {
if (ayo == null) {
U.print("[null]");
return;
}
if (ayo.length > 0) {
U.print("%s[%d]", ayo[0].getClass().getName(), ayo.length);
}else {
U.print("%s[%d]", ayo.getClass().getName(), ayo.length);
}
String stFormat = "[%-" + U.get_intDigit(ayo.length - 1) + "s]=%s";
for (int i = 0; i < ayo.length; i++) {
U.print(stFormat, i, ayo[i]);
}
}
/**
* Fixed at 2014-08-12 20:02:29
**/
public static void print(String ast_text, int ai_size) {
if (ast_text.contains("%")) {
/** void qgb.U.print(String format, Object... args) **/
U.print(ast_text + "%s", ai_size, "");
// U.msgbox();
return;
}
if (ai_size > ast_text.length()) {
ai_size = ast_text.length();
}
System.out.println(ast_text.subSequence(0, ai_size));
}
/**
* fixed at 2014-07-12 22:06:13|auto new line
* **/
public static void print(String format, Object... args) {
System.out.printf(format + "\n", args);
}
// /////////// Print End //////////////////////
public static int get_intDigit(int ai) {
return String.valueOf(ai).length();
}
// ////////////////////////////////////////////////////
public static String cin(String ast_parentComponent, String ast_message) {
return (msgbox(ast_parentComponent, ast_message));
}
public static String cin(String ast_message) {
return (msgbox("Pleaese input text :", ast_message));
}
// ///////////////////////////////////////////////////
public static void write(File filename, String ast_text) {
FileWriter fw = null;
try {
fw = new FileWriter(filename);
fw.write(ast_text);
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* writes ast_text to (gstTestPath +ast_filename)
*
* @see java.io.FileWriter
* @see qgb.U.autoPath
*/
public static void write(String ast_filename, String ast_text) {
FileWriter fw = null;
try {
fw = new FileWriter(autoPath(ast_filename));
fw.write(ast_text);
fw.flush();
// tprint(ast_text);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 绲榮t_filename .澶翠负鍓嶈矾 </p> modified at 2014-07-23 02:08:51 modified at
* 2014-07-31 03:00:19 TODO:mac os 閾板嚖琛椦嶏拷
**/
public static String autoPath(String ast_filename) {
if (ast_filename.startsWith(".")) {
return ast_filename;
}
if (isFullPath(ast_filename)) {// 渚ョ》瑙掑嚖涓哄叏璺�
makeDirs(ast_filename);
return ast_filename;
} else {
makeDirs(gstTestPath + ast_filename);
return (gstTestPath + ast_filename);
}
}
/**
* Encodes this {@code String} into a sequence of bytes using the named
* charset, storing the result into (gstTestPath +ast_filename).
*
* @param ast_filename
* .
* @param ast_text
* .
* @param ast_bytes
* . The name of a supported
* {@linkplain java.nio.charset.Charset charset} ,"UTF-8" etc.
* @see java.nio.charset.Charset
* @see qgb.T#write(String, InputStream)
* @see java.lang.String#getBytes()
*/
public static void write(String ast_filename, String ast_text,
String CharsetName) {
InputStream isa;
try {
isa = BytesToInputStream(ast_text.getBytes(CharsetName));
write(ast_filename, isa);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// U.print(getCurrentMethod());
}
/**
* 2014-2-7 23:00:06
*
* @version 1.1 fixed
*/
public static void write(String ast_filename, InputStream abis) {
byte[] data = new byte[1];
String sFileName = autoPath(ast_filename);
try {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(sFileName));
while (abis.read(data) != -1) {
bos.write(data);
// U.notify("U.write(" + gstTestPath + ast_filename
// + ",InputStream)");
}
// 鍙楗侯偓鍙揪锟�
bos.flush();
// 鎴唻
// bufferedInputStream.close();
bos.close();
abis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void write(String ast_filename, byte[] ayb) {
write(ast_filename, BytesToInputStream(ayb));
}
// /////////////////////////////////////////////////////////////
public static void makeDirs(String fileName) {
File f = new File(fileName);
// U.print("%s isD %b",fileName,F.isDirectory(fileName));
/** 浣縁ile.isDirectory()鍙涓�妭纰変茎纭疯鍑や负鐩綍 **/
if (F.isDirectory(fileName) == false) {
f = f.getParentFile();
if (f == null) {
return;
}
}
// U.print(f.getPath());
if (f.exists()) {
// notify("illegal fileName="+fileName);
return;
}
f.mkdirs();
if (f.exists() == false) {
notify("illegal fileName=" + fileName);
return;
}
}
// ////////////////////////////////////////////
/**
* Automatic judgment on the file path
* <p>
* default charset is GBK
*
* @version 1.1
* @see qgb.T#readSt(String, String)
*/
public static String readSt(String ast_filename) {
BufferedInputStream bis;
bis = readBis(ast_filename);
InputStreamReader isr = new InputStreamReader(bis);
// print("qgb"+isr.getEncoding());
try {
String str = new String(InputStreamToBytes(bis), isr.getEncoding());
bis.close();
return str;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* Automatic judgment on the file path
* <p>
* decodes this file into a sequence of {@code String} using the named
* charset.
*
* @param ast_filename
*
* @param ast_bytes
* , The name of a supported
* {@linkplain java.nio.charset.Charset charset} ,"UTF-8" etc.
* @see java.nio.charset.Charset
* @see qgb.T#readBis(String)
* @see java.lang.String#getBytes()
*/
public static String readSt(String fileName, String ast_CharsetName) {
try {
return new String(InputStreamToBytes(readBis(fileName)),
ast_CharsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
/**
* Automatic judgment on the file path
*
* @see qgb.U.autoPath
*/
public static BufferedInputStream readBis(String fileName) {
File file = new File(autoPath(fileName));
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(fis);
// MP3player.play(bis);//test
return bis;
}
/**
* 2014-10-03 16:58:21
*
* @throws FileNotFoundException
* @see qgb.U.autoPath
*/
public static InputStream readIs(String fileName)
throws FileNotFoundException {
File file = new File(autoPath(fileName));
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
return fis;
} catch (FileNotFoundException e) {
throw e;
// e.printStackTrace();
}
}
public static boolean isFullPath(String fileName) {
return (fileName.contains(":"));
}
// ////////////////////////////////////////////////////////////////
public static void exit(boolean... abnotify) {
if (abnotify.length < 1) {
System.exit(0);
}
notify("Exit!");
System.exit(0);
}
public static void exit() {
System.exit(0);
}
// //////////////////////////////////////////////////////////////
/**
* Tested 2014-3-24 0:46:50
*
* @param abis
* @param ast_charSet
* @return
* @throws IOException
*/
public static String InputStreamToString(InputStream abis,
String ast_charSet) {
if (abis == null) {
U.notify("InputStream null!");
return null;
}
String str = "";
try {
InputStreamReader isr = new InputStreamReader(abis, ast_charSet);
int ich;
while ((ich = isr.read()) != -1) {
str = str + (char) ich;
}
isr.close();
} catch (UnsupportedEncodingException e) {
U.error(e, "UnsupportedEncodingException");
} catch (IOException e) {
U.error(e, "IOException");
}
// U.print(str);
return str;
}
// //////////////////////////////////////////////////////////////////
/** if parameter is null then return null **/
public static byte[] InputStreamToBytes(InputStream ais) {
if (ais == null) {
return null;
}
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
byte imgdata[] = null;
try {
while ((ch = ais.read()) != -1) {
bytestream.write(ch);
}
imgdata = bytestream.toByteArray();
bytestream.close();
ais.close();
} catch (IOException e) {
e.printStackTrace();
}
return imgdata;
}
// ////////////////////////////////////////////////////////////////
/** if parameter is null then return null **/
public static InputStream BytesToInputStream(byte[] b) {
if (b == null) {
return null;
} else {
return new ByteArrayInputStream(b);
}
}
// //////////////////////////////////////////////////////////////////
public static void error(Exception e) {
e.printStackTrace();
}
public static void error(Exception e, String ast) {
System.out.flush();
System.err.flush();
U.print(ast + "|" + time());
System.out.flush();
e.printStackTrace();
System.err.flush();
}
public static byte[] readBytes(String fileName) {
return InputStreamToBytes(readBis(fileName));
}
public static void delFile(String asPath) {
File file = new File(autoPath(asPath));
if (file.exists()) {
if (file.delete() == false) {
U.notify("U.delFile error file is not deleted!"+autoPath(asPath));
}
} else {
U.notify("U.delFile error file is not found! "+autoPath(asPath));
}
}
public static void setProxy(String asIP, String asPort) {
System.getProperties().setProperty("proxySet", "true");
System.getProperties().setProperty("http.proxyHost", asIP);
System.getProperties().setProperty("http.proxyPort", asPort);
}
/*********** setOutStream *************************/
public static void setOutStream(String ast) {
try {
PrintStream ps = new PrintStream(autoPath(ast),
CharsetName.GST_UTF8);
System.setOut(ps);
} catch (FileNotFoundException e) {
U.error(e, ast + " Not Found");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static void setErrStream(String ast) {
try {
PrintStream ps = new PrintStream(autoPath(ast),
CharsetName.GST_UTF8);
System.setErr(ps);
} catch (FileNotFoundException e) {
U.error(e, ast + " Not Found");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static void resetErrStream() {
resetErrStream(true, CharsetName.GST_UTF8);
}
public static void resetErrStream(boolean bFlush, String asCharsetName) {
try {
System.setErr(new PrintStream(new FileOutputStream(
FileDescriptor.err), bFlush, asCharsetName));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static void resetOutStream() {
resetOutStream(true, CharsetName.GST_UTF8);
}
public static void resetOutStream(String asCharsetName) {
resetOutStream(true, asCharsetName);
}
public static void resetOutStream(boolean bFlush, String asCharsetName) {
try {
System.setOut(new PrintStream(new FileOutputStream(
FileDescriptor.out), bFlush, asCharsetName));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/*********** setOutStream End ********************/
/**
* The method max(int, int) in the type T is not applicable for the
* arguments (int, null)
**/
public static int max(int aia, int aib) {
if (aia > aib) {