This commit is contained in:
Looly 2021-06-05 11:16:14 +08:00
parent e7fb9759f3
commit f441b319da
2 changed files with 16 additions and 6 deletions

View File

@ -132,7 +132,7 @@ public class Base64Encoder {
*
* @param arr 被编码的数组
* @param isMultiLine 在76个char之后是CRLF还是EOF
* @param isUrlSafe 是否使用URL安全字符一般为{@code false}
* @param isUrlSafe 是否使用URL安全字符在URL Safe模式下=为URL中的关键字符不需要补充空余的byte位要去掉一般为{@code false}
* @return 编码后的bytes
*/
public static byte[] encode(byte[] arr, boolean isMultiLine, boolean isUrlSafe) {

View File

@ -7,18 +7,28 @@ import org.junit.Test;
/**
* Base64单元测试
*
*
* @author looly
*
*/
public class Base64Test {
@Test
public void encodeAndDecodeTest() {
String a = "伦家是一个非常长的字符串66";
String encode = Base64.encode(a);
Assert.assertEquals("5Lym5a625piv5LiA5Liq6Z2e5bi46ZW/55qE5a2X56ym5LiyNjY=", encode);
String decodeStr = Base64.decodeStr(encode);
Assert.assertEquals(a, decodeStr);
}
@Test
public void encodeAndDecodeWithoutPaddingTest() {
String a = "伦家是一个非常长的字符串66";
String encode = Base64.encodeWithoutPadding(StrUtil.utf8Bytes(a));
Assert.assertEquals("5Lym5a625piv5LiA5Liq6Z2e5bi46ZW/55qE5a2X56ym5LiyNjY", encode);
String decodeStr = Base64.decodeStr(encode);
Assert.assertEquals(a, decodeStr);
}
@ -28,7 +38,7 @@ public class Base64Test {
String a = "a61a5db5a67c01445ca2-HZ20181120172058/pdf/中国电信影像云单体网关Docker版-V1.2.pdf";
String encode = Base64.encode(a, CharsetUtil.UTF_8);
Assert.assertEquals("YTYxYTVkYjVhNjdjMDE0NDVjYTItSFoyMDE4MTEyMDE3MjA1OC9wZGYv5Lit5Zu955S15L+h5b2x5YOP5LqR5Y2V5L2T572R5YWzRG9ja2Vy54mILVYxLjIucGRm", encode);
String decodeStr = Base64.decodeStr(encode, CharsetUtil.UTF_8);
Assert.assertEquals(a, decodeStr);
}
@ -42,7 +52,7 @@ public class Base64Test {
String decodeStr = Base64.decodeStr(encode);
Assert.assertEquals(a, decodeStr);
}
@Test
public void urlSafeEncodeAndDecodeTest() {
String a = "广州伦家需要安全感55";