mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add ZeroPadding support
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.collection.IterUtil;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.lang.Editor;
|
||||
import cn.hutool.core.lang.Filter;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 数组工具类
|
||||
*
|
||||
@@ -494,15 +494,63 @@ public class ArrayUtil {
|
||||
* 调整大小后拷贝原数组到新数组下。扩大则占位前N个位置,缩小则截断
|
||||
*
|
||||
* @param <T> 数组元素类型
|
||||
* @param buffer 原数组
|
||||
* @param data 原数组
|
||||
* @param newSize 新的数组大小
|
||||
* @param componentType 数组元素类型
|
||||
* @return 调整后的新数组
|
||||
*/
|
||||
public static <T> T[] resize(T[] buffer, int newSize, Class<?> componentType) {
|
||||
T[] newArray = newArray(componentType, newSize);
|
||||
if (isNotEmpty(buffer)) {
|
||||
System.arraycopy(buffer, 0, newArray, 0, Math.min(buffer.length, newSize));
|
||||
public static <T> T[] resize(T[] data, int newSize, Class<?> componentType) {
|
||||
if(newSize < 0){
|
||||
return data;
|
||||
}
|
||||
|
||||
final T[] newArray = newArray(componentType, newSize);
|
||||
if (newSize > 0 && isNotEmpty(data)) {
|
||||
System.arraycopy(data, 0, newArray, 0, Math.min(data.length, newSize));
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成一个新的重新设置大小的数组<br>
|
||||
* 调整大小后拷贝原数组到新数组下。扩大则占位前N个位置,其它位置补充0,缩小则截断
|
||||
*
|
||||
* @param array 原数组
|
||||
* @param newSize 新的数组大小
|
||||
* @return 调整后的新数组
|
||||
* @since 4.6.7
|
||||
*/
|
||||
public static Object resize(Object array, int newSize) {
|
||||
if(newSize < 0){
|
||||
return array;
|
||||
}
|
||||
if (null == array) {
|
||||
return null;
|
||||
}
|
||||
final int length = length(array);
|
||||
final Object newArray = Array.newInstance(array.getClass().getComponentType(), newSize);
|
||||
if (newSize > 0 && isNotEmpty(array)) {
|
||||
System.arraycopy(array, 0, newArray, 0, Math.min(length, newSize));
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成一个新的重新设置大小的数组<br>
|
||||
* 调整大小后拷贝原数组到新数组下。扩大则占位前N个位置,其它位置补充0,缩小则截断
|
||||
*
|
||||
* @param bytes 原数组
|
||||
* @param newSize 新的数组大小
|
||||
* @return 调整后的新数组
|
||||
* @since 4.6.7
|
||||
*/
|
||||
public static byte[] resize(byte[] bytes, int newSize) {
|
||||
if(newSize < 0){
|
||||
return bytes;
|
||||
}
|
||||
final byte[] newArray = new byte[newSize];
|
||||
if (newSize > 0 && isNotEmpty(bytes)) {
|
||||
System.arraycopy(bytes, 0, newArray, 0, Math.min(bytes.length, newSize));
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
|
@@ -337,7 +337,7 @@ public class RandomUtil {
|
||||
* @return 随机元素
|
||||
*/
|
||||
public static <T> List<T> randomEles(List<T> list, int count) {
|
||||
final List<T> result = new ArrayList<T>(count);
|
||||
final List<T> result = new ArrayList<>(count);
|
||||
int limit = list.size();
|
||||
while (result.size() < count) {
|
||||
result.add(randomEle(list, limit));
|
||||
@@ -361,7 +361,7 @@ public class RandomUtil {
|
||||
throw new IllegalArgumentException("Count is larger than collection distinct size !");
|
||||
}
|
||||
|
||||
final HashSet<T> result = new HashSet<T>(count);
|
||||
final HashSet<T> result = new HashSet<>(count);
|
||||
int limit = source.size();
|
||||
while (result.size() < count) {
|
||||
result.add(randomEle(source, limit));
|
||||
@@ -409,14 +409,14 @@ public class RandomUtil {
|
||||
* @return 随机字符串
|
||||
*/
|
||||
public static String randomString(String baseString, int length) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
final StringBuilder sb = new StringBuilder(length);
|
||||
|
||||
if (length < 1) {
|
||||
length = 1;
|
||||
}
|
||||
int baseLength = baseString.length();
|
||||
for (int i = 0; i < length; i++) {
|
||||
int number = getRandom().nextInt(baseLength);
|
||||
int number = randomInt(baseLength);
|
||||
sb.append(baseString.charAt(number));
|
||||
}
|
||||
return sb.toString();
|
||||
@@ -450,7 +450,7 @@ public class RandomUtil {
|
||||
* @since 3.1.2
|
||||
*/
|
||||
public static char randomChar(String baseString) {
|
||||
return baseString.charAt(getRandom().nextInt(baseString.length()));
|
||||
return baseString.charAt(randomInt(baseString.length()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user