From 0135134fd03d3d8d6ab1190c1476608f73d8dac9 Mon Sep 17 00:00:00 2001 From: ZZ Date: Thu, 15 Jul 2021 16:54:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86=E4=BB=A3=E7=A0=81&=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pp/server/ChargingPileBinaryHandler.java | 1 - .../xhpc/pp/utils/SpringContextHolder.java | 10 ++- .../com/xhpc/pp/utils/security/HexUtils.java | 72 +++++++------------ 3 files changed, 34 insertions(+), 49 deletions(-) diff --git a/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java b/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java index 5ca88464..5f88f4db 100644 --- a/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java +++ b/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java @@ -76,7 +76,6 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { String version = ChargingPileServer.getVersion(handler.getName()); String poleNo = ChargingPileServer.getPoleNo(handler); Map 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); ServiceResult result = servicemainLogic.process(sp); handler.sendClientBinary(result.getBinary()); diff --git a/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/utils/SpringContextHolder.java b/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/utils/SpringContextHolder.java index 3aca1536..47b7877d 100644 --- a/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/utils/SpringContextHolder.java +++ b/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/utils/SpringContextHolder.java @@ -25,6 +25,7 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { + assertContextInjected(); return applicationContext; } @@ -34,6 +35,7 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB */ @SuppressWarnings("unchecked") public static T getBean(String name) { + assertContextInjected(); return (T) applicationContext.getBean(name); } @@ -42,6 +44,7 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. */ public static T getBean(Class requiredType) { + assertContextInjected(); return applicationContext.getBean(requiredType); } @@ -50,7 +53,8 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB * 清除SpringContextHolder中的ApplicationContext为Null. */ public static void clearHolder() { - if (logger.isDebugEnabled()){ + + if (logger.isDebugEnabled()) { logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext); } applicationContext = null; @@ -62,6 +66,7 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB */ @Override public void setApplicationContext(ApplicationContext applicationContext) { + SpringContextHolder.applicationContext = applicationContext; } @@ -71,6 +76,7 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB */ @Override public void destroy() throws Exception { + SpringContextHolder.clearHolder(); } @@ -78,6 +84,8 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB * 检查ApplicationContext不为空. */ private static void assertContextInjected() { + Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder."); } + } diff --git a/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/utils/security/HexUtils.java b/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/utils/security/HexUtils.java index 4fd993f2..58c1805b 100644 --- a/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/utils/security/HexUtils.java +++ b/xhpc-modules/xhpc-power-pole/src/main/java/com/xhpc/pp/utils/security/HexUtils.java @@ -2,31 +2,32 @@ package com.xhpc.pp.utils.security; import org.apache.commons.lang3.ArrayUtils; +import java.util.Objects; + public class HexUtils { /** * 字节数据转字符串专用集合 */ - private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', - 'F'}; + private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 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(); } public static String toHex(byte[] data, int start, int end) { + if (data == null || data.length < end) return ""; - return toHex(ArrayUtils.subarray(data, start, end)); } @@ -35,134 +36,111 @@ public class HexUtils { } public static String toString(byte[] data, int start, int end) { + if (data == null || data.length < end) return ""; - return toString(ArrayUtils.subarray(data, start, end)); } public static int toInteger(byte[] data, int start, int end) { + if (data == null || data.length < end) return 0; - return Integer.decode("0x" + toHex(data, start, end)); } public static byte[] toBytes(String hex) { + hex = hex.trim(); - if (hex == null || hex.length() == 0 || hex.length() % 2 != 0) + if (hex.length() == 0 || hex.length() % 2 != 0) return null; - byte[] b = new byte[hex.length() / 2]; - for (int i = 0; i < hex.length(); i = i + 2) { b[i / 2] = Integer.decode("0x" + hex.substring(i, i + 2)).byteValue(); } - return b; } public static byte[] stringToBytes(String str, int len) { + byte[] data = str.getBytes(); if (data.length > len) return data; - for (int i = data.length; i < len; i++) data = ArrayUtils.add(data, (byte) 0); - return data; } - public static String toIntString(String hex) { return toIntString(hex, 4); } public static String toIntString(String hex, int len) { + if (hex == null || hex.length() == 0 || hex.length() % len != 0) return null; - - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); 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(); } public static byte[] toIntBytes(String num, int len) { + if (num == null || num.length() == 0 || num.length() % len != 0) return null; - byte[] result = new byte[0]; - for (int i = 0; i < num.length(); i = i + len) { int n = Integer.parseInt(num.substring(i, i + len)); result = ArrayUtils.addAll(result, toIntBytes(n, len / 2)); } - return result; } 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++) { b[i] = (byte) (n >> i * 8 & 0xff); } return b; - } public static int byte2int(byte[] b) { + int res = 0; int bLen = b.length; - if (bLen < 5) {// int 最大到4个字节 for (int i = 0; i < bLen; i++) { res += (b[i] & 0xFF) << (8 * i); } } - return res; } public static byte[] toLongBytes(long n) { - byte[] b = new byte[4]; + byte[] b = new byte[4]; for (int i = 0; i < 4; i++) { b[i] = (byte) ((n >> i * 8) & 0xff); } return b; - } // 使用小端 字节index 0 对应 int 的最后一个字节 public static long byte2Long(byte[] b) { + long res = 0; int bLen = b.length; -// int 最大到4个字节 + // int 最大到4个字节 if (bLen < 9) { for (int i = 0; i < bLen; i++) { res += (b[i] & 0xFFL) << 8 * i; } } - 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)); - } - - }