mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add Base64.isBase64
This commit is contained in:
@@ -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";
|
||||
|
Reference in New Issue
Block a user