This commit is contained in:
Looly 2022-06-08 16:11:23 +08:00
commit 84711ad790
12 changed files with 112 additions and 24 deletions

View File

@ -11,6 +11,9 @@
* 【core 】 IterUtil.get增加判空issue#I5B12A@Gitee
### 🐞Bug修复
* 【core 】 修复NumberUtil.isXXX空判断错误issue#2356@Github
* 【core 】 修复Convert.toSBC空指针问题issue#I5APKK@Gitee
* 【json 】 修复Bean中存在bytes无法转换问题issue#2365@Github
* 【core 】 ArrayUtil.setOrAppend()传入空数组时抛出异常issue#I5APJE@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -754,23 +754,26 @@ public class Convert {
// ----------------------------------------------------------------------- 全角半角转换
/**
* 半角转全角
* 半角转全角{@code null}返回{@code null}
*
* @param input String.
* @return 全角字符串.
* @return 全角字符串{@code null}返回{@code null}
*/
public static String toSBC(String input) {
return toSBC(input, null);
}
/**
* 半角转全角
* 半角转全角{@code null}返回{@code null}
*
* @param input String
* @param notConvertSet 不替换的字符集合
* @return 全角字符串.
* @return 全角字符串{@code null}返回{@code null}
*/
public static String toSBC(String input, Set<Character> notConvertSet) {
if(StrUtil.isEmpty(input)){
return input;
}
final char[] c = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (null != notConvertSet && notConvertSet.contains(c[i])) {
@ -1096,7 +1099,7 @@ public class Convert {
/**
* long转byte数组<br>
* 默认以小端序转换<br>
* from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java
* from: <a href="https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java">https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java</a>
*
* @param longValue long值
* @return byte数组
@ -1109,7 +1112,7 @@ public class Convert {
/**
* byte数组转long<br>
* 默认以小端序转换<br>
* from: https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java
* from: <a href="https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java">https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java</a>
*
* @param bytes byte数组
* @return long值

View File

@ -1,5 +1,6 @@
package cn.hutool.core.convert.impl;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.convert.AbstractConverter;
import cn.hutool.core.convert.Convert;
@ -124,6 +125,16 @@ public class ArrayConverter extends AbstractConverter<Object> {
return convertArrayToArray(value.toString().toCharArray());
}
//issue#2365
// 字符串转bytes首先判断是否为Base64是则转换否则按照默认getBytes方法
if(targetComponentType == byte.class){
final String str = value.toString();
if(Base64.isBase64(str)){
return Base64.decode(value.toString());
}
return str.getBytes();
}
// 单纯字符串情况下按照逗号分隔后劈开
final String[] strings = StrUtil.splitToArray(value.toString(), CharUtil.COMMA);
return convertArrayToArray(strings);

View File

@ -18,6 +18,7 @@ import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.*;
@ -1337,7 +1338,7 @@ public class DateUtil extends CalendarUtil {
return offset(date, DateField.HOUR_OF_DAY, offset);
}
/**
/**w
* 偏移天
*
* @param date 日期
@ -2100,6 +2101,32 @@ public class DateUtil extends CalendarUtil {
return LocalDateTimeUtil.of(date);
}
/**
* {@link Date} 转换时区
*
* @param date {@link Date}
* @param zoneId{@link zoneId}
* @return {@link DateTime}
* @see DateTime(Date, ZoneId )
* @since 5.8.3
*/
public static DateTime convertTimeZone(Date date, ZoneId zoneId) {
return new DateTime(date, ZoneUtil.toTimeZone(zoneId));
}
/**
* {@link Date} 转换时区
*
* @param date {@link Date}
* @param timeZone{@link timeZone}
* @return {@link DateTime}
* @see DateTime(Date,ZoneId)
* @since 5.8.3
*/
public static DateTime convertTimeZone(Date date, TimeZone timeZone) {
return new DateTime(date, timeZone);
}
/**
* 获得指定年份的总天数
*

View File

@ -5,24 +5,11 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.*;
import java.time.chrono.ChronoLocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalUnit;
import java.time.temporal.WeekFields;
import java.time.temporal.*;
import java.util.Date;
import java.util.TimeZone;
@ -30,7 +17,7 @@ import java.util.TimeZone;
* JDK8+中的{@link LocalDateTime} 工具类封装
*
* @author looly
* @see DateUtil java7和下版本使用Date工具类
* @see DateUtil java7和下版本使用Date工具类
* @see DatePattern 常用格式工具类
* @since 5.3.9
*/

View File

@ -1046,7 +1046,7 @@ public class ImgUtil {
/**
* 旋转图片为指定角度<br>
* 来自http://blog.51cto.com/cping1982/130066
* 来自<a href="http://blog.51cto.com/cping1982/130066">http://blog.51cto.com/cping1982/130066</a>
*
* @param image 目标图像
* @param degree 旋转角度

View File

@ -347,6 +347,13 @@ public class ArrayUtil extends PrimitiveArrayUtil {
Array.set(buffer, index, value);
return buffer;
} else {
if(ArrayUtil.isEmpty(buffer)){
// issue#I5APJE
// 可变长类型在buffer为空的情况下类型会被擦除导致报错此处修正
final T[] values = newArray(value.getClass(), 1);
values[0] = value;
return append(buffer, values);
}
return append(buffer, value);
}
}

View File

@ -392,4 +392,16 @@ public class ConvertTest {
final LocalDate convert = Convert.convert(LocalDate.class, localDateTime);
Assert.assertEquals(localDateTime.toLocalDate(), convert);
}
@Test
public void toSBCTest(){
final String s = Convert.toSBC(null);
Assert.assertNull(s);
}
@Test
public void toDBCTest(){
final String s = Convert.toDBC(null);
Assert.assertNull(s);
}
}

View File

@ -527,4 +527,11 @@ public class ArrayUtilTest {
result = ArrayUtil.replace(g, 0, h);
Assert.assertArrayEquals(g, result);
}
@Test
public void setOrAppendTest(){
String[] arr = new String[0];
String[] newArr = ArrayUtil.setOrAppend(arr, 0, "Good");// ClassCastException
Assert.assertArrayEquals(new String[]{"Good"}, newArr);
}
}

View File

@ -297,6 +297,15 @@ public abstract class HttpBase<T> {
return (T) this;
}
/**
* 获取bodyBytes存储字节码
*
* @return byte[]
*/
public byte[] bodyBytes() {
return this.bodyBytes;
}
/**
* 返回字符集
*

View File

@ -249,6 +249,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
*
* @return byte[]
*/
@Override
public byte[] bodyBytes() {
sync();
return this.bodyBytes;

View File

@ -0,0 +1,21 @@
import cn.hutool.json.JSONUtil;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
public class Issue2365Test {
@Test
public void toBeanTest(){
String jsonStr = "{\"fileName\":\"aaa\",\"fileBytes\":\"AQ==\"}";
final FileInfo fileInfo = JSONUtil.toBean(jsonStr, FileInfo.class);
Assert.assertEquals("aaa", fileInfo.getFileName());
Assert.assertArrayEquals(new byte[]{1}, fileInfo.getFileBytes());
}
@Data
public static class FileInfo {
private String fileName;
private byte[] fileBytes;
}
}