mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
change Base64 impl
This commit is contained in:
parent
7b4fd66d81
commit
f52ec39d44
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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安全的
|
||||||
*
|
*
|
||||||
|
@ -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天内可观看专区中除单独计费影片外的所有内容,到期自动取消。";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user