什么是 CharsetDecoder.decode(ByteBuffer, CharBuffer, endOfInput)

2024-02-28

我有一个问题CharsetDecoder class.

第一个代码示例(有效):

    final CharsetDecoder dec = Charset.forName("UTF-8").newDecoder();
    final ByteBuffer b = ByteBuffer.allocate(3);
    final byte[] tab = new byte[]{(byte)-30, (byte)-126, (byte)-84}; //char €
    for (int i=0; i<tab.length; i++){
        b.put(tab, i, 1);
    }
    try {
        b.flip();
        System.out.println("a" + dec.decode(b).toString() + "a");
    } catch (CharacterCodingException e1) {
        e1.printStackTrace();
    }

结果是a€a

但是当我执行这段代码时:

    final CharsetDecoder dec = Charset.forName("UTF-8").newDecoder();
    final CharBuffer chars = CharBuffer.allocate(3);
    final byte[] tab = new byte[]{(byte)-30, (byte)-126, (byte)-84}; //char €
    for (int i=0; i<tab.length; i++){
        ByteBuffer buffer = ByteBuffer.wrap(tab, i, 1);
        dec.decode(buffer, chars, i == 2);
    }
    dec.flush(chars);
    System.out.println("a" + chars.toString() + "a");

结果是a

为什么结果不一样?

使用方法decode(ByteBuffer, CharBuffer, endOfInput)班级的CharsetDecoder为了检索结果a€a ?

- 编辑 -

所以我用 Jesper 的代码做到了这一点。它并不完美,但可以与step= 1、2 和 3

final CharsetDecoder dec = Charset.forName("UTF-8").newDecoder();
    final CharBuffer chars = CharBuffer.allocate(6);
    final byte[] tab = new byte[]{(byte)97, (byte)-30, (byte)-126, (byte)-84, (byte)97, (byte)97}; //char €

    final ByteBuffer buffer = ByteBuffer.allocate(10);

    final int step = 3;
    for (int i = 0; i < tab.length; i++) {
        // Add the next byte to the buffer
        buffer.put(tab, i, step);
        i+=step-1;

        // Remember the current position
        final int pos = buffer.position();
        int l=chars.position();

        // Try to decode
        buffer.flip();
        final CoderResult result = dec.decode(buffer, chars, i >= tab.length -1);
        System.out.println(result);

        if (result.isUnderflow() && chars.position() == l) {
            // Underflow, prepare the buffer for more writing
            buffer.position(pos);
        }else{
            if (buffer.position() == buffer.limit()){
                //ByteBuffer decoded
                buffer.clear();
                buffer.position(0);
            }else{
                //a part of ByteBuffer is decoded. We keep only bytes which are not decoded
                final byte[] b = buffer.array();
                final int f = buffer.position();
                final int g = buffer.limit() - buffer.position();
                buffer.clear();
                buffer.position(0);
                buffer.put(b, f, g);
            }
        }
        buffer.limit(buffer.capacity());
    }

    dec.flush(chars);
    chars.flip();

    System.out.println(chars.toString());

方法decode(ByteBuffer, CharBuffer, boolean) http://docs.oracle.com/javase/8/docs/api/java/nio/charset/CharsetDecoder.html#decode-java.nio.ByteBuffer-java.nio.CharBuffer-boolean-返回一个结果,但你忽略了结果。如果在第二个代码片段中打印结果:

for (int i = 0; i < tab.length; i++) {
    ByteBuffer buffer = ByteBuffer.wrap(tab, i, 1);
    System.out.println(dec.decode(buffer, chars, i == 2));
}

你会看到这个输出:

UNDERFLOW
MALFORMED[1]
MALFORMED[1]
a   a

显然,如果您在字符中间开始解码,它就无法正常工作。解码器期望它读取的第一件事是有效 UTF-8 序列的开头。

edit- 当解码器报告时UNDERFLOW,它希望您向输入缓冲区添加更多数据,然后尝试调用decode()再次,但您必须重新向其提供您尝试解码的 UTF-8 序列开头的数据。您无法在 UTF-8 序列的中间继续。

这是一个有效的版本,从tab在循环的每次迭代中:

final CharsetDecoder dec = Charset.forName("UTF-8").newDecoder();
final CharBuffer chars = CharBuffer.allocate(3);
final byte[] tab = new byte[]{(byte) -30, (byte) -126, (byte) -84}; //char €

final ByteBuffer buffer = ByteBuffer.allocate(10);

for (int i = 0; i < tab.length; i++) {
    // Add the next byte to the buffer
    buffer.put(tab[i]);

    // Remember the current position
    final int pos = buffer.position();

    // Try to decode
    buffer.flip();
    final CoderResult result = dec.decode(buffer, chars, i == 2);
    System.out.println(result);

    if (result.isUnderflow()) {
        // Underflow, prepare the buffer for more writing
        buffer.limit(buffer.capacity());
        buffer.position(pos);
    }
}

dec.flush(chars);
chars.flip();

System.out.println("a" + chars.toString() + "a");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

什么是 CharsetDecoder.decode(ByteBuffer, CharBuffer, endOfInput) 的相关文章

随机推荐