All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.founder.mip.utils.CommonUtil Maven / Gradle / Ivy
package com.founder.mip.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.founder.core.log.MyLog;
import jdk.nashorn.internal.objects.NativeString;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class CommonUtil {
private static final MyLog LOG = MyLog.getLog(CommonUtil.class);
public CommonUtil() {
}
public static SortedMap objectToMap(Object object) {
if (object == null) {
return null;
} else {
Class clazz = object.getClass();
ArrayList fieldList;
for (fieldList = new ArrayList(); clazz != null; clazz = clazz.getSuperclass()) {
fieldList.addAll(new ArrayList(Arrays.asList(clazz.getDeclaredFields())));
}
SortedMap res = new TreeMap();
Iterator var4 = fieldList.iterator();
while (var4.hasNext()) {
Field field = (Field) var4.next();
field.setAccessible(true);
try {
if (field.get(object) instanceof List) {
res.put(field.getName(), JSON.toJSON(field.get(object)));
} else {
res.put(field.getName(), field.get(object));
}
} catch (IllegalAccessException var7) {
LOG.error("get obj field error", var7);
}
}
return res;
}
}
private static String getParamsFromMap(SortedMap map) {
StringBuilder sb = new StringBuilder();
for (String key : map.keySet()) {
Object val = map.get(key);
if ((null != val) && (!Objects.equals(key, "signData")) && (!Objects.equals(key, "encData")) && (!Objects.equals(key, "extra"))) {
String value;
if (isBaseDataType(val.getClass())) {
value = val.toString();
} else {
value = JSON.toJSONString(val, new SerializerFeature[]{SerializerFeature.MapSortField});
}
if (!value.trim().equals("")) {
if (sb.length() > 0) {
sb.append("&");
}
sb.append(key);
sb.append("=");
sb.append(value);
}
}
}
return sb.toString();
}
// private static String getParamsFromMap(SortedMap map) {
// StringBuilder sb = new StringBuilder();
// Iterator var2 = map.keySet().iterator();
//
// while (var2.hasNext()) {
// String key = (String) var2.next();
// Object val = map.get(key);
// if (null != val && !Objects.equals(key, "signData") && !Objects.equals(key, "encData") && !Objects.equals(key, "extra")) {
// String value;
// if (isBaseDataType(val.getClass())) {
// value = val.toString();
// } else {
// value = JSON.toJSONString(val, new SerializerFeature[]{SerializerFeature.MapSortField});
// }
//
// if (!value.trim().equals("")) {
// if (sb.length() > 0) {
// sb.append("&");
// }
//
// sb.append(key);
// sb.append("=");
// sb.append(value);
// }
// }
// }
//
// return sb.toString();
// }
private static boolean isBaseDataType(Class clazz) {
return clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(BigDecimal.class) || clazz.equals(BigInteger.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class) || clazz.isPrimitive();
}
public static String getSignStr(String appSecret, Map param) {
SortedMap paramMap = new TreeMap(param);
String sortStr = getParamsFromMap(paramMap);
sortStr = sortStr + "&key=" + appSecret;
LOG.info("待签名字符串:" + sortStr);
return sortStr;
}
public static String stringToHexString(String s) {
String str = "";
for (int i = 0; i < s.length(); ++i) {
int ch = s.charAt(i);
String s4 = Integer.toHexString(ch);
str = str + s4;
}
return str;
}
public static String byteArrayToHex(byte[] byteArray) {
char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char[] resultCharArray = new char[byteArray.length * 2];
int index = 0;
byte[] var4 = byteArray;
int var5 = byteArray.length;
for (int var6 = 0; var6 < var5; ++var6) {
byte b = var4[var6];
resultCharArray[index++] = hexDigits[b >>> 4 & 15];
resultCharArray[index++] = hexDigits[b & 15];
}
return (new String(resultCharArray)).toUpperCase();
}
public static String byteToHex(byte[] b) {
if (b == null) {
throw new IllegalArgumentException("Argument b ( byte array ) is null! ");
} else {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; ++n) {
stmp = Integer.toHexString(b[n] & 255);
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
}
public static List getStrList(String inputString, int length, int size) {
List list = new ArrayList();
for (int index = 0; index < size; ++index) {
String childStr = NativeString.substring(inputString, index * length, (index + 1) * length);
list.add(childStr);
}
return list;
}
public static String valueToString(JSONObject jsonObj) {
if (jsonObj != null) {
Set set = new HashSet();
for (String key : jsonObj.keySet()) {
if ((jsonObj.get(key) != null)
&& (!Objects.equals(jsonObj.get(key), ""))
&& (!Objects.equals(jsonObj.get(key), null))) {
Object json = new JSONTokener(jsonObj.get(key).toString()).nextValue();
if ((json instanceof JSONObject)) {
valueToString(jsonObj.getJSONObject(key));
} else if ((json instanceof JSONArray)) {
for (Object object : jsonObj.getJSONArray(key)) {
valueToString((JSONObject) object);
}
} else {
jsonObj.put(key, jsonObj.get(key).toString());
}
} else {
set.add(key);
}
}
for (String setStr : set) {
jsonObj.remove(setStr);
}
}
return jsonObj.toString();
}
// public static String valueToString(JSONObject jsonObj) {
// if (jsonObj != null) {
// Set set = new HashSet();
// Iterator var2 = jsonObj.keySet().iterator();
//
// label51:
// while (true) {
// while (true) {
// String key;
// while (var2.hasNext()) {
// key = (String) var2.next();
// if (jsonObj.get(key) != null && !Objects.equals(jsonObj.get(key), "") && !Objects.equals(jsonObj.get(key), (Object) null)) {
// Object json = (new JSONTokener(jsonObj.get(key).toString())).nextValue();
// if (json instanceof JSONObject) {
// valueToString(jsonObj.getJSONObject(key));
// } else if (json instanceof JSONArray) {
// Iterator var5 = jsonObj.getJSONArray(key).iterator();
//
// while (var5.hasNext()) {
// Object object = var5.next();
// valueToString((JSONObject) object);
// }
// } else {
// jsonObj.put(key, jsonObj.get(key).toString());
// }
// } else {
// set.add(key);
// }
// }
//
// var2 = set.iterator();
//
// while (var2.hasNext()) {
// key = (String) var2.next();
// jsonObj.remove(key);
// }
// break label51;
// }
// }
// }
//
// return jsonObj.toString();
// }
}