mirror of
https://github.com/chillzhuang/blade-tool
synced 2024-12-23 01:29:29 +08:00
✨ 完善工具类
This commit is contained in:
parent
e2dceaef0f
commit
7c25bf2870
@ -172,7 +172,7 @@ public class NumberUtil extends org.springframework.util.NumberUtils {
|
||||
/**
|
||||
* All possible chars for representing a number as a String
|
||||
*/
|
||||
private final static char[] DIGITS = {
|
||||
protected final static byte[] DIGITS = {
|
||||
'0', '1', '2', '3', '4', '5',
|
||||
'6', '7', '8', '9', 'a', 'b',
|
||||
'c', 'd', 'e', 'f', 'g', 'h',
|
||||
@ -194,7 +194,7 @@ public class NumberUtil extends org.springframework.util.NumberUtils {
|
||||
*/
|
||||
public static String to62String(long i) {
|
||||
int radix = DIGITS.length;
|
||||
char[] buf = new char[65];
|
||||
byte[] buf = new byte[65];
|
||||
int charPos = 64;
|
||||
i = -i;
|
||||
while (i <= -radix) {
|
||||
@ -202,8 +202,35 @@ public class NumberUtil extends org.springframework.util.NumberUtils {
|
||||
i = i / radix;
|
||||
}
|
||||
buf[charPos] = DIGITS[(int) (-i)];
|
||||
|
||||
return new String(buf, charPos, (65 - charPos));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 62 进制字符串转为数字
|
||||
*
|
||||
* @param s 字符串
|
||||
* @return 数字
|
||||
*/
|
||||
public static long form62String(String s) {
|
||||
char[] chars = s.toCharArray();
|
||||
char c;
|
||||
int idx;
|
||||
long res = 0;
|
||||
int len = chars.length;
|
||||
int lenIdx = len - 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
c = chars[i];
|
||||
// 将字符转换为对应的数字
|
||||
if (c >= 'A' && c <= 'Z') {
|
||||
idx = c - 29;
|
||||
} else if (c >= 'a' && c <= 'z') {
|
||||
idx = c - 87;
|
||||
} else {
|
||||
idx = c - 48;
|
||||
}
|
||||
res += (long) (idx * StrictMath.pow(62, lenIdx - i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ import org.springframework.web.util.HtmlUtils;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
@ -181,8 +182,59 @@ public class StringUtil extends org.springframework.util.StringUtils {
|
||||
* @return UUID
|
||||
*/
|
||||
public static String randomUUID() {
|
||||
ThreadLocalRandom random = ThreadLocalRandom.current();
|
||||
return new UUID(random.nextLong(), random.nextLong()).toString().replace(StringPool.DASH, StringPool.EMPTY);
|
||||
return getId(ThreadLocalRandom.current(), 32, 16);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个快速生成的随机 id,包含数字,大小写,同长度比 uuid 冲突概率更小得多
|
||||
*
|
||||
* @param len 为了减少冲突,len 需要大于7,实际尽量设置在10~16或以上。
|
||||
* @return id 字符串
|
||||
*/
|
||||
public static String getFastId(int len) {
|
||||
return getId(ThreadLocalRandom.current(), len, 62);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个安全的随机 id,包含数字,大小写,同长度比 uuid 冲突概率更小得多
|
||||
*
|
||||
* @param len 为了减少冲突,len 需要大于7,实际尽量设置在10~16或以上。
|
||||
* @return id 字符串
|
||||
*/
|
||||
public static String getSafeId(int len) {
|
||||
return getId(Holder.SECURE_RANDOM, len, 62);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个生成的随机 id,同长度比 uuid 冲突概率更小得多
|
||||
*
|
||||
* @param random Random
|
||||
* @param len 为了减少冲突,len 需要大于7,实际尽量设置在10~16或以上。
|
||||
* @return id 字符串
|
||||
*/
|
||||
public static String getId(Random random, int len) {
|
||||
return getId(random, len, 62);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个生成的随机 id,同长度比 uuid 冲突概率更小得多
|
||||
*
|
||||
* @param random Random
|
||||
* @param len 为了减少冲突,len 需要大于7,实际尽量设置在10~16或以上。
|
||||
* @param radix radix,36 包含字母和数字,62 包含大写字母
|
||||
* @return id 字符串
|
||||
*/
|
||||
public static String getId(Random random, int len, int radix) {
|
||||
if (len < 8) {
|
||||
throw new IllegalArgumentException("为了减少冲突,len 需要大于7,实际尽量设置在10~16或以上。");
|
||||
}
|
||||
byte[] randomBytes = new byte[len];
|
||||
random.nextBytes(randomBytes);
|
||||
int mask = radix - 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
randomBytes[i] = NumberUtil.DIGITS[(randomBytes[i] & 0xff) & mask];
|
||||
}
|
||||
return new String(randomBytes, StandardCharsets.ISO_8859_1);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1444,7 +1496,6 @@ public class StringUtil extends org.springframework.util.StringUtils {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 首字母变小写
|
||||
*
|
||||
|
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springblade.core.tool.utils;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
@ -45,7 +47,7 @@ public class UrlUtil extends org.springframework.web.util.UriUtils {
|
||||
* @return 解码url
|
||||
*/
|
||||
public static String decodeURL(String source, Charset charset) {
|
||||
return UrlUtil.decode(source, charset.name());
|
||||
return StringUtils.uriDecode(source, charset);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user