清理代码&格式化

This commit is contained in:
ZZ 2021-07-15 16:54:11 +08:00
parent 412ba3f6db
commit 0135134fd0
3 changed files with 34 additions and 49 deletions

View File

@ -76,7 +76,6 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
String version = ChargingPileServer.getVersion(handler.getName()); String version = ChargingPileServer.getVersion(handler.getName());
String poleNo = ChargingPileServer.getPoleNo(handler); String poleNo = ChargingPileServer.getPoleNo(handler);
Map<String, Object> req = analysis(data, serviceName, version); Map<String, Object> req = analysis(data, serviceName, version);
// log.info("pole [{}] status: |{}|", StringUtils.isNotBlank(poleNo) ? poleNo : "null", req.get("status") != null ? MQPole.PoleStatus.toString((Long) req.get("status")) : "N/A");
ServiceParameter sp = new ServiceParameter(serviceName, poleNo, req); ServiceParameter sp = new ServiceParameter(serviceName, poleNo, req);
ServiceResult result = servicemainLogic.process(sp); ServiceResult result = servicemainLogic.process(sp);
handler.sendClientBinary(result.getBinary()); handler.sendClientBinary(result.getBinary());

View File

@ -25,6 +25,7 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
* 取得存储在静态变量中的ApplicationContext. * 取得存储在静态变量中的ApplicationContext.
*/ */
public static ApplicationContext getApplicationContext() { public static ApplicationContext getApplicationContext() {
assertContextInjected(); assertContextInjected();
return applicationContext; return applicationContext;
} }
@ -34,6 +35,7 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T getBean(String name) { public static <T> T getBean(String name) {
assertContextInjected(); assertContextInjected();
return (T) applicationContext.getBean(name); return (T) applicationContext.getBean(name);
} }
@ -42,6 +44,7 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/ */
public static <T> T getBean(Class<T> requiredType) { public static <T> T getBean(Class<T> requiredType) {
assertContextInjected(); assertContextInjected();
return applicationContext.getBean(requiredType); return applicationContext.getBean(requiredType);
} }
@ -50,6 +53,7 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
* 清除SpringContextHolder中的ApplicationContext为Null. * 清除SpringContextHolder中的ApplicationContext为Null.
*/ */
public static void clearHolder() { public static void clearHolder() {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext); logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
} }
@ -62,6 +66,7 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
*/ */
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) { public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext; SpringContextHolder.applicationContext = applicationContext;
} }
@ -71,6 +76,7 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
*/ */
@Override @Override
public void destroy() throws Exception { public void destroy() throws Exception {
SpringContextHolder.clearHolder(); SpringContextHolder.clearHolder();
} }
@ -78,6 +84,8 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
* 检查ApplicationContext不为空. * 检查ApplicationContext不为空.
*/ */
private static void assertContextInjected() { private static void assertContextInjected() {
Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder."); Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
} }
} }

View File

