mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix HexUtil
This commit is contained in:
parent
890307e123
commit
ab79a7e2bb
@ -3,10 +3,11 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.6.6 (2021-05-10)
|
||||
# 5.6.6 (2021-05-11)
|
||||
|
||||
### 🐣新特性
|
||||
* 【cron 】 增加时间轮简单实现
|
||||
* 【core 】 BeanUtil.copyToList增加重载(pr#321@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
|
||||
|
@ -12,6 +12,7 @@ import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.ModifierUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
@ -709,10 +710,7 @@ public class BeanUtil {
|
||||
* @param copyOptions 拷贝选项,见 {@link CopyOptions}
|
||||
*/
|
||||
public static void copyProperties(Object source, Object target, CopyOptions copyOptions) {
|
||||
if (null == copyOptions) {
|
||||
copyOptions = new CopyOptions();
|
||||
}
|
||||
BeanCopier.create(source, target, copyOptions).copy();
|
||||
BeanCopier.create(source, target, ObjectUtil.defaultIfNull(copyOptions, CopyOptions.create())).copy();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -742,13 +740,10 @@ public class BeanUtil {
|
||||
* @param targetType 目标Bean类型
|
||||
* @param <T> Bean类型
|
||||
* @return 复制后的List
|
||||
* @since 5.6.6
|
||||
*/
|
||||
public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType){
|
||||
return collection.stream().map((source)->{
|
||||
final T target = ReflectUtil.newInstanceIfPossible(targetType);
|
||||
copyProperties(source, target);
|
||||
return target;
|
||||
}).collect(Collectors.toList());
|
||||
return copyToList(collection, targetType, CopyOptions.create());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -843,7 +838,7 @@ public class BeanUtil {
|
||||
* @return 是否为空,{@code true} - 空 / {@code false} - 非空
|
||||
* @since 4.1.10
|
||||
*/
|
||||
public static boolean isEmpty(Object bean, String... ignoreFiledNames) {
|
||||
public static boolean isEmpty(Object bean, String... ignoreFiledNames) {
|
||||
if (null != bean) {
|
||||
for (Field field : ReflectUtil.getFields(bean.getClass())) {
|
||||
if (ModifierUtil.isStatic(field)) {
|
||||
|
@ -1,5 +1,7 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.Charset;
|
||||
@ -160,6 +162,16 @@ public class HexUtil {
|
||||
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 如果源十六进制字符数组是一个奇怪的长度,将抛出运行时异常
|
||||
*/
|
||||
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.
|
||||
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++;
|
||||
f = f | toDigit(hexData[j], j);
|
||||
f = f | toDigit(hexData.charAt(j), j);
|
||||
j++;
|
||||
out[i] = (byte) (f & 0xFF);
|
||||
}
|
||||
@ -189,21 +218,6 @@ public class HexUtil {
|
||||
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
|
||||
|
||||
/**
|
||||
@ -411,12 +425,12 @@ public class HexUtil {
|
||||
* @param ch 十六进制char
|
||||
* @param index 十六进制字符在字符数组中的位置
|
||||
* @return 一个整数
|
||||
* @throws RuntimeException 当ch不是一个合法的十六进制字符时,抛出运行时异常
|
||||
* @throws UtilException 当ch不是一个合法的十六进制字符时,抛出运行时异常
|
||||
*/
|
||||
private static int toDigit(char ch, int index) {
|
||||
int digit = Character.digit(ch, 16);
|
||||
if (digit == -1) {
|
||||
throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index);
|
||||
if (digit < 0) {
|
||||
throw new UtilException("Illegal hexadecimal character {} at index {}", ch, index);
|
||||
}
|
||||
return digit;
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
@ -21,7 +20,14 @@ import java.io.Serializable;
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.LocalDate;
|
||||
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工具单元测试
|
||||
@ -503,12 +509,6 @@ public class BeanUtilTest {
|
||||
Assert.assertEquals("{codeList={0={name=张三}}}", resultMap.toString());
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Student{
|
||||
String name;
|
||||
int age;
|
||||
Long no;
|
||||
}
|
||||
@Test
|
||||
public void beanCopyTest() {
|
||||
final Station station = new Station();
|
||||
@ -521,25 +521,28 @@ public class BeanUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyListTest(){
|
||||
|
||||
Student student = ReflectUtil.newInstance(Student.class);
|
||||
public void copyListTest() {
|
||||
Student student = new Student();
|
||||
student.setName("张三");
|
||||
student.setAge(123);
|
||||
student.setNo(3158L);
|
||||
Student student2 = ReflectUtil.newInstance(Student.class);
|
||||
|
||||
Student student2 = new Student();
|
||||
student.setName("李四");
|
||||
student.setAge(125);
|
||||
student.setNo(8848L);
|
||||
|
||||
List<Student> studentList = ListUtil.of(student, student2);
|
||||
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++) {
|
||||
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).getName(), people.get(i).getName());
|
||||
Assert.assertEquals(studentList.get(i).getAge(), people.get(i).getAge());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Station extends Tree<Station, Long> {
|
||||
|
||||
}
|
||||
@ -561,7 +564,7 @@ public class BeanUtilTest {
|
||||
a.setId("1");
|
||||
a.setName("2");
|
||||
a.setCode("3");
|
||||
a.setCreateTime(new Date());
|
||||
a.setCreateTime(new Date());
|
||||
a.setSortOrder(9L);
|
||||
|
||||
Map<String, Object> f = BeanUtil.beanToMap(
|
||||
@ -585,7 +588,7 @@ public class BeanUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFieldValue(){
|
||||
public void getFieldValue() {
|
||||
TestPojo testPojo = new TestPojo();
|
||||
testPojo.setName("名字");
|
||||
|
||||
@ -595,23 +598,30 @@ public class BeanUtilTest {
|
||||
testPojo3.setAge(3);
|
||||
|
||||
|
||||
testPojo.setTestPojo2List(new TestPojo2[]{testPojo2,testPojo3});
|
||||
testPojo.setTestPojo2List(new TestPojo2[]{testPojo2, testPojo3});
|
||||
|
||||
BeanPath beanPath = BeanPath.create("testPojo2List.age");
|
||||
Object o = beanPath.get(testPojo);
|
||||
|
||||
Assert.assertEquals(Integer.valueOf(2), ArrayUtil.get(o,0));
|
||||
Assert.assertEquals(Integer.valueOf(3), ArrayUtil.get(o,1));
|
||||
Assert.assertEquals(Integer.valueOf(2), ArrayUtil.get(o, 0));
|
||||
Assert.assertEquals(Integer.valueOf(3), ArrayUtil.get(o, 1));
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class TestPojo{
|
||||
public static class TestPojo {
|
||||
private String name;
|
||||
private TestPojo2[] testPojo2List;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class TestPojo2{
|
||||
public static class TestPojo2 {
|
||||
private int age;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Student {
|
||||
String name;
|
||||
int age;
|
||||
Long no;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user