如何按字节大小截断包含表情符号的字符串

How to truncate String contain emoji by byte size

我想将 UTF-8 字符集大小的字符串限制为 30 个字节,我找到了解决方案 this

所以我创建了一个基于此的方法

public static String truncateTextByByteLimit(String message, int byteLimit) {
    String result = "";
    try {
        Charset utf8Charset = Charset.forName("UTF-8");
        CharsetDecoder cd = utf8Charset.newDecoder();
        byte[] utf8Bytes = message.getBytes(utf8Charset);
        System.out.println("check message: " + message + " /length: " +message.length()+ " //byte length: " + utf8Bytes.length + "/limit: " + byteLimit + " /codePoint: " +message.codePointCount(0, message.length()));
        ByteBuffer bb = ByteBuffer.wrap(utf8Bytes, 0, byteLimit);
        CharBuffer cb = CharBuffer.allocate(byteLimit);
        // Ignore an incomplete character
        cd.onMalformedInput(CodingErrorAction.IGNORE);
        cd.decode(bb, cb, true);
        cd.flush(cb);
        result = new String(cb.array(), 0, cb.position());
        if (result.length()<=0) {
            return truncateTextByByteLimit(message, (byteLimit+1));
        } else {
            return result;
        }
    } catch (Exception e) {
        e.printStackTrace();

        return message;
    }
}

问题出在我用表情符号测试 String 时,如下所示: System.out.println(truncateTextByByteLimit("let's \uD83D\uDE09", 30));

显示错误

java.lang.IndexOutOfBoundsException
at java.nio.ByteBuffer.wrap(ByteBuffer.java:371)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

并且我的调试消息显示 check message: let's /length: 8 //byte length: 10/limit: 30 /codePoint: 7

当我使用相同的消息和小于或等于 10 的 byteLimit 进行测试时,它可以正常工作...

所以我不明白为什么会显示java.lang.IndexOutOfBoundsException

ByteBuffer#wrap has a limitation 关于允许的长度。

The length of the subarray to be used; must be non-negative and no larger than array.length - offset. The new buffer's limit will be set to offset + length.

为了解决这个问题,您需要取两个长度中的较小者 - 要么是您的绝对最大值 byteLimit,要么是 utf8Bytes 数组的大小。

ByteBuffer.wrap(utf8Bytes, 0, Math.min(utf8Bytes.length, byteLimit));