PageRenderTime 80ms CodeModel.GetById 15ms app.highlight 60ms RepoModel.GetById 1ms app.codeStats 0ms

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