Mutf8.java 文件源码

java
阅读 22 收藏 0 点赞 0 评论 0

项目:JCL 作者:
/**
 * Returns the number of bytes the modified UTF8 representation of 's' would take.
 */
private static long countBytes(String s, boolean shortLength) throws UTFDataFormatException {
    long result = 0;
    final int length = s.length();
    for (int i = 0; i < length; ++i) {
        char ch = s.charAt(i);
        if (ch != 0 && ch <= 127) { // U+0000 uses two bytes.
            ++result;
        } else if (ch <= 2047) {
            result += 2;
        } else {
            result += 3;
        }
        if (shortLength && result > 65535) {
            throw new UTFDataFormatException("String more than 65535 UTF bytes long");
        }
    }
    return result;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号