diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/Base64Util.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/Base64Util.java index 6f349b0..edf84ff 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/Base64Util.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/Base64Util.java @@ -15,12 +15,19 @@ */ package org.springblade.core.tool.utils; +import java.nio.charset.Charset; +import java.util.Base64; + /** * Base64工具 * * @author L.cm */ -public class Base64Util extends org.springframework.util.Base64Utils { +public class Base64Util { + public static final Base64.Encoder ENCODER = Base64.getEncoder(); + public static final Base64.Encoder URL_ENCODER = Base64.getUrlEncoder(); + public static final Base64.Decoder DECODER = Base64.getDecoder(); + public static final Base64.Decoder URL_DECODER = Base64.getUrlDecoder(); /** * 编码 @@ -29,7 +36,7 @@ public class Base64Util extends org.springframework.util.Base64Utils { * @return {String} */ public static String encode(String value) { - return Base64Util.encode(value, Charsets.UTF_8); + return encode(value, Charsets.UTF_8); } /** @@ -39,9 +46,9 @@ public class Base64Util extends org.springframework.util.Base64Utils { * @param charset 字符集 * @return {String} */ - public static String encode(String value, java.nio.charset.Charset charset) { + public static String encode(String value, Charset charset) { byte[] val = value.getBytes(charset); - return new String(Base64Util.encode(val), charset); + return new String(encode(val), charset); } /** @@ -51,7 +58,7 @@ public class Base64Util extends org.springframework.util.Base64Utils { * @return {String} */ public static String encodeUrlSafe(String value) { - return Base64Util.encodeUrlSafe(value, Charsets.UTF_8); + return encodeUrlSafe(value, Charsets.UTF_8); } /** @@ -61,9 +68,9 @@ public class Base64Util extends org.springframework.util.Base64Utils { * @param charset 字符集 * @return {String} */ - public static String encodeUrlSafe(String value, java.nio.charset.Charset charset) { + public static String encodeUrlSafe(String value, Charset charset) { byte[] val = value.getBytes(charset); - return new String(Base64Util.encodeUrlSafe(val), charset); + return new String(encodeUrlSafe(val), charset); } /** @@ -73,7 +80,7 @@ public class Base64Util extends org.springframework.util.Base64Utils { * @return {String} */ public static String decode(String value) { - return Base64Util.decode(value, Charsets.UTF_8); + return decode(value, Charsets.UTF_8); } /** @@ -83,9 +90,9 @@ public class Base64Util extends org.springframework.util.Base64Utils { * @param charset 字符集 * @return {String} */ - public static String decode(String value, java.nio.charset.Charset charset) { + public static String decode(String value, Charset charset) { byte[] val = value.getBytes(charset); - byte[] decodedValue = Base64Util.decode(val); + byte[] decodedValue = decode(val); return new String(decodedValue, charset); } @@ -96,7 +103,7 @@ public class Base64Util extends org.springframework.util.Base64Utils { * @return {String} */ public static String decodeUrlSafe(String value) { - return Base64Util.decodeUrlSafe(value, Charsets.UTF_8); + return decodeUrlSafe(value, Charsets.UTF_8); } /** @@ -106,9 +113,113 @@ public class Base64Util extends org.springframework.util.Base64Utils { * @param charset 字符集 * @return {String} */ - public static String decodeUrlSafe(String value, java.nio.charset.Charset charset) { + public static String decodeUrlSafe(String value, Charset charset) { byte[] val = value.getBytes(charset); - byte[] decodedValue = Base64Util.decodeUrlSafe(val); + byte[] decodedValue = decodeUrlSafe(val); return new String(decodedValue, charset); } + + /** + * Base64-encode the given byte array. + * + * @param src the original byte array + * @return the encoded byte array + */ + public static byte[] encode(byte[] src) { + if (src.length == 0) { + return src; + } + return ENCODER.encode(src); + } + + /** + * Base64-decode the given byte array. + * + * @param src the encoded byte array + * @return the original byte array + */ + public static byte[] decode(byte[] src) { + if (src.length == 0) { + return src; + } + return DECODER.decode(src); + } + + /** + * Base64-encode the given byte array using the RFC 4648 + * "URL and Filename Safe Alphabet". + * + * @param src the original byte array + * @return the encoded byte array + */ + public static byte[] encodeUrlSafe(byte[] src) { + if (src.length == 0) { + return src; + } + return URL_ENCODER.encode(src); + } + + /** + * Base64-decode the given byte array using the RFC 4648 + * "URL and Filename Safe Alphabet". + * + * @param src the encoded byte array + * @return the original byte array + * @since 4.2.4 + */ + public static byte[] decodeUrlSafe(byte[] src) { + if (src.length == 0) { + return src; + } + return URL_DECODER.decode(src); + } + + /** + * Base64-encode the given byte array to a String. + * + * @param src the original byte array + * @return the encoded byte array as a UTF-8 String + */ + public static String encodeToString(byte[] src) { + if (src.length == 0) { + return ""; + } + return new String(encode(src), Charsets.UTF_8); + } + + /** + * Base64-decode the given byte array from an UTF-8 String. + * + * @param src the encoded UTF-8 String + * @return the original byte array + */ + public static byte[] decodeFromString(String src) { + if (src.isEmpty()) { + return new byte[0]; + } + return decode(src.getBytes(Charsets.UTF_8)); + } + + /** + * Base64-encode the given byte array to a String using the RFC 4648 + * "URL and Filename Safe Alphabet". + * + * @param src the original byte array + * @return the encoded byte array as a UTF-8 String + */ + public static String encodeToUrlSafeString(byte[] src) { + return new String(encodeUrlSafe(src), Charsets.UTF_8); + } + + /** + * Base64-decode the given byte array from an UTF-8 String using the RFC 4648 + * "URL and Filename Safe Alphabet". + * + * @param src the encoded UTF-8 String + * @return the original byte array + */ + public static byte[] decodeFromUrlSafeString(String src) { + return decodeUrlSafe(src.getBytes(Charsets.UTF_8)); + } + }