分享按钮

Java如何获得文件编码格式

 

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的代码

  

 

posted @ 2011-07-20 16:22  草莓在努力  阅读(418)  评论(0编辑  收藏  举报