添加大数据加密指令
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package com.sunyard.entity;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class MutiReturn7 {
|
||||
// 返回码
|
||||
private int ret;
|
||||
// 0-不是最后 1-是最后
|
||||
private int isLast;
|
||||
// 数据
|
||||
private ByteBuffer data;
|
||||
|
||||
public MutiReturn7(int ret, int isLast, ByteBuffer data) {
|
||||
this.ret = ret;
|
||||
this.isLast = isLast;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public int getRet() {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void setRet(int ret) {
|
||||
this.ret = ret;
|
||||
}
|
||||
|
||||
public int getIsLast() {
|
||||
return isLast;
|
||||
}
|
||||
|
||||
public void setIsLast(int isLast) {
|
||||
this.isLast = isLast;
|
||||
}
|
||||
|
||||
public ByteBuffer getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(ByteBuffer data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.sunyard.util;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class ByteBufferUtil {
|
||||
public static ByteBuffer sliceRange(ByteBuffer source, int start, int length) {
|
||||
// 保存原 buffer 的 position 和 limit
|
||||
int oldPos = source.position();
|
||||
int oldLimit = source.limit();
|
||||
|
||||
// 设置新的 position 和 limit 为目标区间
|
||||
source.position(start);
|
||||
source.limit(start + length);
|
||||
|
||||
// 创建共享视图(零拷贝)
|
||||
ByteBuffer slice = source.slice();
|
||||
|
||||
// 恢复原 buffer 的 position 和 limit
|
||||
source.position(oldPos);
|
||||
source.limit(oldLimit);
|
||||
|
||||
return slice;
|
||||
}
|
||||
|
||||
public static ByteBuffer sliceAndConsume(ByteBuffer source, int length) {
|
||||
// 保存原 buffer 的 position 和 limit
|
||||
int oldPos = source.position();
|
||||
int oldLimit = source.limit();
|
||||
|
||||
// 设置新的 position 和 limit 为目标区间
|
||||
int start = oldPos;
|
||||
source.position(start);
|
||||
source.limit(start + length);
|
||||
|
||||
// 创建共享视图(零拷贝)
|
||||
ByteBuffer slice = source.slice();
|
||||
|
||||
// 恢复原 buffer 的 position 和 limit
|
||||
source.position(oldPos + length);
|
||||
source.limit(oldLimit);
|
||||
|
||||
return slice;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,367 @@
|
||||
package com.sunyard.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 参数检查工具类,提供常见的参数校验方法。
|
||||
* <p>
|
||||
* 所有方法在校验失败时均抛出 {@link IllegalArgumentException} 异常,
|
||||
* 并附带包含参数名的明确错误信息。成功时返回被校验的值,便于链式调用。
|
||||
* </p>
|
||||
*
|
||||
* <p>使用示例:
|
||||
* <pre>{@code
|
||||
* public void setAge(int age) {
|
||||
* ParamChecker.checkInRange(age, "age", 0, 150);
|
||||
* }
|
||||
*
|
||||
* public void setName(String name) {
|
||||
* ParamChecker.checkNotBlank(name, "name");
|
||||
* }
|
||||
*
|
||||
* public void setStatus(int status) {
|
||||
* ParamChecker.checkInValues(status, "status", 1, 2, 3);
|
||||
* }
|
||||
* }</pre>
|
||||
* </p>
|
||||
*
|
||||
* @author Cheney
|
||||
*/
|
||||
public final class ParamChecker {
|
||||
|
||||
private ParamChecker() {
|
||||
// 私有构造器,防止实例化
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查表达式是否为真,若为假则抛出异常。
|
||||
*
|
||||
* @param expression 待检查的布尔表达式
|
||||
* @param message 异常信息
|
||||
* @throws IllegalArgumentException 如果 expression 为 false
|
||||
*/
|
||||
public static void checkArgument(boolean expression, String message) {
|
||||
if (!expression) {
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 对象非空检查 ====================
|
||||
|
||||
/**
|
||||
* 检查对象不为 null。
|
||||
*
|
||||
* @param obj 待检查的对象
|
||||
* @param paramName 参数名称(用于异常信息)
|
||||
* @param <T> 对象类型
|
||||
* @return 传入的对象(便于链式调用)
|
||||
* @throws IllegalArgumentException 如果 obj 为 null
|
||||
*/
|
||||
public static <T> T checkNotNull(T obj, String paramName) {
|
||||
if (obj == null) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null");
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
// ==================== 字符串检查 ====================
|
||||
|
||||
/**
|
||||
* 检查字符串不为 null 且长度大于 0(不忽略空白字符)。
|
||||
*
|
||||
* @param str 待检查的字符串
|
||||
* @param paramName 参数名称
|
||||
* @return 传入的字符串
|
||||
* @throws IllegalArgumentException 如果 str 为 null 或空字符串
|
||||
*/
|
||||
public static String checkNotEmpty(String str, String paramName) {
|
||||
if (str == null || str.isEmpty()) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空字符串");
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字符串不为 null 且去除首尾空白后长度大于 0。
|
||||
*
|
||||
* @param str 待检查的字符串
|
||||
* @param paramName 参数名称
|
||||
* @return 传入的字符串(原值,非 trim 后结果)
|
||||
* @throws IllegalArgumentException 如果 str 为 null 或仅包含空白字符
|
||||
*/
|
||||
public static String checkNotBlank(String str, String paramName) {
|
||||
if (str == null || str.isBlank()) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null、空字符串或仅包含空白字符");
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字符串长度在指定范围内(包含边界)。
|
||||
*
|
||||
* @param str 待检查的字符串
|
||||
* @param paramName 参数名称
|
||||
* @param min 最小长度(包含)
|
||||
* @param max 最大长度(包含)
|
||||
* @return 传入的字符串
|
||||
* @throws IllegalArgumentException 如果 str 为 null,或长度不在 [min, max] 范围内
|
||||
*/
|
||||
public static String checkLengthBetween(String str, String paramName, int min, int max) {
|
||||
checkNotNull(str, paramName);
|
||||
if (min > max) {
|
||||
throw new IllegalArgumentException("内部错误:长度检查的最小值 " + min + " 不能大于最大值 " + max);
|
||||
}
|
||||
int len = str.length();
|
||||
if (len < min || len > max) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("参数 '%s' 的长度 %d 不在允许范围 [%d, %d] 内", paramName, len, min, max));
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
// ==================== 整数(int)检查 ====================
|
||||
|
||||
/**
|
||||
* 检查 int 值大于 0。
|
||||
*
|
||||
* @param value 待检查的值
|
||||
* @param paramName 参数名称
|
||||
* @return 传入的值
|
||||
* @throws IllegalArgumentException 如果 value <= 0
|
||||
*/
|
||||
public static int checkPositive(int value, String paramName) {
|
||||
if (value <= 0) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 必须为正数,当前值: " + value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 int 值 >= 0。
|
||||
*
|
||||
* @param value 待检查的值
|
||||
* @param paramName 参数名称
|
||||
* @return 传入的值
|
||||
* @throws IllegalArgumentException 如果 value < 0
|
||||
*/
|
||||
public static int checkNonNegative(int value, String paramName) {
|
||||
if (value < 0) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为负数,当前值: " + value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 int 值在指定范围内(包含边界)。
|
||||
*
|
||||
* @param value 待检查的值
|
||||
* @param paramName 参数名称
|
||||
* @param min 最小值(包含)
|
||||
* @param max 最大值(包含)
|
||||
* @return 传入的值
|
||||
* @throws IllegalArgumentException 如果 value 不在 [min, max] 范围内,或 min > max
|
||||
*/
|
||||
public static int checkInRange(int value, String paramName, int min, int max) {
|
||||
if (min > max) {
|
||||
throw new IllegalArgumentException("内部错误:范围检查的最小值 " + min + " 不能大于最大值 " + max);
|
||||
}
|
||||
if (value < min || value > max) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("参数 '%s' 的值 %d 不在允许范围 [%d, %d] 内", paramName, value, min, max));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 int 值是否等于给定集合中的任意一个值。
|
||||
*
|
||||
* @param value 待检查的值
|
||||
* @param paramName 参数名称
|
||||
* @param validValues 允许的值列表
|
||||
* @return 传入的值
|
||||
* @throws IllegalArgumentException 如果 value 不在 validValues 中,或 validValues 为空
|
||||
*/
|
||||
public static int checkInValues(int value, String paramName, int... validValues) {
|
||||
if (validValues == null || validValues.length == 0) {
|
||||
throw new IllegalArgumentException("内部错误:允许的值集合不能为空");
|
||||
}
|
||||
for (int v : validValues) {
|
||||
if (value == v) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
// 构建允许值字符串用于提示
|
||||
StringBuilder allowed = new StringBuilder();
|
||||
for (int i = 0; i < validValues.length; i++) {
|
||||
if (i > 0) allowed.append(", ");
|
||||
allowed.append(validValues[i]);
|
||||
}
|
||||
throw new IllegalArgumentException(
|
||||
String.format("参数 '%s' 的值 %d 不在允许值集合 [%s] 中", paramName, value, allowed));
|
||||
}
|
||||
|
||||
// ==================== 长整型(long)检查 ====================
|
||||
|
||||
/**
|
||||
* 检查 long 值大于 0。
|
||||
*
|
||||
* @param value 待检查的值
|
||||
* @param paramName 参数名称
|
||||
* @return 传入的值
|
||||
* @throws IllegalArgumentException 如果 value <= 0
|
||||
*/
|
||||
public static long checkPositive(long value, String paramName) {
|
||||
if (value <= 0) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 必须为正数,当前值: " + value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 long 值 >= 0。
|
||||
*
|
||||
* @param value 待检查的值
|
||||
* @param paramName 参数名称
|
||||
* @return 传入的值
|
||||
* @throws IllegalArgumentException 如果 value < 0
|
||||
*/
|
||||
public static long checkNonNegative(long value, String paramName) {
|
||||
if (value < 0) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为负数,当前值: " + value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 long 值在指定范围内(包含边界)。
|
||||
*
|
||||
* @param value 待检查的值
|
||||
* @param paramName 参数名称
|
||||
* @param min 最小值(包含)
|
||||
* @param max 最大值(包含)
|
||||
* @return 传入的值
|
||||
* @throws IllegalArgumentException 如果 value 不在 [min, max] 范围内,或 min > max
|
||||
*/
|
||||
public static long checkInRange(long value, String paramName, long min, long max) {
|
||||
if (min > max) {
|
||||
throw new IllegalArgumentException("内部错误:范围检查的最小值 " + min + " 不能大于最大值 " + max);
|
||||
}
|
||||
if (value < min || value > max) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("参数 '%s' 的值 %d 不在允许范围 [%d, %d] 内", paramName, value, min, max));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// ==================== 浮点数(double)简单检查 ====================
|
||||
|
||||
/**
|
||||
* 检查 double 值大于 0(考虑极小正数,不做精度处理)。
|
||||
*
|
||||
* @param value 待检查的值
|
||||
* @param paramName 参数名称
|
||||
* @return 传入的值
|
||||
* @throws IllegalArgumentException 如果 value <= 0 或为 NaN
|
||||
*/
|
||||
public static double checkPositive(double value, String paramName) {
|
||||
if (value <= 0 || Double.isNaN(value)) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 必须为正数,当前值: " + value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 double 值 >= 0。
|
||||
*
|
||||
* @param value 待检查的值
|
||||
* @param paramName 参数名称
|
||||
* @return 传入的值
|
||||
* @throws IllegalArgumentException 如果 value < 0 或为 NaN
|
||||
*/
|
||||
public static double checkNonNegative(double value, String paramName) {
|
||||
if (value < 0 || Double.isNaN(value)) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为负数或 NaN,当前值: " + value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// ==================== 集合与数组非空检查 ====================
|
||||
|
||||
/**
|
||||
* 检查集合不为 null 且非空。
|
||||
*
|
||||
* @param coll 待检查的集合
|
||||
* @param paramName 参数名称
|
||||
* @param <T> 集合元素类型
|
||||
* @return 传入的集合
|
||||
* @throws IllegalArgumentException 如果 coll 为 null 或空集合
|
||||
*/
|
||||
public static <T> Collection<T> checkNotEmpty(Collection<T> coll, String paramName) {
|
||||
if (coll == null || coll.isEmpty()) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空集合");
|
||||
}
|
||||
return coll;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 Map 不为 null 且非空。
|
||||
*
|
||||
* @param map 待检查的 Map
|
||||
* @param paramName 参数名称
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @return 传入的 Map
|
||||
* @throws IllegalArgumentException 如果 map 为 null 或空 Map
|
||||
*/
|
||||
public static <K, V> Map<K, V> checkNotEmpty(Map<K, V> map, String paramName) {
|
||||
if (map == null || map.isEmpty()) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空 Map");
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查数组不为 null 且长度大于 0。
|
||||
*
|
||||
* @param array 待检查的数组
|
||||
* @param paramName 参数名称
|
||||
* @param <T> 数组元素类型
|
||||
* @return 传入的数组
|
||||
* @throws IllegalArgumentException 如果 array 为 null 或长度为 0
|
||||
*/
|
||||
public static <T> T[] checkNotEmpty(T[] array, String paramName) {
|
||||
if (array == null || array.length == 0) {
|
||||
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空数组");
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
// ==================== 布尔条件检查 ====================
|
||||
|
||||
/**
|
||||
* 检查布尔条件为 true。
|
||||
*
|
||||
* @param condition 待检查的条件
|
||||
* @param paramName 参数名称(描述条件含义)
|
||||
* @throws IllegalArgumentException 如果 condition 为 false
|
||||
*/
|
||||
public static void checkTrue(boolean condition, String paramName) {
|
||||
if (!condition) {
|
||||
throw new IllegalArgumentException("参数条件 '" + paramName + "' 必须为 true");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查布尔条件为 false。
|
||||
*
|
||||
* @param condition 待检查的条件
|
||||
* @param paramName 参数名称(描述条件含义)
|
||||
* @throws IllegalArgumentException 如果 condition 为 true
|
||||
*/
|
||||
public static void checkFalse(boolean condition, String paramName) {
|
||||
if (condition) {
|
||||
throw new IllegalArgumentException("参数条件 '" + paramName + "' 必须为 false");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user