mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix isBase64
This commit is contained in:
parent
74c6dca648
commit
0f96a95f2c
@ -22,6 +22,7 @@
|
|||||||
* 【core 】 Week增加of重载,支持DayOfWek(pr#1872@Github)
|
* 【core 】 Week增加of重载,支持DayOfWek(pr#1872@Github)
|
||||||
* 【poi 】 优化read,避免多次创建CopyOptions(issue#1875@Github)
|
* 【poi 】 优化read,避免多次创建CopyOptions(issue#1875@Github)
|
||||||
* 【core 】 优化CsvReader,实现可控遍历(pr#1873@Github)
|
* 【core 】 优化CsvReader,实现可控遍历(pr#1873@Github)
|
||||||
|
* 【core 】 优化Base64.isBase64判断(pr#1879@Github)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【http 】 修复HttpCookie设置cookies的方法,不符合RFC6265规范问题(pr#418@Gitee)
|
* 【http 】 修复HttpCookie设置cookies的方法,不符合RFC6265规范问题(pr#418@Gitee)
|
||||||
|
@ -322,12 +322,12 @@ public class Base64 {
|
|||||||
* @return 是否为Base64
|
* @return 是否为Base64
|
||||||
* @since 5.7.5
|
* @since 5.7.5
|
||||||
*/
|
*/
|
||||||
public static boolean isBase64(CharSequence base64){
|
public static boolean isBase64(CharSequence base64) {
|
||||||
if (base64 == null || base64.length() < 2) {
|
if (base64 == null || base64.length() < 2) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] bytes = StrUtil.utf8Bytes(base64);
|
final byte[] bytes = StrUtil.utf8Bytes(base64);
|
||||||
|
|
||||||
if (bytes.length != base64.length()) {
|
if (bytes.length != base64.length()) {
|
||||||
// 如果长度不相等,说明存在双字节字符,肯定不是Base64,直接返回false
|
// 如果长度不相等,说明存在双字节字符,肯定不是Base64,直接返回false
|
||||||
@ -344,15 +344,15 @@ public class Base64 {
|
|||||||
* @return 是否为Base64
|
* @return 是否为Base64
|
||||||
* @since 5.7.5
|
* @since 5.7.5
|
||||||
*/
|
*/
|
||||||
public static boolean isBase64(byte[] base64Bytes){
|
public static boolean isBase64(byte[] base64Bytes) {
|
||||||
boolean hasPadding = false;
|
boolean hasPadding = false;
|
||||||
for (byte base64Byte : base64Bytes) {
|
for (byte base64Byte : base64Bytes) {
|
||||||
if(hasPadding){
|
if (hasPadding) {
|
||||||
if('=' != base64Byte){
|
if ('=' != base64Byte) {
|
||||||
// 前一个字符是'=',则后边的字符都必须是'=',即'='只能都位于结尾
|
// 前一个字符是'=',则后边的字符都必须是'=',即'='只能都位于结尾
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if('=' == base64Byte){
|
} else if ('=' == base64Byte) {
|
||||||
// 发现'=' 标记之
|
// 发现'=' 标记之
|
||||||
hasPadding = true;
|
hasPadding = true;
|
||||||
} else if (false == (Base64Decoder.isBase64Code(base64Byte) || isWhiteSpace(base64Byte))) {
|
} else if (false == (Base64Decoder.isBase64Code(base64Byte) || isWhiteSpace(base64Byte))) {
|
||||||
@ -364,12 +364,12 @@ public class Base64 {
|
|||||||
|
|
||||||
private static boolean isWhiteSpace(byte byteToCheck) {
|
private static boolean isWhiteSpace(byte byteToCheck) {
|
||||||
switch (byteToCheck) {
|
switch (byteToCheck) {
|
||||||
case ' ' :
|
case ' ':
|
||||||
case '\n' :
|
case '\n':
|
||||||
case '\r' :
|
case '\r':
|
||||||
case '\t' :
|
case '\t':
|
||||||
return true;
|
return true;
|
||||||
default :
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user