diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/beans/MicaBeanCopier.java b/mica-core/src/main/java/net/dreamlu/mica/core/beans/MicaBeanCopier.java index 9433ce6d1..0b2a8f3b6 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/beans/MicaBeanCopier.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/beans/MicaBeanCopier.java @@ -16,7 +16,10 @@ package net.dreamlu.mica.core.beans; -import net.dreamlu.mica.core.utils.*; +import net.dreamlu.mica.core.utils.BeanUtil; +import net.dreamlu.mica.core.utils.CollectionUtil; +import net.dreamlu.mica.core.utils.ReflectUtil; +import net.dreamlu.mica.core.utils.StringUtil; import org.springframework.asm.ClassVisitor; import org.springframework.asm.Label; import org.springframework.asm.Opcodes; diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/convert/MicaConverter.java b/mica-core/src/main/java/net/dreamlu/mica/core/convert/MicaConverter.java index f8505bce7..38bf90a69 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/convert/MicaConverter.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/convert/MicaConverter.java @@ -19,7 +19,10 @@ import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import net.dreamlu.mica.core.function.CheckedFunction; -import net.dreamlu.mica.core.utils.*; +import net.dreamlu.mica.core.utils.CollectionUtil; +import net.dreamlu.mica.core.utils.ConvertUtil; +import net.dreamlu.mica.core.utils.ReflectUtil; +import net.dreamlu.mica.core.utils.Unchecked; import org.springframework.cglib.core.Converter; import org.springframework.core.convert.TypeDescriptor; import org.springframework.lang.Nullable; diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/$.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/$.java index 4641b0d1c..ff1a4beff 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/$.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/$.java @@ -42,6 +42,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.text.DecimalFormat; import java.time.Duration; import java.time.LocalDate; @@ -1010,7 +1011,7 @@ public static byte[] sha224(final byte[] bytes) { * @return digest as a hex string */ public static String sha224Hex(String data) { - return DigestUtil.sha224Hex(data.getBytes(Charsets.UTF_8)); + return DigestUtil.sha224Hex(data.getBytes(StandardCharsets.UTF_8)); } /** @@ -1855,7 +1856,7 @@ public static Map readJsonAsMap(@Nullable String content, Class * @return the encoded String */ public static String urlEncode(String source) { - return UriUtils.encode(source, Charsets.UTF_8); + return UriUtils.encode(source, StandardCharsets.UTF_8); } /** @@ -1879,7 +1880,7 @@ public static String urlEncode(String source, Charset charset) { * @see java.net.URLDecoder#decode(String, String) */ public static String urlDecode(String source) { - return StringUtils.uriDecode(source, Charsets.UTF_8); + return StringUtils.uriDecode(source, StandardCharsets.UTF_8); } /** diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/AesUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/AesUtil.java index 688b4434b..ab17a9754 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/AesUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/AesUtil.java @@ -21,7 +21,6 @@ * @author L.cm */ public class AesUtil { - public static final Charset DEFAULT_CHARSET = Charsets.UTF_8; public static String genAesKey() { return StringUtil.random(32); @@ -49,7 +48,7 @@ public static SecretKeySpec genMySqlAesKey(final byte[] key) { * @return SecretKeySpec */ public static SecretKeySpec genMySqlAesKey(final String key) { - return genMySqlAesKey(key.getBytes(DEFAULT_CHARSET)); + return genMySqlAesKey(key.getBytes(StandardCharsets.UTF_8)); } public static String encryptToHex(String content, String aesTextKey) { @@ -69,7 +68,7 @@ public static String encryptToBase64(byte[] content, String aesTextKey) { } public static byte[] encrypt(String content, String aesTextKey) { - return encrypt(content.getBytes(DEFAULT_CHARSET), aesTextKey); + return encrypt(content.getBytes(StandardCharsets.UTF_8), aesTextKey); } public static byte[] encrypt(String content, Charset charset, String aesTextKey) { @@ -77,7 +76,7 @@ public static byte[] encrypt(String content, Charset charset, String aesTextKey) } public static byte[] encrypt(byte[] content, String aesTextKey) { - return encrypt(content, Objects.requireNonNull(aesTextKey).getBytes(DEFAULT_CHARSET)); + return encrypt(content, Objects.requireNonNull(aesTextKey).getBytes(StandardCharsets.UTF_8)); } @Nullable @@ -86,7 +85,7 @@ public static String decryptFormHexToString(@Nullable String content, String aes if (hexBytes == null) { return null; } - return new String(hexBytes, DEFAULT_CHARSET); + return new String(hexBytes, StandardCharsets.UTF_8); } @Nullable @@ -94,7 +93,7 @@ public static byte[] decryptFormHex(@Nullable String content, String aesTextKey) if (StringUtil.isBlank(content)) { return null; } - return decryptFormHex(content.getBytes(DEFAULT_CHARSET), aesTextKey); + return decryptFormHex(content.getBytes(StandardCharsets.UTF_8), aesTextKey); } public static byte[] decryptFormHex(byte[] content, String aesTextKey) { @@ -107,7 +106,7 @@ public static String decryptFormBase64ToString(@Nullable String content, String if (hexBytes == null) { return null; } - return new String(hexBytes, DEFAULT_CHARSET); + return new String(hexBytes, StandardCharsets.UTF_8); } @Nullable @@ -115,7 +114,7 @@ public static byte[] decryptFormBase64(@Nullable String content, String aesTextK if (StringUtil.isBlank(content)) { return null; } - return decryptFormBase64(content.getBytes(DEFAULT_CHARSET), aesTextKey); + return decryptFormBase64(content.getBytes(StandardCharsets.UTF_8), aesTextKey); } public static byte[] decryptFormBase64(byte[] content, String aesTextKey) { @@ -123,11 +122,11 @@ public static byte[] decryptFormBase64(byte[] content, String aesTextKey) { } public static String decryptToString(byte[] content, String aesTextKey) { - return new String(decrypt(content, aesTextKey), DEFAULT_CHARSET); + return new String(decrypt(content, aesTextKey), StandardCharsets.UTF_8); } public static byte[] decrypt(byte[] content, String aesTextKey) { - return decrypt(content, Objects.requireNonNull(aesTextKey).getBytes(DEFAULT_CHARSET)); + return decrypt(content, Objects.requireNonNull(aesTextKey).getBytes(StandardCharsets.UTF_8)); } public static byte[] encrypt(byte[] content, byte[] aesKey) { @@ -190,7 +189,7 @@ public static T encryptMysql(String input, String aesKey, Function txt.getBytes(DEFAULT_CHARSET), aesKey); + return decryptMysql(input, txt -> txt.getBytes(StandardCharsets.UTF_8), aesKey); } /** @@ -219,7 +218,7 @@ public static byte[] decryptMysql(String input, Function inputMa * @return 字符串 */ public static String decryptMysqlToString(String input, Function inputMapper, String aesKey) { - return new String(decryptMysql(input, inputMapper, aesKey), DEFAULT_CHARSET); + return new String(decryptMysql(input, inputMapper, aesKey), StandardCharsets.UTF_8); } /** @@ -230,7 +229,7 @@ public static String decryptMysqlToString(String input, Function * @return 字符串 */ public static String decryptMysqlToString(String input, String aesKey) { - return decryptMysqlToString(input, txt -> txt.getBytes(DEFAULT_CHARSET), aesKey); + return decryptMysqlToString(input, txt -> txt.getBytes(StandardCharsets.UTF_8), aesKey); } } diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/Base64Util.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/Base64Util.java index 7441dccd8..6ee5b9186 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/Base64Util.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/Base64Util.java @@ -17,6 +17,7 @@ package net.dreamlu.mica.core.utils; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Base64; /** @@ -37,7 +38,7 @@ public class Base64Util { * @return {String} */ public static String encode(String value) { - return encode(value, Charsets.UTF_8); + return encode(value, StandardCharsets.UTF_8); } /** @@ -59,7 +60,7 @@ public static String encode(String value, Charset charset) { * @return {String} */ public static String encodeUrlSafe(String value) { - return encodeUrlSafe(value, Charsets.UTF_8); + return encodeUrlSafe(value, StandardCharsets.UTF_8); } /** @@ -81,7 +82,7 @@ public static String encodeUrlSafe(String value, Charset charset) { * @return {String} */ public static String decode(String value) { - return decode(value, Charsets.UTF_8); + return decode(value, StandardCharsets.UTF_8); } /** @@ -104,7 +105,7 @@ public static String decode(String value, Charset charset) { * @return {String} */ public static String decodeUrlSafe(String value) { - return decodeUrlSafe(value, Charsets.UTF_8); + return decodeUrlSafe(value, StandardCharsets.UTF_8); } /** @@ -185,7 +186,7 @@ public static String encodeToString(byte[] src) { if (src.length == 0) { return ""; } - return new String(encode(src), Charsets.UTF_8); + return new String(encode(src), StandardCharsets.UTF_8); } /** @@ -198,7 +199,7 @@ public static byte[] decodeFromString(String src) { if (src.isEmpty()) { return new byte[0]; } - return decode(src.getBytes(Charsets.UTF_8)); + return decode(src.getBytes(StandardCharsets.UTF_8)); } /** @@ -209,7 +210,7 @@ public static byte[] decodeFromString(String src) { * @return the encoded byte array as a UTF-8 String */ public static String encodeToUrlSafeString(byte[] src) { - return new String(encodeUrlSafe(src), Charsets.UTF_8); + return new String(encodeUrlSafe(src), StandardCharsets.UTF_8); } /** @@ -220,7 +221,7 @@ public static String encodeToUrlSafeString(byte[] src) { * @return the original byte array */ public static byte[] decodeFromUrlSafeString(String src) { - return decodeUrlSafe(src.getBytes(Charsets.UTF_8)); + return decodeUrlSafe(src.getBytes(StandardCharsets.UTF_8)); } } diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/DesUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/DesUtil.java index e4098622e..c755dfebb 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/DesUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/DesUtil.java @@ -21,6 +21,7 @@ import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; +import java.nio.charset.StandardCharsets; import java.util.Objects; /** @@ -66,7 +67,7 @@ public static String encryptToHex(@Nullable String data, String password) { if (StringUtil.isBlank(data)) { return null; } - byte[] dataBytes = data.getBytes(Charsets.UTF_8); + byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8); return encryptToHex(dataBytes, password); } @@ -83,7 +84,7 @@ public static String decryptFormHex(@Nullable String data, String password) { return null; } byte[] hexBytes = HexUtil.decode(data); - return new String(decrypt(hexBytes, password), Charsets.UTF_8); + return new String(decrypt(hexBytes, password), StandardCharsets.UTF_8); } /** @@ -109,7 +110,7 @@ public static String encryptToBase64(@Nullable String data, String password) { if (StringUtil.isBlank(data)) { return null; } - byte[] dataBytes = data.getBytes(Charsets.UTF_8); + byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8); return encryptToBase64(dataBytes, password); } @@ -138,7 +139,7 @@ public static String decryptFormBase64(@Nullable String data, String password) { return null; } byte[] dataBytes = Base64Util.decodeFromString(data); - return new String(decrypt(dataBytes, password), Charsets.UTF_8); + return new String(decrypt(dataBytes, password), StandardCharsets.UTF_8); } /** @@ -160,7 +161,7 @@ public static byte[] encrypt(byte[] data, byte[] desKey) { * @return byte array */ public static byte[] encrypt(byte[] data, String desKey) { - return encrypt(data, Objects.requireNonNull(desKey).getBytes(Charsets.UTF_8)); + return encrypt(data, Objects.requireNonNull(desKey).getBytes(StandardCharsets.UTF_8)); } /** @@ -182,7 +183,7 @@ public static byte[] decrypt(byte[] data, byte[] desKey) { * @return byte array */ public static byte[] decrypt(byte[] data, String desKey) { - return decrypt(data, Objects.requireNonNull(desKey).getBytes(Charsets.UTF_8)); + return decrypt(data, Objects.requireNonNull(desKey).getBytes(StandardCharsets.UTF_8)); } /** diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/DigestUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/DigestUtil.java index d2cd77b2e..f7e22d6ea 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/DigestUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/DigestUtil.java @@ -50,7 +50,7 @@ public static byte[] md5(final byte[] bytes) { * @return MD5 digest as a hex array */ public static byte[] md5(final String data) { - return DigestUtil.md5(data.getBytes(Charsets.UTF_8)); + return DigestUtil.md5(data.getBytes(StandardCharsets.UTF_8)); } /** @@ -80,7 +80,7 @@ public static String md5Hex(final byte[] bytes) { * @return digest as a hex array */ public static byte[] sha1(String data) { - return DigestUtil.sha1(data.getBytes(Charsets.UTF_8)); + return DigestUtil.sha1(data.getBytes(StandardCharsets.UTF_8)); } /** @@ -100,7 +100,7 @@ public static byte[] sha1(final byte[] bytes) { * @return digest as a hex string */ public static String sha1Hex(String data) { - return DigestUtil.encodeHex(sha1(data.getBytes(Charsets.UTF_8))); + return DigestUtil.encodeHex(sha1(data.getBytes(StandardCharsets.UTF_8))); } /** @@ -120,7 +120,7 @@ public static String sha1Hex(final byte[] bytes) { * @return digest as a byte array */ public static byte[] sha224(String data) { - return DigestUtil.sha224(data.getBytes(Charsets.UTF_8)); + return DigestUtil.sha224(data.getBytes(StandardCharsets.UTF_8)); } /** @@ -140,7 +140,7 @@ public static byte[] sha224(final byte[] bytes) { * @return digest as a hex string */ public static String sha224Hex(String data) { - return DigestUtil.encodeHex(sha224(data.getBytes(Charsets.UTF_8))); + return DigestUtil.encodeHex(sha224(data.getBytes(StandardCharsets.UTF_8))); } /** @@ -160,7 +160,7 @@ public static String sha224Hex(final byte[] bytes) { * @return digest as a byte array */ public static byte[] sha256(String data) { - return DigestUtil.sha256(data.getBytes(Charsets.UTF_8)); + return DigestUtil.sha256(data.getBytes(StandardCharsets.UTF_8)); } /** @@ -180,7 +180,7 @@ public static byte[] sha256(final byte[] bytes) { * @return digest as a hex string */ public static String sha256Hex(String data) { - return DigestUtil.encodeHex(sha256(data.getBytes(Charsets.UTF_8))); + return DigestUtil.encodeHex(sha256(data.getBytes(StandardCharsets.UTF_8))); } /** @@ -200,7 +200,7 @@ public static String sha256Hex(final byte[] bytes) { * @return digest as a byte array */ public static byte[] sha384(String data) { - return DigestUtil.sha384(data.getBytes(Charsets.UTF_8)); + return DigestUtil.sha384(data.getBytes(StandardCharsets.UTF_8)); } /** @@ -220,7 +220,7 @@ public static byte[] sha384(final byte[] bytes) { * @return digest as a hex string */ public static String sha384Hex(String data) { - return DigestUtil.encodeHex(sha384(data.getBytes(Charsets.UTF_8))); + return DigestUtil.encodeHex(sha384(data.getBytes(StandardCharsets.UTF_8))); } /** @@ -240,7 +240,7 @@ public static String sha384Hex(final byte[] bytes) { * @return digest as a byte array */ public static byte[] sha512(String data) { - return DigestUtil.sha512(data.getBytes(Charsets.UTF_8)); + return DigestUtil.sha512(data.getBytes(StandardCharsets.UTF_8)); } /** @@ -260,7 +260,7 @@ public static byte[] sha512(final byte[] bytes) { * @return digest as a hex string */ public static String sha512Hex(String data) { - return DigestUtil.encodeHex(sha512(data.getBytes(Charsets.UTF_8))); + return DigestUtil.encodeHex(sha512(data.getBytes(StandardCharsets.UTF_8))); } /** @@ -308,7 +308,7 @@ public static String digestHex(String algorithm, byte[] bytes) { * @return digest as a byte array */ public static byte[] hmacMd5(String data, String key) { - return DigestUtil.hmacMd5(data.getBytes(Charsets.UTF_8), key); + return DigestUtil.hmacMd5(data.getBytes(StandardCharsets.UTF_8), key); } /** @@ -330,7 +330,7 @@ public static byte[] hmacMd5(final byte[] bytes, String key) { * @return digest as a hex string */ public static String hmacMd5Hex(String data, String key) { - return DigestUtil.encodeHex(hmacMd5(data.getBytes(Charsets.UTF_8), key)); + return DigestUtil.encodeHex(hmacMd5(data.getBytes(StandardCharsets.UTF_8), key)); } /** @@ -352,7 +352,7 @@ public static String hmacMd5Hex(final byte[] bytes, String key) { * @return digest as a byte array */ public static byte[] hmacSha1(String data, String key) { - return DigestUtil.hmacSha1(data.getBytes(Charsets.UTF_8), key); + return DigestUtil.hmacSha1(data.getBytes(StandardCharsets.UTF_8), key); } /** @@ -374,7 +374,7 @@ public static byte[] hmacSha1(final byte[] bytes, String key) { * @return digest as a hex string */ public static String hmacSha1Hex(String data, String key) { - return DigestUtil.encodeHex(hmacSha1(data.getBytes(Charsets.UTF_8), key)); + return DigestUtil.encodeHex(hmacSha1(data.getBytes(StandardCharsets.UTF_8), key)); } /** @@ -396,7 +396,7 @@ public static String hmacSha1Hex(final byte[] bytes, String key) { * @return digest as a hex string */ public static byte[] hmacSha224(String data, String key) { - return DigestUtil.hmacSha224(data.getBytes(Charsets.UTF_8), key); + return DigestUtil.hmacSha224(data.getBytes(StandardCharsets.UTF_8), key); } /** @@ -418,7 +418,7 @@ public static byte[] hmacSha224(final byte[] bytes, String key) { * @return digest as a hex string */ public static String hmacSha224Hex(String data, String key) { - return DigestUtil.encodeHex(hmacSha224(data.getBytes(Charsets.UTF_8), key)); + return DigestUtil.encodeHex(hmacSha224(data.getBytes(StandardCharsets.UTF_8), key)); } /** @@ -440,7 +440,7 @@ public static String hmacSha224Hex(final byte[] bytes, String key) { * @return digest as a hex string */ public static byte[] hmacSha256(String data, String key) { - return DigestUtil.hmacSha256(data.getBytes(Charsets.UTF_8), key); + return DigestUtil.hmacSha256(data.getBytes(StandardCharsets.UTF_8), key); } /** @@ -462,7 +462,7 @@ public static byte[] hmacSha256(final byte[] bytes, String key) { * @return digest as a byte array */ public static String hmacSha256Hex(String data, String key) { - return DigestUtil.encodeHex(hmacSha256(data.getBytes(Charsets.UTF_8), key)); + return DigestUtil.encodeHex(hmacSha256(data.getBytes(StandardCharsets.UTF_8), key)); } /** @@ -484,7 +484,7 @@ public static String hmacSha256Hex(final byte[] bytes, String key) { * @return digest as a byte array */ public static byte[] hmacSha384(String data, String key) { - return DigestUtil.hmacSha384(data.getBytes(Charsets.UTF_8), key); + return DigestUtil.hmacSha384(data.getBytes(StandardCharsets.UTF_8), key); } /** @@ -506,7 +506,7 @@ public static byte[] hmacSha384(final byte[] bytes, String key) { * @return digest as a hex string */ public static String hmacSha384Hex(String data, String key) { - return DigestUtil.encodeHex(hmacSha384(data.getBytes(Charsets.UTF_8), key)); + return DigestUtil.encodeHex(hmacSha384(data.getBytes(StandardCharsets.UTF_8), key)); } /** @@ -528,7 +528,7 @@ public static String hmacSha384Hex(final byte[] bytes, String key) { * @return digest as a byte array */ public static byte[] hmacSha512(String data, String key) { - return DigestUtil.hmacSha512(data.getBytes(Charsets.UTF_8), key); + return DigestUtil.hmacSha512(data.getBytes(StandardCharsets.UTF_8), key); } /** @@ -550,7 +550,7 @@ public static byte[] hmacSha512(final byte[] bytes, String key) { * @return digest as a hex string */ public static String hmacSha512Hex(String data, String key) { - return DigestUtil.encodeHex(hmacSha512(data.getBytes(Charsets.UTF_8), key)); + return DigestUtil.encodeHex(hmacSha512(data.getBytes(StandardCharsets.UTF_8), key)); } /** @@ -594,7 +594,7 @@ public static String digestHmacHex(String algorithm, final byte[] bytes, String * @return digest as a byte array */ public static byte[] digestHmac(String algorithm, final byte[] bytes, String key) { - SecretKey secretKey = new SecretKeySpec(key.getBytes(Charsets.UTF_8), algorithm); + SecretKey secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), algorithm); try { Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); @@ -635,7 +635,7 @@ public static boolean slowEquals(@Nullable String a, @Nullable String b) { if (a == null || b == null) { return false; } - return DigestUtil.slowEquals(a.getBytes(Charsets.UTF_8), b.getBytes(Charsets.UTF_8)); + return DigestUtil.slowEquals(a.getBytes(StandardCharsets.UTF_8), b.getBytes(StandardCharsets.UTF_8)); } /** diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/FileUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/FileUtil.java index 9b3789db2..4e80cbe35 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/FileUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/FileUtil.java @@ -22,6 +22,7 @@ import java.io.*; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -283,7 +284,7 @@ public static File toTempDir(String subDirFile) { * @return the file contents, never {@code null} */ public static String readToString(final File file) { - return readToString(file, Charsets.UTF_8); + return readToString(file, StandardCharsets.UTF_8); } /** @@ -324,7 +325,7 @@ public static byte[] readToByteArray(final File file) { * @param data the content to write to the file */ public static void writeToFile(final File file, final String data) { - writeToFile(file, data, Charsets.UTF_8, false); + writeToFile(file, data, StandardCharsets.UTF_8, false); } /** @@ -336,7 +337,7 @@ public static void writeToFile(final File file, final String data) { * end of the file rather than overwriting */ public static void writeToFile(final File file, final String data, final boolean append) { - writeToFile(file, data, Charsets.UTF_8, append); + writeToFile(file, data, StandardCharsets.UTF_8, append); } /** @@ -490,7 +491,7 @@ public static List readLines(File file) { * @return 行列表 */ public static List readLines(Path path) { - return readLines(path, Charsets.UTF_8); + return readLines(path, StandardCharsets.UTF_8); } /** diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/HexUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/HexUtil.java index 8db97c562..da5f6351d 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/HexUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/HexUtil.java @@ -19,6 +19,7 @@ import org.springframework.lang.Nullable; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; /** * hex 工具,编解码全用 byte @@ -26,7 +27,6 @@ * @author L.cm */ public class HexUtil { - public static final Charset DEFAULT_CHARSET = Charsets.UTF_8; private static final byte[] DIGITS_LOWER = new byte[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; private static final byte[] DIGITS_UPPER = new byte[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; @@ -75,7 +75,7 @@ private static byte[] encode(byte[] data, byte[] digits) { * @return bytes as a hex string */ public static String encodeToString(byte[] data, boolean toLowerCase) { - return new String(encode(data, toLowerCase), DEFAULT_CHARSET); + return new String(encode(data, toLowerCase), StandardCharsets.UTF_8); } /** @@ -85,7 +85,7 @@ public static String encodeToString(byte[] data, boolean toLowerCase) { * @return bytes as a hex string */ public static String encodeToString(byte[] data) { - return encodeToString(data, DEFAULT_CHARSET); + return encodeToString(data, StandardCharsets.UTF_8); } /** @@ -110,7 +110,7 @@ public static String encodeToString(@Nullable String data) { if (StringUtil.isBlank(data)) { return null; } - return encodeToString(data.getBytes(DEFAULT_CHARSET)); + return encodeToString(data.getBytes(StandardCharsets.UTF_8)); } /** @@ -146,7 +146,7 @@ public static String encodeToString(String data, Charset dataCharset, Charset to * @return decode hex to bytes */ public static byte[] decode(String data) { - return decode(data, DEFAULT_CHARSET); + return decode(data, StandardCharsets.UTF_8); } /** @@ -171,7 +171,7 @@ public static byte[] decode(String data, Charset charset) { */ public static String decodeToString(byte[] data) { byte[] decodeBytes = decode(data); - return new String(decodeBytes, DEFAULT_CHARSET); + return new String(decodeBytes, StandardCharsets.UTF_8); } /** @@ -197,7 +197,7 @@ public static String decodeToString(@Nullable String data) { if (StringUtil.isBlank(data)) { return null; } - return decodeToString(data.getBytes(DEFAULT_CHARSET)); + return decodeToString(data.getBytes(StandardCharsets.UTF_8)); } /** diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/IoUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/IoUtil.java index 1dc752715..741b7e9ca 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/IoUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/IoUtil.java @@ -21,6 +21,7 @@ import java.io.*; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; /** * IOUtil @@ -59,7 +60,7 @@ public static void closeQuietly(@Nullable Closeable closeable) { * @return the requested String */ public static String readToString(InputStream input) { - return readToString(input, Charsets.UTF_8); + return readToString(input, StandardCharsets.UTF_8); } /** diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/NumberUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/NumberUtil.java index cf0735b74..8008d6aa9 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/NumberUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/NumberUtil.java @@ -18,6 +18,8 @@ import org.springframework.lang.Nullable; +import java.nio.charset.StandardCharsets; + /** * 数字类型工具类 * @@ -109,16 +111,12 @@ public static long toLong(@Nullable final String str, final long defaultValue) { * All possible chars for representing a number as a String */ static final byte[] DIGITS = { - '0', '1', '2', '3', '4', '5', - '6', '7', '8', '9', 'a', 'b', - 'c', 'd', 'e', 'f', 'g', 'h', - 'i', 'j', 'k', 'l', 'm', 'n', - 'o', 'p', 'q', 'r', 's', 't', - 'u', 'v', 'w', 'x', 'y', 'z', - 'A', 'B', 'C', 'D', 'E', 'F', - 'G', 'H', 'I', 'J', 'K', 'L', - 'M', 'N', 'O', 'P', 'Q', 'R', - 'S', 'T', 'U', 'V', 'W', 'X', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', + 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', + 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '-' }; @@ -138,7 +136,7 @@ public static String to62Str(long i) { i = i / radix; } buf[charPos] = DIGITS[(int) (-i)]; - return new String(buf, charPos, (65 - charPos), Charsets.UTF_8); + return new String(buf, charPos, (65 - charPos), StandardCharsets.UTF_8); } /** diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/PathUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/PathUtil.java index 6f5b36172..265ff172b 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/PathUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/PathUtil.java @@ -22,6 +22,7 @@ import java.io.File; import java.net.URL; +import java.nio.charset.StandardCharsets; /** * 用来获取各种目录 @@ -49,7 +50,7 @@ public static String getJarPath() { private static String toFilePath(@Nullable URL url) { if (url == null) { return null; } String protocol = url.getProtocol(); - String file = UriUtils.decode(url.getPath(), Charsets.UTF_8); + String file = UriUtils.decode(url.getPath(), StandardCharsets.UTF_8); if (ResourceUtils.URL_PROTOCOL_FILE.equals(protocol)) { return new File(file).getParentFile().getParentFile().getAbsolutePath(); } else if (ResourceUtils.URL_PROTOCOL_JAR.equals(protocol) diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/RsaUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/RsaUtil.java index 2dfce5e4a..ff9520c3b 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/RsaUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/RsaUtil.java @@ -22,6 +22,7 @@ import javax.crypto.Cipher; import java.math.BigInteger; +import java.nio.charset.StandardCharsets; import java.security.*; import java.security.interfaces.RSAKey; import java.security.spec.*; @@ -262,7 +263,7 @@ public static String encryptToBase64(PublicKey publicKey, @Nullable String data) if (StringUtil.isBlank(data)) { return null; } - return Base64Util.encodeToString(encrypt(publicKey, data.getBytes(Charsets.UTF_8))); + return Base64Util.encodeToString(encrypt(publicKey, data.getBytes(StandardCharsets.UTF_8))); } /** @@ -416,7 +417,7 @@ public static String decryptFromBase64(PrivateKey privateKey, @Nullable String b if (StringUtil.isBlank(base64Data)) { return null; } - return new String(decrypt(privateKey, Base64Util.decodeFromString(base64Data)), Charsets.UTF_8); + return new String(decrypt(privateKey, Base64Util.decodeFromString(base64Data)), StandardCharsets.UTF_8); } /** @@ -454,7 +455,7 @@ public static String decryptByPublicKeyFromBase64(PublicKey publicKey, @Nullable if (StringUtil.isBlank(base64Data)) { return null; } - return new String(decryptByPublicKey(publicKey, Base64Util.decodeFromString(base64Data)), Charsets.UTF_8); + return new String(decryptByPublicKey(publicKey, Base64Util.decodeFromString(base64Data)), StandardCharsets.UTF_8); } /** diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java index a03068c5c..2f8b2b764 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java @@ -20,6 +20,7 @@ import org.springframework.util.*; import org.springframework.web.util.HtmlUtils; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; import java.util.Map; @@ -418,12 +419,12 @@ public static boolean simpleMatch(@Nullable String[] patterns, String str) { } /** - * 生成uuid(更快,存在冲突的可能),采用 jdk 9 的形式,优化性能 + * 生成 uuid,随机位数是普通 uuid 的一倍,冲突概率更小 * * @return UUID */ public static String getUUID() { - return getUUID(ThreadLocalRandom.current()); + return getId(ThreadLocalRandom.current(), 32, 16); } /** @@ -432,12 +433,8 @@ public static String getUUID() { * @return UUID */ public static String getSafeUUID() { - return getUUID(Holder.SECURE_RANDOM); - } - - private static String getUUID(Random random) { - long lsb = random.nextLong(); - long msb = random.nextLong(); + long lsb = Holder.SECURE_RANDOM.nextLong(); + long msb = Holder.SECURE_RANDOM.nextLong(); byte[] buf = new byte[32]; int radix = 1 << 4; formatUnsignedLong(lsb, radix, buf, 20, 12); @@ -445,7 +442,16 @@ private static String getUUID(Random random) { formatUnsignedLong(msb, radix, buf, 12, 4); formatUnsignedLong(msb >>> 16, radix, buf, 8, 4); formatUnsignedLong(msb >>> 32, radix, buf, 0, 8); - return new String(buf, Charsets.UTF_8); + return new String(buf, StandardCharsets.ISO_8859_1); + } + + private static void formatUnsignedLong(long val, int radix, byte[] buf, int offset, int len) { + int charPos = offset + len; + int mask = radix - 1; + do { + buf[--charPos] = NumberUtil.DIGITS[((int) val) & mask]; + val >>>= 4; + } while (charPos > offset); } /** @@ -485,24 +491,59 @@ public static String getSafeNanoId62() { } private static String getNanoId(Random random, boolean radix64) { - long lsb = random.nextLong(); - long msb = random.nextLong(); - byte[] buf = new byte[21]; - int radix = radix64 ? 64 : 62; - formatUnsignedLong(lsb, radix, buf, 14, 7); - formatUnsignedLong(msb, radix, buf, 10, 4); - formatUnsignedLong(msb >>> 16, radix, buf, 6, 4); - formatUnsignedLong(msb >>> 32, radix, buf, 0, 6); - return new String(buf, Charsets.UTF_8); + return getId(random, 21, radix64 ? 64 : 62); } - private static void formatUnsignedLong(long val, int radix, byte[] buf, int offset, int len) { - int charPos = offset + len; + /** + * 获取一个快速生成的随机 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; - do { - buf[--charPos] = NumberUtil.DIGITS[((int) val) & mask]; - val >>>= 4; - } while (charPos > offset); + for (int i = 0; i < len; i++) { + randomBytes[i] = NumberUtil.DIGITS[(randomBytes[i] & 0xff) & mask]; + } + return new String(randomBytes, StandardCharsets.ISO_8859_1); } /** @@ -995,10 +1036,11 @@ public static boolean isHttpUrl(String text) { * StringUtils.defaultIfBlank("bat", "NULL") = "bat" * StringUtils.defaultIfBlank("", null) = null * - * @param the specific kind of CharSequence - * @param str the CharSequence to check, may be null - * @param defaultStr the default CharSequence to return - * if the input is whitespace, empty ("") or {@code null}, may be null + * + * @param the specific kind of CharSequence + * @param str the CharSequence to check, may be null + * @param defaultStr the default CharSequence to return + * if the input is whitespace, empty ("") or {@code null}, may be null * @return the passed in CharSequence, or the default */ @Nullable diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/UrlUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/UrlUtil.java index 37e1b0583..b4721a32a 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/UrlUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/UrlUtil.java @@ -19,6 +19,8 @@ import org.springframework.util.StringUtils; import org.springframework.web.util.UriUtils; +import java.nio.charset.StandardCharsets; + /** * url处理工具类 * @@ -33,7 +35,7 @@ public class UrlUtil extends org.springframework.web.util.UriUtils { * @return sourced String */ public static String encode(String source) { - return UriUtils.encode(source, Charsets.UTF_8); + return UriUtils.encode(source, StandardCharsets.UTF_8); } /** @@ -43,6 +45,6 @@ public static String encode(String source) { * @return decoded String */ public static String decode(String source) { - return StringUtils.uriDecode(source, Charsets.UTF_8); + return StringUtils.uriDecode(source, StandardCharsets.UTF_8); } } diff --git a/mica-core/src/test/java/net/dreamlu/mica/test/utils/RsaUtilTest.java b/mica-core/src/test/java/net/dreamlu/mica/test/utils/RsaUtilTest.java index 1dcdd1f85..e8f795e02 100644 --- a/mica-core/src/test/java/net/dreamlu/mica/test/utils/RsaUtilTest.java +++ b/mica-core/src/test/java/net/dreamlu/mica/test/utils/RsaUtilTest.java @@ -1,12 +1,13 @@ package net.dreamlu.mica.test.utils; import net.dreamlu.mica.core.tuple.KeyPair; -import net.dreamlu.mica.core.utils.Charsets; import net.dreamlu.mica.core.utils.RsaUtil; import net.dreamlu.mica.core.utils.StringUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.nio.charset.StandardCharsets; + class RsaUtilTest { private final static String text = "我爱mica"; @@ -37,7 +38,7 @@ void test2() { // java 加密,js 解密 String pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDN7Y4Cx2lJtZcPxLmSrHrcHUiDZGTVeHReOn9aTSTC/IGPMiOI+kFuC+3tS2k9GdQL68SUDHHsysoJ2CP6z1C3gPyhM2nVEJOhVu8OczVw0uGO6t7PGzVufwEQA19BLWYWY159EiFmNHiwkzmbsM4hAhEGnmjY6+f/8FtlN85cpwIDAQAB"; String privateBase64 = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAM3tjgLHaUm1lw/EuZKsetwdSINkZNV4dF46f1pNJML8gY8yI4j6QW4L7e1LaT0Z1AvrxJQMcezKygnYI/rPULeA/KEzadUQk6FW7w5zNXDS4Y7q3s8bNW5/ARADX0EtZhZjXn0SIWY0eLCTOZuwziECEQaeaNjr5//wW2U3zlynAgMBAAECgYEAuaugyVymz/DMvUjw0RB2zhQRF3bje53ZvkQcI30+0hf9cPESHSUW7XQQGE5QGuk4yW7QlXQLHCdTt0CMYE2gRdUPkJS2S53efLGfSohCt0LusYFj5uxn3jZqMsr6STspWaWxNjF4DNWT7XOb73B5qG0Vt/v+Zav6/qk2Zy6btlECQQD2f5FU2JKJA7f5+2w3AGRno1AF+SyfmyLLPRwPdyC9DwWK6PGarWGOLC//DkQ8X4x55gtyRVao5KQ+TlWpWV67AkEA1d2kDYAlRJm8JmPMWQnAuaaRPliE7RBtn5wFQoRwGKRD5MtGOUGu7kyydICW7B0GEBmPdBkI27wu46J//pnZBQJAKIT9ydmz9MvksTYQKtZoqtSgseqs2CuzQ39vHmexvQI8IgJ6vLdlgu5mkWGx+86lE+Cp3wXg4fB7wzYzRJxJQwJBAJpAi4PlSnwJOkiiMJCD1UrSFe5W4EEwWTEG0lBgv402ZXXPG65ObsKX9RrMaI6SfH3+QZamO9ppB5TN7u2Ov5UCQQC3xJx2qMbwltY6mPYYU+CdkNeODKmAj1ZtaGEt4oGcEOhSKo6Q34cWlqurgE/vt5jlze0D2Z1ku75ARsx3TpMe"; - String encrypt = RsaUtil.encryptByPrivateKeyToBase64(privateBase64, text.getBytes(Charsets.UTF_8)); + String encrypt = RsaUtil.encryptByPrivateKeyToBase64(privateBase64, text.getBytes(StandardCharsets.UTF_8)); System.out.println(encrypt); String decrypt = RsaUtil.decryptByPublicKeyFromBase64(pubKey, encrypt); System.out.println(decrypt); diff --git a/mica-core/src/test/java/net/dreamlu/mica/test/utils/UUIDTest.java b/mica-core/src/test/java/net/dreamlu/mica/test/utils/UUIDTest.java index c1042f284..be68e46ab 100644 --- a/mica-core/src/test/java/net/dreamlu/mica/test/utils/UUIDTest.java +++ b/mica-core/src/test/java/net/dreamlu/mica/test/utils/UUIDTest.java @@ -1,7 +1,6 @@ package net.dreamlu.mica.test.utils; import net.dreamlu.mica.core.utils.StringUtil; -import net.dreamlu.mica.core.utils.ThreadUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -17,25 +16,63 @@ * @author L.cm */ class UUIDTest { + private static final int size = 1000_0000; - @Test - void test() { - ExecutorService service = Executors.newFixedThreadPool(10); - Set uuidSet = ConcurrentHashMap.newKeySet(); - int size = 100_0000; - for (int i = 0; i < size; i++) { - service.submit(() -> { - String nanoId = StringUtil.getNanoId(); - if (uuidSet.contains(nanoId)) { - System.out.println("-----------存在冲突-------"); - } else { - uuidSet.add(nanoId); - } - }); - } - ThreadUtil.sleep(TimeUnit.SECONDS, 5); - Assertions.assertEquals(size, uuidSet.size()); - service.shutdown(); - } + @Test + void test() throws InterruptedException { + ExecutorService service = Executors.newFixedThreadPool(100); + Set uuidSet = ConcurrentHashMap.newKeySet(); + for (int i = 0; i < size; i++) { + service.submit(() -> { + String nanoId = StringUtil.getNanoId(); + if (uuidSet.contains(nanoId)) { + System.out.println("-----------存在冲突-------"); + } else { + uuidSet.add(nanoId); + } + }); + } + service.awaitTermination(10, TimeUnit.SECONDS); + Assertions.assertEquals(size, uuidSet.size()); + service.shutdown(); + } + + @Test + void testUUID() throws InterruptedException { + ExecutorService service = Executors.newFixedThreadPool(100); + Set uuidSet = ConcurrentHashMap.newKeySet(); + for (int i = 0; i < size; i++) { + service.submit(() -> { + String uuid = StringUtil.getUUID(); + if (uuidSet.contains(uuid)) { + System.out.println("-----------存在冲突-------"); + } else { + uuidSet.add(uuid); + } + }); + } + service.awaitTermination(10, TimeUnit.SECONDS); + Assertions.assertEquals(size, uuidSet.size()); + service.shutdown(); + } + + @Test + void testID() throws InterruptedException { + ExecutorService service = Executors.newFixedThreadPool(100); + Set uuidSet = ConcurrentHashMap.newKeySet(); + for (int i = 0; i < size; i++) { + service.submit(() -> { + String uuid = StringUtil.getFastId(10); + if (uuidSet.contains(uuid)) { + System.out.println("-----------存在冲突-------"); + } else { + uuidSet.add(uuid); + } + }); + } + service.awaitTermination(10, TimeUnit.SECONDS); + Assertions.assertEquals(size, uuidSet.size()); + service.shutdown(); + } } diff --git a/mica-ip2region/src/main/java/net/dreamlu/mica/ip2region/utils/IpInfoUtil.java b/mica-ip2region/src/main/java/net/dreamlu/mica/ip2region/utils/IpInfoUtil.java index a0a5e24bc..4a2e5c62d 100644 --- a/mica-ip2region/src/main/java/net/dreamlu/mica/ip2region/utils/IpInfoUtil.java +++ b/mica-ip2region/src/main/java/net/dreamlu/mica/ip2region/utils/IpInfoUtil.java @@ -74,7 +74,6 @@ public static IpInfo toIpInfo(@Nullable String region) { * @param ipRecord ipRecord * @return IpInfo */ - @Nullable public static IpInfo toIpV6Info(String[] ipRecord) { IpInfo ipInfo = new IpInfo(); String info1 = ipRecord[0]; diff --git a/mica-lite/src/main/java/net/dreamlu/mica/lite/base/IController.java b/mica-lite/src/main/java/net/dreamlu/mica/lite/base/IController.java index 92f01fe2c..251b41294 100644 --- a/mica-lite/src/main/java/net/dreamlu/mica/lite/base/IController.java +++ b/mica-lite/src/main/java/net/dreamlu/mica/lite/base/IController.java @@ -19,7 +19,6 @@ import net.dreamlu.mica.core.result.IResultCode; import net.dreamlu.mica.core.result.R; import net.dreamlu.mica.core.result.SystemCode; -import net.dreamlu.mica.core.utils.Charsets; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; @@ -30,6 +29,7 @@ import org.springframework.web.util.UriUtils; import java.io.File; +import java.nio.charset.StandardCharsets; /** * 基础 controller @@ -160,7 +160,7 @@ default ResponseEntity download(File file, String fileName) { default ResponseEntity download(Resource resource, String fileName) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); - String encodeFileName = UriUtils.encode(fileName, Charsets.UTF_8); + String encodeFileName = UriUtils.encode(fileName, StandardCharsets.UTF_8); // 兼容各种浏览器下载: // https://blog.robotshell.org/2012/deal-with-http-header-encoding-for-file-download/ String disposition = "attachment;" + diff --git a/mica-lite/src/main/java/net/dreamlu/mica/lite/error/RestExceptionTranslator.java b/mica-lite/src/main/java/net/dreamlu/mica/lite/error/RestExceptionTranslator.java index 672455e7a..f2302985a 100644 --- a/mica-lite/src/main/java/net/dreamlu/mica/lite/error/RestExceptionTranslator.java +++ b/mica-lite/src/main/java/net/dreamlu/mica/lite/error/RestExceptionTranslator.java @@ -146,7 +146,7 @@ public R handleError(HttpMediaTypeNotSupportedException e) { @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE) public R handleError(HttpMediaTypeNotAcceptableException e) { - String message = e.getMessage() + " " + StringUtil.join(e.getSupportedMediaTypes()); + String message = e.getMessage() + ' ' + StringUtil.join(e.getSupportedMediaTypes()); log.error("不接受的媒体类型:{}", message); return R.fail(SystemCode.MEDIA_TYPE_NOT_SUPPORTED, message); } diff --git a/mica-lite/src/main/java/net/dreamlu/mica/lite/upload/MicaReactiveUploadConfiguration.java b/mica-lite/src/main/java/net/dreamlu/mica/lite/upload/MicaReactiveUploadConfiguration.java index 7ed64fb54..683743874 100644 --- a/mica-lite/src/main/java/net/dreamlu/mica/lite/upload/MicaReactiveUploadConfiguration.java +++ b/mica-lite/src/main/java/net/dreamlu/mica/lite/upload/MicaReactiveUploadConfiguration.java @@ -44,7 +44,7 @@ public class MicaReactiveUploadConfiguration implements WebFluxConfigurer { public void addResourceHandlers(ResourceHandlerRegistry registry) { String path = properties.getSavePath(); registry.addResourceHandler(properties.getUploadPathPattern()) - .addResourceLocations("file:" + path + "/"); + .addResourceLocations("file:" + path + '/'); } } diff --git a/mica-lite/src/main/java/net/dreamlu/mica/lite/upload/MicaServletUploadConfiguration.java b/mica-lite/src/main/java/net/dreamlu/mica/lite/upload/MicaServletUploadConfiguration.java index 9fa22a500..7e30321a4 100644 --- a/mica-lite/src/main/java/net/dreamlu/mica/lite/upload/MicaServletUploadConfiguration.java +++ b/mica-lite/src/main/java/net/dreamlu/mica/lite/upload/MicaServletUploadConfiguration.java @@ -44,7 +44,7 @@ public class MicaServletUploadConfiguration implements WebMvcConfigurer { public void addResourceHandlers(ResourceHandlerRegistry registry) { String path = properties.getSavePath(); registry.addResourceHandler(properties.getUploadPathPattern()) - .addResourceLocations("file:" + path + "/"); + .addResourceLocations("file:" + path + '/'); } } diff --git a/mica-qrcode/src/test/java/net/dreamlu/mica/qrcode/QrCodeTest.java b/mica-qrcode/src/test/java/net/dreamlu/mica/qrcode/QrCodeTest.java index fc87c807a..ff3f8593b 100644 --- a/mica-qrcode/src/test/java/net/dreamlu/mica/qrcode/QrCodeTest.java +++ b/mica-qrcode/src/test/java/net/dreamlu/mica/qrcode/QrCodeTest.java @@ -1,11 +1,11 @@ package net.dreamlu.mica.qrcode; -import net.dreamlu.mica.core.utils.Charsets; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.awt.*; import java.awt.image.BufferedImage; +import java.nio.charset.StandardCharsets; /** * qrcode 测试 @@ -26,7 +26,7 @@ public static void main(String[] args) { .size(512) // 默认 512,可以不设置 .backGroundColor(Color.WHITE) // 默认白色,可以不设置 .foreGroundColor(Color.BLACK) // 默认黑色,可以不设置 - .encode(Charsets.UTF_8) // 默认 UTF_8,可以不设置 + .encode(StandardCharsets.UTF_8) // 默认 UTF_8,可以不设置 .imageFormat("png") // 默认 png,可以不设置 .deleteMargin(true) // 删除白边,默认为 true,可以不设置 .logo("/Users/lcm/Desktop/mica.png") // 设置二维码 logo