package com.xgong.webframe.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
public class Util
{
public static String getNow(String format)
{
long ts = System.currentTimeMillis();
return Date2String(format, ts);
}
/**
* @deprecated
*/
public static String getTime(String format, long ts)
{
return Date2String(format, ts);
}
/**
* @deprecated
*/
public static Date getFormatDate(String format, String date)
{
return String2Date(format, date);
}
public static String Date2String(String format, long ts)
{
Date dt = new Date(ts);
SimpleDateFormat sf = new SimpleDateFormat(format);
String now = sf.format(dt);
return now;
}
public static Date String2Date(String format, String date)
{
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date res = null;
try {
res = formatter.parse(date);
} catch (ParseException ex1) {
ex1.printStackTrace();
}
return res;
}
public static String replaceAllString(String source, String search, String replace)
{
StringBuffer sb = new StringBuffer(source);
while (sb.indexOf(search) != -1) {
int pos = sb.indexOf(search);
sb.replace(pos, pos + search.length(), replace);
}
return sb.toString();
}
public static boolean isNumber(String s)
{
if (s == null)
return false;
try
{
Long.parseLong(s.trim());
return true; } catch (NumberFormatException numberformatexception) {
}
return false;
}
public static boolean isEmpty(Object obj)
{
if (obj == null) return true;
if (obj instanceof String)
return "".equals(obj);
if (obj instanceof StringBuffer)
return (((StringBuffer)obj).length() > 0);
if (obj instanceof Set)
return ((Set)obj).isEmpty();
if (obj instanceof Map)
return ((Map)obj).isEmpty();
if (obj instanceof List)
return ((List)obj).isEmpty();
try
{
return ((Boolean)obj.getClass().getMethod("isEmpty", new Class[0]).invoke(obj, new Object[0])).booleanValue();
} catch (Exception ex) {
}
return false;
}
public static boolean isEqual(Object obj1, Object obj2)
{
if ((obj1 == null) && (obj2 == null))
return true;
if ((obj1 != null) && (obj2 != null))
return obj1.equals(obj2);
return false;
}
public static String htmlEncoding(String source)
{
if (source == null)
return null;
return source.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("'", "'").replaceAll("\"", ""e;").replaceAll(" ", " ").replaceAll("\n", "<br>");
}
public static String htmlDecoding(String source)
{
if (source == null)
return null;
return source.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("'", "'").replaceAll(""e;", "\"").replaceAll(" ", " ");
}
public static String urlEncode(String str)
{
if (str == null)
return null;
StringBuffer tmp = new StringBuffer();
for (int i = 0; i < str.length(); ++i) {
char a = str.charAt(i);
if (((a < ':') && (a > '/')) || ((a < '[') && (a > '@')) || ((a < '{') && (a > '`')) || (a == '_'))
tmp.append(a);
else if (a < '\16')
tmp.append("%0" + Integer.toHexString(a));
else
tmp.append("%" + Integer.toHexString(a));
}
return tmp.toString();
}
public static String urlDecode(String str)
{
if (str == null)
return null;
StringBuffer tmp = new StringBuffer();
for (int i = 0; i < str.length(); ++i) {
char a = str.charAt(i);
if (a != '%') {
tmp.append(a);
} else {
char tmps = (char)Integer.valueOf(str.substring(i + 1, i + 3), 16).intValue();
i += 2;
tmp.append(tmps);
}
}
return tmp.toString();
}
public static String toByteString(String source)
{
byte[] bytes = source.getBytes();
StringBuffer buf = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; ++i) {
if ((bytes[i] & 0xFF) < 16)
buf.append("0");
buf.append(Long.toString(bytes[i] & 0xFF, 16));
}
return buf.toString();
}
public static long inetAtoN(String ip)
{
long result = 65177412912021504L;
try {
StringTokenizer st = new StringTokenizer(ip, ".");
result += (Long.parseLong(st.nextToken()) << 24);
result += (Long.parseLong(st.nextToken()) << 16);
result += (Long.parseLong(st.nextToken()) << 8);
result += Long.parseLong(st.nextToken());
} catch (Exception e) {
return 65178271905480704L;
}
return result;
}
public static String inetNtoA(long ip)
{
StringBuffer result = new StringBuffer(16);
result.append(Long.toString(ip >> 24)).append(".");
result.append(Long.toString((ip & 0xFFFFFF) >> 16)).append(".");
result.append(Long.toString((ip & 0xFFFF) >> 8)).append(".");
result.append(Long.toString(ip & 0xFF));
return new String(result);
}
public static byte[] Int162Array(int sour) {
byte[] dest = { (byte)((sour & 0xFF00) >> 8), (byte)(sour & 0xFF) };
return dest;
}
public static byte[] Int322Array(int sour) {
byte[] dest = { (byte)((sour & 0xFF000000) >> 24), (byte)((sour & 0xFF0000) >> 16), (byte)((sour & 0xFF00) >> 8), (byte)(sour & 0xFF) };
return dest;
}
public static int Array2Int16(byte[] sour, int offset) {
return (sour[offset] << 8 & 0xFF00 | sour[(offset + 1)] & 0xFF);
}
public static int Array2Int32(byte[] sour, int offset) {
return (sour[offset] << 24 & 0xFF000000 | sour[(offset + 1)] << 16 & 0xFF0000 | sour[(offset + 2)] << 8 & 0xFF00 | sour[(offset + 3)] & 0xFF);
}
public static String Int322Ip(int ip) {
StringBuffer sb = new StringBuffer();
sb.append(Integer.toString(ip >> 24 & 0xFF)).append('.');
sb.append(Integer.toString(ip >> 16 & 0xFF)).append('.');
sb.append(Integer.toString(ip >> 8 & 0xFF)).append('.');
sb.append(Integer.toString(ip & 0xFF));
return new String(sb);
}
public static int Ip2Int32(String ip) {
String[] sp = ip.split("\\.");
int dest = Integer.parseInt(sp[0]) << 24;
dest |= Integer.parseInt(sp[1]) << 16;
dest |= Integer.parseInt(sp[2]) << 8;
dest |= Integer.parseInt(sp[3]);
return dest;
}
public static <K, V> Map<K, V> String2Map(String str, String delim, String pair, Class<K> key, Class<V> value, V def, boolean urlDecode)
throws NoSuchMethodException
{
if (str == null) return null;
Map ret = new HashMap();
Constructor kc = key.getConstructor(new Class[] { String.class });
Constructor vc = value.getConstructor(new Class[] { String.class });
StringTokenizer st = new StringTokenizer(str, delim);
while (st.hasMoreTokens()) {
String kvp = st.nextToken();
int pos = kvp.indexOf(pair);
String k = (pos > 0) ? kvp.substring(0, pos) : kvp;
String v = (pos > 0) ? kvp.substring(pos + pair.length()) : null;
if (urlDecode == true) {
k = urlDecode(k);
v = urlDecode(v);
}
try {
ret.put(kc.newInstance(new Object[] { k }), (v == null) ? def : vc.newInstance(new Object[] { v }));
} catch (Exception ex) {
}
}
return ret;
}
public static <K, V> String Map2String(Map<K, V> map, String delim, String pair, boolean encode)
{
i