mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add Base64.isBase64
This commit is contained in:
parent
f68a73538e
commit
dfa10be599
@ -12,6 +12,7 @@
|
||||
* 【core 】 MapBuilder增加put方法(pr#367@Gitee)
|
||||
* 【core 】 StrUtil.insert支持负数index
|
||||
* 【core 】 Calculator类支持取模运算(issue#I40DUW@Gitee)
|
||||
* 【core 】 增加Base64.isBase64方法(issue#1710@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复FileUtil.normalize处理上级路径的问题(issue#I3YPEH@Gitee)
|
||||
|
@ -314,4 +314,43 @@ public class Base64 {
|
||||
public static byte[] decode(byte[] in) {
|
||||
return Base64Decoder.decode(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为Base64
|
||||
*
|
||||
* @param base64 Base64的bytes
|
||||
* @return 是否为Base64
|
||||
* @since 5.7.5
|
||||
*/
|
||||
public static boolean isBase64(String base64){
|
||||
return isBase64(StrUtil.utf8Bytes(base64));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为Base64
|
||||
*
|
||||
* @param base64Bytes Base64的bytes
|
||||
* @return 是否为Base64
|
||||
* @since 5.7.5
|
||||
*/
|
||||
public static boolean isBase64(byte[] base64Bytes){
|
||||
for (byte base64Byte : base64Bytes) {
|
||||
if (false == (Base64Decoder.isBase64Code(base64Byte) || isWhiteSpace(base64Byte))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isWhiteSpace(byte byteToCheck) {
|
||||
switch (byteToCheck) {
|
||||
case ' ' :
|
||||
case '\n' :
|
||||
case '\r' :
|
||||
case '\t' :
|
||||
return true;
|
||||
default :
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
package cn.hutool.core.codec;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Base64解码实现
|
||||
*
|
||||
@ -130,6 +130,17 @@ public class Base64Decoder {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 给定的字符是否为Base64字符
|
||||
*
|
||||
* @param octet 被检查的字符
|
||||
* @return 是否为Base64字符
|
||||
* @since 5.7.5
|
||||
*/
|
||||
public static boolean isBase64Code(byte octet) {
|
||||
return octet == '=' || (octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------- Private start
|
||||
/**
|
||||
* 获取下一个有效的byte字符
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.codec;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@ -13,6 +14,11 @@ import org.junit.Test;
|
||||
*/
|
||||
public class Base64Test {
|
||||
|
||||
@Test
|
||||
public void isBase64Test(){
|
||||
Assert.assertTrue(Base64.isBase64(Base64.encode(RandomUtil.randomString(100))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeAndDecodeTest() {
|
||||
String a = "伦家是一个非常长的字符串66";
|
||||
|
Loading…
x
Reference in New Issue
Block a user