From 7c25bf28701789c7e12d09eec66370775439df18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=A2=A6=E6=8A=80=E6=9C=AF?= <596392912@qq.com> Date: Wed, 24 Apr 2024 12:08:34 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=AE=8C=E5=96=84=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/tool/utils/NumberUtil.java | 33 ++++++++++- .../core/tool/utils/StringUtil.java | 57 ++++++++++++++++++- .../springblade/core/tool/utils/UrlUtil.java | 4 +- 3 files changed, 87 insertions(+), 7 deletions(-) diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/NumberUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/NumberUtil.java index ccb057a..2ce4a87 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/NumberUtil.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/NumberUtil.java @@ -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; + } + } diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/StringUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/StringUtil.java index b6f1946..5606906 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/StringUtil.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/StringUtil.java @@ -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 { } - /** * 首字母变小写 * diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/UrlUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/UrlUtil.java index 1d2eafd..19f7ff7 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/UrlUtil.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/UrlUtil.java @@ -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); } /**