public static boolean isValidUtf8(byte[] b,int aMaxCount){
int lLen=b.length,lCharCount=0;
for(int i=0;i<lLen && lCharCount<aMaxCount;++lCharCount){
byte lByte=b[i++];//to fast operation, ++ now, ready for the following for(;;)
if(lByte>=0) continue;//>=0 is normal ascii
if(lByte<(byte)0xc0 || lB yte>(byte)0xfd) return false;
int lCount=lByte>(byte)0xfc?5:lByte>(byte)0xf8?4
:lByte>(byte)0xf0?3:lByte>(byte)0xe0?2:1;
if(i+lCount>lLen) return false;
for(int j=0;j<lCount;++j,++i) if(b[i]>=(byte)0xc0) return false;
}
return true;
}
收藏的一个判断UTF-8的代码
Java如何获得文件编码格式
最新推荐文章于 2025-04-12 19:09:55 发布
本文提供了一个用于验证 Java 字符串是否符合 UTF-8 编码规范的算法实现,通过遍历输入字节数组并判断每个字节是否在合法范围,确保字符串正确编码。
4994

被折叠的 条评论
为什么被折叠?



