fix HexUtil

This commit is contained in:
Looly 2021-05-11 11:58:10 +08:00
parent 890307e123
commit ab79a7e2bb
4 changed files with 78 additions and 58 deletions

View File

@ -3,10 +3,11 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.6.6 (2021-05-10) # 5.6.6 (2021-05-11)
### 🐣新特性 ### 🐣新特性
* 【cron 】 增加时间轮简单实现 * 【cron 】 增加时间轮简单实现
* 【core 】 BeanUtil.copyToList增加重载pr#321@Gitee
### 🐞Bug修复 ### 🐞Bug修复

View File

@ -12,6 +12,7 @@ import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ClassUtil; import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ModifierUtil; import cn.hutool.core.util.ModifierUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
@ -709,10 +710,7 @@ public class BeanUtil {
* @param copyOptions 拷贝选项 {@link CopyOptions} * @param copyOptions 拷贝选项 {@link CopyOptions}
*/ */
public static void copyProperties(Object source, Object target, CopyOptions copyOptions) { public static void copyProperties(Object source, Object target, CopyOptions copyOptions) {
if (null == copyOptions) { BeanCopier.create(source, target, ObjectUtil.defaultIfNull(copyOptions, CopyOptions.create())).copy();
copyOptions = new CopyOptions();
}
BeanCopier.create(source, target, copyOptions).copy();
} }
/** /**
@ -742,13 +740,10 @@ public class BeanUtil {
* @param targetType 目标Bean类型 * @param targetType 目标Bean类型
* @param <T> Bean类型 * @param <T> Bean类型
* @return 复制后的List * @return 复制后的List
* @since 5.6.6
*/ */
public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType){ public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType){
return collection.stream().map((source)->{ return copyToList(collection, targetType, CopyOptions.create());
final T target = ReflectUtil.newInstanceIfPossible(targetType);
copyProperties(source, target);
return target;
}).collect(Collectors.toList());
} }
/** /**
@ -843,7 +838,7 @@ public class BeanUtil {
* @return 是否为空{@code true} - / {@code false} - 非空 * @return 是否为空{@code true} - / {@code false} - 非空
* @since 4.1.10 * @since 4.1.10
*/ */
public static boolean isEmpty(Object bean, String... ignoreFiledNames) { public static boolean isEmpty(Object bean, String... ignoreFiledNames) {
if (null != bean) { if (null != bean) {
for (Field field : ReflectUtil.getFields(bean.getClass())) { for (Field field : ReflectUtil.getFields(bean.getClass())) {
if (ModifierUtil.isStatic(field)) { if (ModifierUtil.isStatic(field)) {

View File

@ -1,5 +1,7 @@
package cn.hutool.core.util; package cn.hutool.core.util;
import cn.hutool.core.exceptions.UtilException;
import java.awt.Color; import java.awt.Color;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -160,6 +162,16 @@ public class HexUtil {
return StrUtil.str(decodeHex(hexData), charset); return StrUtil.str(decodeHex(hexData), charset);
} }
/**
* 将十六进制字符串解码为byte[]
*
* @param hexStr 十六进制String
* @return byte[]
*/
public static byte[] decodeHex(String hexStr) {
return decodeHex((CharSequence) hexStr);
}
/** /**
* 将十六进制字符数组转换为字节数组 * 将十六进制字符数组转换为字节数组
* *
@ -168,20 +180,37 @@ public class HexUtil {
* @throws RuntimeException 如果源十六进制字符数组是一个奇怪的长度将抛出运行时异常 * @throws RuntimeException 如果源十六进制字符数组是一个奇怪的长度将抛出运行时异常
*/ */
public static byte[] decodeHex(char[] hexData) { public static byte[] decodeHex(char[] hexData) {
return decodeHex(String.valueOf(hexData));
}
int len = hexData.length; /**
* 将十六进制字符数组转换为字节数组
if ((len & 0x01) != 0) { *
throw new RuntimeException("Odd number of characters."); * @param hexData 十六进制字符串
* @return byte[]
* @throws UtilException 如果源十六进制字符数组是一个奇怪的长度将抛出运行时异常
* @since 5.6.6
*/
public static byte[] decodeHex(CharSequence hexData) {
if (StrUtil.isEmpty(hexData)) {
return null;
} }
byte[] out = new byte[len >> 1]; hexData = StrUtil.cleanBlank(hexData);
final int len = hexData.length();
if ((len & 0x01) != 0) {
throw new UtilException("Odd number of characters.");
}
final byte[] out = new byte[len >> 1];
// two characters form the hex value. // two characters form the hex value.
for (int i = 0, j = 0; j < len; i++) { for (int i = 0, j = 0; j < len; i++) {
int f = toDigit(hexData[j], j) << 4; int f = toDigit(hexData.charAt(j), j) << 4;
j++; j++;
f = f | toDigit(hexData[j], j); f = f | toDigit(hexData.charAt(j), j);
j++; j++;
out[i] = (byte) (f & 0xFF); out[i] = (byte) (f & 0xFF);
} }
@ -189,21 +218,6 @@ public class HexUtil {
return out; return out;
} }
/**
* 将十六进制字符串解码为byte[]
*
* @param hexStr 十六进制String
* @return byte[]
*/
public static byte[] decodeHex(String hexStr) {
if (StrUtil.isEmpty(hexStr)) {
return null;
}
hexStr = StrUtil.cleanBlank(hexStr);
return decodeHex(hexStr.toCharArray());
}
// ---------------------------------------------------------------------------------------- Color // ---------------------------------------------------------------------------------------- Color
/** /**
@ -411,12 +425,12 @@ public class HexUtil {
* @param ch 十六进制char * @param ch 十六进制char
* @param index 十六进制字符在字符数组中的位置 * @param index 十六进制字符在字符数组中的位置
* @return 一个整数 * @return 一个整数
* @throws RuntimeException 当ch不是一个合法的十六进制字符时抛出运行时异常 * @throws UtilException 当ch不是一个合法的十六进制字符时抛出运行时异常
*/ */
private static int toDigit(char ch, int index) { private static int toDigit(char ch, int index) {
int digit = Character.digit(ch, 16); int digit = Character.digit(ch, 16);
if (digit == -1) { if (digit < 0) {
throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index); throw new UtilException("Illegal hexadecimal character {} at index {}", ch, index);
} }
return digit; return digit;
} }

View File

@ -7,7 +7,6 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import lombok.Data; import lombok.Data;
import lombok.Getter; import lombok.Getter;
@ -21,7 +20,14 @@ import java.io.Serializable;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/** /**
* Bean工具单元测试 * Bean工具单元测试
@ -503,12 +509,6 @@ public class BeanUtilTest {
Assert.assertEquals("{codeList={0={name=张三}}}", resultMap.toString()); Assert.assertEquals("{codeList={0={name=张三}}}", resultMap.toString());
} }
@Data
public static class Student{
String name;
int age;
Long no;
}
@Test @Test
public void beanCopyTest() { public void beanCopyTest() {
final Station station = new Station(); final Station station = new Station();
@ -521,25 +521,28 @@ public class BeanUtilTest {
} }
@Test @Test
public void copyListTest(){ public void copyListTest() {
Student student = new Student();
Student student = ReflectUtil.newInstance(Student.class);
student.setName("张三"); student.setName("张三");
student.setAge(123); student.setAge(123);
student.setNo(3158L); student.setNo(3158L);
Student student2 = ReflectUtil.newInstance(Student.class);
Student student2 = new Student();
student.setName("李四"); student.setName("李四");
student.setAge(125); student.setAge(125);
student.setNo(8848L); student.setNo(8848L);
List<Student> studentList = ListUtil.of(student, student2); List<Student> studentList = ListUtil.of(student, student2);
List<Person> people = BeanUtil.copyToList(studentList, Person.class); List<Person> people = BeanUtil.copyToList(studentList, Person.class);
Assert.assertEquals(studentList.size(),people.size());
Assert.assertEquals(studentList.size(), people.size());
for (int i = 0; i < studentList.size(); i++) { for (int i = 0; i < studentList.size(); i++) {
Assert.assertEquals(studentList.get(i).getName(),people.get(i).getName()); Assert.assertEquals(studentList.get(i).getName(), people.get(i).getName());
Assert.assertEquals(studentList.get(i).getAge(),people.get(i).getAge()); Assert.assertEquals(studentList.get(i).getAge(), people.get(i).getAge());
} }
} }
public static class Station extends Tree<Station, Long> { public static class Station extends Tree<Station, Long> {
} }
@ -561,7 +564,7 @@ public class BeanUtilTest {
a.setId("1"); a.setId("1");
a.setName("2"); a.setName("2");
a.setCode("3"); a.setCode("3");
a.setCreateTime(new Date()); a.setCreateTime(new Date());
a.setSortOrder(9L); a.setSortOrder(9L);
Map<String, Object> f = BeanUtil.beanToMap( Map<String, Object> f = BeanUtil.beanToMap(
@ -585,7 +588,7 @@ public class BeanUtilTest {
} }
@Test @Test
public void getFieldValue(){ public void getFieldValue() {
TestPojo testPojo = new TestPojo(); TestPojo testPojo = new TestPojo();
testPojo.setName("名字"); testPojo.setName("名字");
@ -595,23 +598,30 @@ public class BeanUtilTest {
testPojo3.setAge(3); testPojo3.setAge(3);
testPojo.setTestPojo2List(new TestPojo2[]{testPojo2,testPojo3}); testPojo.setTestPojo2List(new TestPojo2[]{testPojo2, testPojo3});
BeanPath beanPath = BeanPath.create("testPojo2List.age"); BeanPath beanPath = BeanPath.create("testPojo2List.age");
Object o = beanPath.get(testPojo); Object o = beanPath.get(testPojo);
Assert.assertEquals(Integer.valueOf(2), ArrayUtil.get(o,0)); Assert.assertEquals(Integer.valueOf(2), ArrayUtil.get(o, 0));
Assert.assertEquals(Integer.valueOf(3), ArrayUtil.get(o,1)); Assert.assertEquals(Integer.valueOf(3), ArrayUtil.get(o, 1));
} }
@Data @Data
public static class TestPojo{ public static class TestPojo {
private String name; private String name;
private TestPojo2[] testPojo2List; private TestPojo2[] testPojo2List;
} }
@Data @Data
public static class TestPojo2{ public static class TestPojo2 {
private int age; private int age;
} }
@Data
public static class Student {
String name;
int age;
Long no;
}
} }