@ -2,31 +2,32 @@ package com.xhpc.pp.utils.security;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import java.util.Objects;
public class HexUtils { public class HexUtils {
/** /**
* 字节数据转字符串专用集合 * 字节数据转字符串专用集合
*/ */
private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
'F'};
public static String toHex(byte[] data) { public static String toHex(byte[] data) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < data.length; i++) {
// 取出字节的高四位 作为索引得到相应的十六进制标识符 注意无符号右移
stringBuilder.append(HEX_CHAR[(data[i] & 0xf0) >>> 4]);
// 取出字节的低四位 作为索引得到相应的十六进制标识符
stringBuilder.append(HEX_CHAR[(data[i] & 0x0f)]);
}
StringBuilder stringBuilder = new StringBuilder();
for (byte datum : data) {
// 取出字节的高四位 作为索引得到相应的十六进制标识符 注意无符号右移
stringBuilder.append(HEX_CHAR[(datum & 0xf0) >>> 4]);
// 取出字节的低四位 作为索引得到相应的十六进制标识符
stringBuilder.append(HEX_CHAR[(datum & 0x0f)]);
}
return stringBuilder.toString(); return stringBuilder.toString();
} }
public static String toHex(byte[] data, int start, int end) { public static String toHex(byte[] data, int start, int end) {
if (data == null || data.length < end) if (data == null || data.length < end)
return ""; return "";
return toHex(ArrayUtils.subarray(data, start, end)); return toHex(ArrayUtils.subarray(data, start, end));
} }
@ -35,111 +36,102 @@ public class HexUtils {
} }
public static String toString(byte[] data, int start, int end) { public static String toString(byte[] data, int start, int end) {
if (data == null || data.length < end) if (data == null || data.length < end)
return ""; return "";
return toString(ArrayUtils.subarray(data, start, end)); return toString(ArrayUtils.subarray(data, start, end));
} }
public static int toInteger(byte[] data, int start, int end) { public static int toInteger(byte[] data, int start, int end) {
if (data == null || data.length < end) if (data == null || data.length < end)
return 0; return 0;
return Integer.decode("0x" + toHex(data, start, end)); return Integer.decode("0x" + toHex(data, start, end));
} }
public static byte[] toBytes(String hex) { public static byte[] toBytes(String hex) {
hex = hex.trim(); hex = hex.trim();
if (hex == null || hex.length() == 0 || hex.length() % 2 != 0) if (hex.length() == 0 || hex.length() % 2 != 0)
return null; return null;
byte[] b = new byte[hex.length() / 2]; byte[] b = new byte[hex.length() / 2];
for (int i = 0; i < hex.length(); i = i + 2) { for (int i = 0; i < hex.length(); i = i + 2) {
b[i / 2] = Integer.decode("0x" + hex.substring(i, i + 2)).byteValue(); b[i / 2] = Integer.decode("0x" + hex.substring(i, i + 2)).byteValue();
} }
return b; return b;
} }
public static byte[] stringToBytes(String str, int len) { public static byte[] stringToBytes(String str, int len) {
byte[] data = str.getBytes(); byte[] data = str.getBytes();
if (data.length > len) if (data.length > len)
return data; return data;
for (int i = data.length; i < len; i++) for (int i = data.length; i < len; i++)
data = ArrayUtils.add(data, (byte) 0); data = ArrayUtils.add(data, (byte) 0);
return data; return data;
} }
public static String toIntString(String hex) { public static String toIntString(String hex) {
return toIntString(hex, 4); return toIntString(hex, 4);
} }
public static String toIntString(String hex, int len) { public static String toIntString(String hex, int len) {
if (hex == null || hex.length() == 0 || hex.length() % len != 0) if (hex == null || hex.length() == 0 || hex.length() % len != 0)
return null; return null;
StringBuilder sb = new StringBuilder();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < hex.length(); i = i + len) { for (int i = 0; i < hex.length(); i = i + len) {
sb.append(String.format("%0" + len + "d", byte2int(toBytes(hex.substring(i, i + len))))); sb.append(String.format("%0" + len + "d", byte2int(Objects.requireNonNull(toBytes(hex.substring(i, i + len))))));
} }
return sb.toString(); return sb.toString();
} }
public static byte[] toIntBytes(String num, int len) { public static byte[] toIntBytes(String num, int len) {
if (num == null || num.length() == 0 || num.length() % len != 0) if (num == null || num.length() == 0 || num.length() % len != 0)
return null; return null;
byte[] result = new byte[0]; byte[] result = new byte[0];
for (int i = 0; i < num.length(); i = i + len) { for (int i = 0; i < num.length(); i = i + len) {
int n = Integer.parseInt(num.substring(i, i + len)); int n = Integer.parseInt(num.substring(i, i + len));
result = ArrayUtils.addAll(result, toIntBytes(n, len / 2)); result = ArrayUtils.addAll(result, toIntBytes(n, len / 2));
} }
return result; return result;
} }
public static byte[] toIntBytes(int n, int len) { public static byte[] toIntBytes(int n, int len) {
byte[] b = new byte[len];
byte[] b = new byte[len];
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
b[i] = (byte) (n >> i * 8 & 0xff); b[i] = (byte) (n >> i * 8 & 0xff);
} }
return b; return b;
} }
public static int byte2int(byte[] b) { public static int byte2int(byte[] b) {
int res = 0; int res = 0;
int bLen = b.length; int bLen = b.length;
if (bLen < 5) {// int 最大到4个字节 if (bLen < 5) {// int 最大到4个字节
for (int i = 0; i < bLen; i++) { for (int i = 0; i < bLen; i++) {
res += (b[i] & 0xFF) << (8 * i); res += (b[i] & 0xFF) << (8 * i);
} }
} }
return res; return res;
} }
public static byte[] toLongBytes(long n) { public static byte[] toLongBytes(long n) {
byte[] b = new byte[4];
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
b[i] = (byte) ((n >> i * 8) & 0xff); b[i] = (byte) ((n >> i * 8) & 0xff);
} }
return b; return b;
} }
// 使用小端 字节index 0 对应 int 的最后一个字节 // 使用小端 字节index 0 对应 int 的最后一个字节
public static long byte2Long(byte[] b) { public static long byte2Long(byte[] b) {
long res = 0; long res = 0;
int bLen = b.length; int bLen = b.length;
// int 最大到4个字节 // int 最大到4个字节
@ -148,21 +140,7 @@ public class HexUtils {
res += (b[i] & 0xFFL) << 8 * i; res += (b[i] & 0xFFL) << 8 * i;
} }
} }
return res; return res;
} }
public static void main(String[] args) {
System.out.println(toIntString("E80373000E02E903", 4));
System.out.println(toHex(toIntBytes("1000011505261001", 4)));
System.out.println(toIntBytes("0016537797330272", 4));
// System.out.println(toIntString("32FFD505484B323441260443AAAAAAAA",8));
System.out.println(toHex(toLongBytes(1000000L)));
System.out.println(byte2Long(toBytes("20090080")));
System.out.println(0x80 & 0xff);
System.out.println(String.format("%032d", 123456));
}
} }