change Base64 impl

This commit is contained in:
Looly 2022-03-18 01:16:42 +08:00
parent 7b4fd66d81
commit f52ec39d44
3 changed files with 20 additions and 24 deletions

View File

@ -159,7 +159,7 @@ public class Base64 {
* @since 3.0.6 * @since 3.0.6
*/ */
public static String encodeUrlSafe(byte[] source) { public static String encodeUrlSafe(byte[] source) {
return java.util.Base64.getUrlEncoder().encodeToString(source); return java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(source);
} }
/** /**

View File

@ -6,7 +6,8 @@ import cn.hutool.core.util.StrUtil;
import java.nio.charset.Charset; import java.nio.charset.Charset;
/** /**
* Base64编码 * Base64编码<br>
* TODO 6.x移除此类使用JDK自身
* *
* @author looly * @author looly
* @since 3.2.0 * @since 3.2.0
@ -14,7 +15,9 @@ import java.nio.charset.Charset;
public class Base64Encoder { public class Base64Encoder {
private static final Charset DEFAULT_CHARSET = CharsetUtil.CHARSET_UTF_8; private static final Charset DEFAULT_CHARSET = CharsetUtil.CHARSET_UTF_8;
/** 标准编码表 */ /**
* 标准编码表
*/
private static final byte[] STANDARD_ENCODE_TABLE = { // private static final byte[] STANDARD_ENCODE_TABLE = { //
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', //
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', //
@ -25,7 +28,9 @@ public class Base64Encoder{
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 'w', 'x', 'y', 'z', '0', '1', '2', '3', //
'4', '5', '6', '7', '8', '9', '+', '/' // '4', '5', '6', '7', '8', '9', '+', '/' //
}; };
/** URL安全的编码表将 + 和 / 替换为 - 和 _ */ /**
* URL安全的编码表 + / 替换为 - _
*/
private static final byte[] URL_SAFE_ENCODE_TABLE = { // private static final byte[] URL_SAFE_ENCODE_TABLE = { //
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', //
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', //
@ -38,6 +43,7 @@ public class Base64Encoder{
}; };
// -------------------------------------------------------------------- encode // -------------------------------------------------------------------- encode
/** /**
* 编码为Base64非URL安全的 * 编码为Base64非URL安全的
* *

View File

@ -71,16 +71,6 @@ public class Base64Test {
Assert.assertEquals(a, decodeStr); Assert.assertEquals(a, decodeStr);
} }
@Test
public void urlSafeEncodeAndDecodeTest() {
String a = "广州伦家需要安全感55";
String encode = StrUtil.utf8Str(Base64.encodeUrlSafe(StrUtil.utf8Bytes(a), false));
Assert.assertEquals("5bm_5bee5Lym5a626ZyA6KaB5a6J5YWo5oSfNTU", encode);
String decodeStr = Base64.decodeStr(encode);
Assert.assertEquals(a, decodeStr);
}
@Test @Test
public void encodeAndDecodeGbkTest(){ public void encodeAndDecodeGbkTest(){
String orderDescription = "订购成功立即生效30天内可观看专区中除单独计费影片外的所有内容到期自动取消。"; String orderDescription = "订购成功立即生效30天内可观看专区中除单独计费影片外的所有内容到期自动取消。";