/ictclas4j/src/com/gftech/util/GFCommon.java

http://ictclas4j.googlecode.com/ · Java · 593 lines · 354 code · 81 blank · 158 comment · 133 complexity · 3e3a5dc0ae770460e4fdcbcf407205f0 MD5 · raw file

  1. /*
  2. * Created on 2005-1-10
  3. *
  4. * TODO To change the template for this generated file go to Window -
  5. * Preferences - Java - Code Style - Code Templates
  6. */
  7. package com.gftech.util;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.util.ArrayList;
  12. import java.util.Stack;
  13. public class GFCommon {
  14. /**
  15. * ????????COPY?????????????? ??maxlen??source????????source????0???maxlen??
  16. * ??maxlen??source????????source??????????????maxlen?
  17. *
  18. * @param d
  19. * destination ??
  20. * @param s
  21. * source??
  22. * @param from
  23. * destination????
  24. * @param maxlen
  25. * source??????COPY?destination?????????????
  26. * @return ??????????????
  27. */
  28. public static int bytesCopy(byte d[], byte s[], int from, int maxlen) {
  29. int end = from;
  30. if (s != null && d != null) {
  31. if (from >= 0 && maxlen > 0) {
  32. if (s.length < maxlen) {
  33. for (int i = 0; i < s.length && i + from < d.length; i++)
  34. d[i + from] = s[i];
  35. end = from + maxlen - 1;
  36. } else {
  37. for (int i = 0; i < maxlen && i + from < d.length; i++) {
  38. d[i + from] = s[i];
  39. end = i + from;
  40. }
  41. }
  42. } else if (from < 0 && maxlen > 0) {
  43. for (int i = d.length + from, j = 0; i > 0 && j < (s.length > maxlen ? maxlen : s.length); i--, j++) {
  44. d[i] = s[j];
  45. end = i;
  46. }
  47. }
  48. }
  49. return end;
  50. }
  51. /**
  52. * ??????????????????len??? ??from??????????????len??
  53. *
  54. * @param src
  55. * ???
  56. * @param from
  57. * ????
  58. * @param len
  59. * COPY???
  60. * @return ???????
  61. */
  62. public static byte[] bytesCopy(byte[] src, int from, int len) {
  63. byte[] result = null;
  64. int totalLen = 0;
  65. if (src != null && src.length > 0 && len > 0) {
  66. if (from >= 0) {
  67. totalLen = src.length > from + len ? len : src.length - from;
  68. result = new byte[Math.abs(len)];
  69. for (int i = from, j = 0; i < from + totalLen; i++, j++)
  70. result[j] = src[i];
  71. } else {
  72. int i0 = src.length + from;// ??????
  73. if (i0 >= 0) {
  74. if (i0 - len < 0)
  75. totalLen = i0 + 1;
  76. else
  77. totalLen = len;
  78. result = new byte[totalLen];
  79. for (int i = i0, j = 0; i >= 0 && j < totalLen; i--, j++)
  80. result[j] = src[i];
  81. }
  82. }
  83. }
  84. return result;
  85. }
  86. public static byte[] int2bytes(int a, boolean isHighFirst) {
  87. byte[] result = new byte[4];
  88. if (isHighFirst) {
  89. result[0] = (byte) (a >> 24 & 0xff);
  90. result[1] = (byte) (a >> 16 & 0xff);
  91. result[2] = (byte) (a >> 8 & 0xff);
  92. result[3] = (byte) (a & 0xff);
  93. } else {
  94. result[3] = (byte) (a >> 24 & 0xff);
  95. result[2] = (byte) (a >> 16 & 0xff);
  96. result[1] = (byte) (a >> 8 & 0xff);
  97. result[0] = (byte) (a & 0xff);
  98. }
  99. return result;
  100. }
  101. public static byte[] int2bytes(int a) {
  102. return int2bytes(a, true);
  103. }
  104. /**
  105. * ?????????
  106. *
  107. * @param obj
  108. * ??
  109. * @return ?????
  110. */
  111. public static String getClassName(Object obj) {
  112. String name = null;
  113. if (obj != null) {
  114. int index = 0;
  115. String temp = obj.getClass().toString();
  116. index = temp.lastIndexOf(".");
  117. if (index > 0 && index < temp.length())
  118. name = temp.substring(index + 1);
  119. }
  120. return name;
  121. }
  122. public static int bytes2int(byte[] b) {
  123. return (int) bytes2long(b);
  124. }
  125. public static int bytes2int(byte[] b, boolean isHighFirst) {
  126. return (int) bytes2long(b, isHighFirst);
  127. }
  128. /**
  129. * ????????????????????
  130. *
  131. * @param b
  132. * @return
  133. */
  134. public static long bytes2long(byte[] b) {
  135. return bytes2long(b, true);
  136. }
  137. /**
  138. * ?????????
  139. *
  140. * @param b
  141. * @param isHighFirst
  142. * ??????
  143. * @return
  144. */
  145. public static long bytes2long(byte[] b, boolean isHighFirst) {
  146. long result = 0;
  147. if (b != null && b.length <= 8) {
  148. long value;
  149. if (isHighFirst) {
  150. for (int i = b.length - 1, j = 0; i >= 0; i--, j++) {
  151. value = (long) (b[i] & 0xFF);
  152. result += value << (j << 3);
  153. }
  154. } else {
  155. for (int i = 0, j = 0; i < b.length - 1; i++, j++) {
  156. value = (long) (b[i] & 0xFF);
  157. result += value << (j << 3);
  158. }
  159. }
  160. }
  161. return result;
  162. }
  163. public static String byte2bin(byte b) {
  164. String result = "";
  165. for (int i = 0; i < 8; i++)
  166. if (((b >>> (7 - i)) & 1) == 0)
  167. result += "0";
  168. else
  169. result += "1";
  170. return result;
  171. }
  172. public static String int2bin(int value) {
  173. String result = "";
  174. for (int i = 0; i < 32; i++)
  175. if (((value >>> (31 - i)) & 1) == 0) {
  176. if (result.length() != 0)
  177. result += "0";
  178. } else
  179. result += "1";
  180. if (result.length() == 0)
  181. result = "0";
  182. return result;
  183. }
  184. public static String long2bin(long value) {
  185. String result = "";
  186. for (int i = 0; i < 64; i++)
  187. if (((value >>> (63 - i)) & 1) == 0)
  188. result += "0";
  189. else
  190. result += "1";
  191. return result;
  192. }
  193. public static byte[] long2bytes(long value) {
  194. return long2bytes(value, true);
  195. }
  196. public static byte[] long2bytes(long value, boolean isHighFirst) {
  197. byte[] b = new byte[8];
  198. if (isHighFirst) {
  199. for (int i = 0; i < 8; i++) {
  200. b[i] = (byte) (value >> (8 * (7 - i)) & 0xFF);
  201. // System.out.println("b["+i+"]:"+GFString.byte2hex(b[i]));
  202. }
  203. } else {
  204. for (int i = 0, j = 7; i < 8; i++, j--)
  205. b[j] = (byte) (value >> (8 * (7 - i)) & 0xFF);
  206. }
  207. return b;
  208. }
  209. /**
  210. * ???IP??,?219.11.33.44???????:219011033044
  211. *
  212. * @param ip
  213. * @return
  214. */
  215. public static String formatIP(String ip) {
  216. String result = null;
  217. if (ip != null) {
  218. String[] p = new String[4];
  219. int index = ip.indexOf(".");
  220. if (index > 0 && index < ip.length() - 1)
  221. p[0] = ip.substring(0, index);
  222. else
  223. return null;
  224. ip = ip.substring(index + 1);
  225. index = ip.indexOf(".");
  226. if (index > 0 && index < ip.length() - 1)
  227. p[1] = ip.substring(0, index);
  228. else
  229. return null;
  230. ip = ip.substring(index + 1);
  231. index = ip.indexOf(".");
  232. if (index > 0 && index < ip.length() - 1)
  233. p[2] = ip.substring(0, index);
  234. else
  235. return null;
  236. p[3] = ip.substring(index + 1);
  237. if (p != null && p.length == 4) {
  238. result = GFString.getFixedLenStr(p[0], 3, '0');
  239. result += GFString.getFixedLenStr(p[1], 3, '0');
  240. result += GFString.getFixedLenStr(p[2], 3, '0');
  241. result += GFString.getFixedLenStr(p[3], 3, '0');
  242. }
  243. }
  244. return result;
  245. }
  246. public static boolean isActiveThread(ThreadGroup group, String threadName) {
  247. if (group != null && threadName != null) {
  248. Thread[] thd = new Thread[group.activeCount()];
  249. group.enumerate(thd);
  250. String name = null;
  251. for (int i = 0; i < thd.length && thd[i] != null; i++) {
  252. name = thd[i].getName();
  253. if (name != null && name.equals(threadName))
  254. return true;
  255. }
  256. }
  257. return false;
  258. }
  259. /**
  260. * ???????
  261. * <p>
  262. * ????????JAVA????
  263. *
  264. * @return
  265. */
  266. public static String getSystemInfo() {
  267. String result = "os.name:" + System.getProperty("os.name") + "\n" + "os.arch:" + System.getProperty("os.arch") + "\n\n" + "java.vendor:"
  268. + System.getProperty("java.vendor") + "\n" + "java.home:" + System.getProperty("java.home") + "\n" + "java.version:"
  269. + System.getProperty("java.version") + "\n" + "java.vm.version:" + System.getProperty("java.vm.version") + "\n\n" + "user.name:"
  270. + System.getProperty("user.name") + "\n" + "user.dir:" + System.getProperty("user.dir");
  271. return result;
  272. }
  273. /**
  274. * ?????????
  275. *
  276. * @param driver
  277. * ???????
  278. * @param url
  279. * ???URL??
  280. * @param userName
  281. * ????????
  282. * @param pwd
  283. * ????
  284. * @param conn
  285. * ?????
  286. * @return ?????
  287. */
  288. public static Connection getConn(String driver, String url, String userName, String pwd) {
  289. Connection conn = null;
  290. if (driver != null && url != null && userName != null && pwd != null) {
  291. try {
  292. Class.forName(driver);
  293. conn = DriverManager.getConnection(url, userName, pwd);
  294. if (conn != null) {
  295. String str = "???????????!";
  296. System.out.println(str);
  297. }
  298. } catch (ClassNotFoundException e) {
  299. } catch (SQLException e) {
  300. e.printStackTrace();
  301. }
  302. }
  303. return conn;
  304. }
  305. /**
  306. * ??Class????????
  307. *
  308. * @param className
  309. * ??
  310. * @return Class???????????Class????????????com.gftech.web.Test,?????????/E:/gftech/project/web/bin/
  311. *
  312. */
  313. public static String getClassPath(String className) {
  314. try {
  315. return Class.forName(className).getClassLoader().getResource("").getPath();
  316. } catch (ClassNotFoundException e) {
  317. e.printStackTrace();
  318. }
  319. return null;
  320. }
  321. /**
  322. * ??Class????????
  323. *
  324. * @param objName
  325. * ????
  326. * @return Class???????????Class????????????com.gftech.web.Test,?????????/E:/gftech/project/web/bin/
  327. *
  328. */
  329. public static String getClassPath(Object objName) {
  330. return objName.getClass().getClassLoader().getResource("").getPath();
  331. }
  332. /**
  333. * ??Jsp?????WEB-INF????
  334. *
  335. * @param classPath
  336. * WEB-INF/classes???????/E:/web/myjsp/WEB-INF/classes/
  337. * @return WEB-INF???????E:\web\myjsp\WEB-INF\
  338. */
  339. public static String getWebinfPath(String classPath) {
  340. String path = null;
  341. if (classPath != null) {
  342. String[] strs = classPath.split("/");
  343. path = "";
  344. for (int i = 1; i < strs.length - 1; i++) {
  345. // System.out.println("S:" + s);
  346. if (strs[i] != null)
  347. path += strs[i] + System.getProperty("file.separator");
  348. }
  349. }
  350. return path;
  351. }
  352. /**
  353. * ???????seed????
  354. *
  355. * @param seed
  356. * ????????
  357. * @return ???
  358. */
  359. public static int random(int seed) {
  360. long result = 0;
  361. if (seed != 0) {
  362. double d = Math.random();
  363. String temp = d + "";
  364. // System.out.println("temp:" + temp);
  365. int len = temp.length() - 2;// ??????
  366. d = d * Math.pow(10, len);
  367. // System.out.println("d:" + d);
  368. result = (long) d % seed;
  369. }
  370. return (int) result;
  371. }
  372. /**
  373. * ?????min?max??????
  374. *
  375. * @param min
  376. * ???
  377. * @param max
  378. * ???
  379. * @return
  380. */
  381. public static int random(int min, int max) {
  382. int rd = random(max);
  383. if (rd >= min)
  384. return rd;
  385. else
  386. return random(min, max);
  387. }
  388. /**
  389. * ?????0?????b??????
  390. *
  391. * @param b
  392. * @return
  393. */
  394. public static int getZeroIndex(byte[] b) {
  395. if (b != null) {
  396. for (int i = 0; i < b.length; i++) {
  397. if (b[i] == 0)
  398. return i;
  399. }
  400. }
  401. return -1;
  402. }
  403. /**
  404. * ???????0?
  405. *
  406. * @param b
  407. * @return
  408. */
  409. public static boolean isHasZero(byte[] b) {
  410. if (b == null)
  411. return true;
  412. for (byte b1 : b)
  413. if (b1 == 0)
  414. return true;
  415. return false;
  416. }
  417. /**
  418. * ????????????,??:1--?,5--?
  419. *
  420. * @param num
  421. * @return
  422. */
  423. public static String num2cnum(int num) {
  424. if (num > -1 && num < 10) {
  425. return GFFinal.CHINA_NUMBER[num];
  426. }
  427. return null;
  428. }
  429. /**
  430. * ????????????????,??:1--?,5--?
  431. *
  432. * @param num
  433. * @return
  434. */
  435. public static String num2anum(int num) {
  436. if (num > -1 && num < 10) {
  437. return GFFinal.ACCOUNT_NUMBER[num];
  438. }
  439. return null;
  440. }
  441. public static int getUnsigned(byte b) {
  442. if (b > 0)
  443. return (int) b;
  444. else
  445. return (b & 0x7F + 128);
  446. }
  447. // ?????????????????????????????
  448. public static void appendInterpunction(StringBuffer sb, String interpunction) {
  449. if (sb != null) {
  450. int size = sb.toString().length();
  451. if (size > 0) {
  452. String last = sb.substring(size - 1, size);
  453. if (GFString.isInterpunction(last))
  454. return;
  455. }
  456. sb.append(interpunction);
  457. }
  458. }
  459. /**
  460. * ????????(?????
  461. *
  462. * @param list
  463. * ????
  464. * @param start
  465. * ????
  466. * @param end
  467. * ????
  468. */
  469. public static void quickSort(ArrayList<Comparable> list, int start, int end) {
  470. if (list != null && list.size() > 1) {
  471. Stack<Integer> s = new Stack<Integer>();
  472. s.push(start);
  473. s.push(end);
  474. while (s.size() > 0) {
  475. int j = s.pop();
  476. int i = s.pop();
  477. while (i < j) {
  478. int p = quickSort0(list, i, j);
  479. if (p - i < j - p) {
  480. s.push(p + 1);
  481. s.push(j);
  482. j = p - 1;
  483. } else {
  484. s.push(i);
  485. s.push(p - 1);
  486. i = p + 1;
  487. }
  488. }
  489. }
  490. }
  491. }
  492. private static int quickSort0(ArrayList<Comparable> list, int start, int end) {
  493. if (list != null) {
  494. int i = start;
  495. int j = end;
  496. Comparable pivot = list.get(start);
  497. while (i < j) {
  498. while (i < j && pivot.compareTo(list.get(j)) < 0)
  499. j--;
  500. if (i < j)
  501. list.set(i++, list.get(j));
  502. while (i < j && pivot.compareTo(list.get(i)) >= 0)
  503. i++;
  504. if (i < j)
  505. list.set(j--, list.get(i));
  506. }
  507. list.set(i, pivot);
  508. return i;
  509. }
  510. return -1;
  511. }
  512. }