从ByteBuffer中删除前n个字节

时间:2013-08-03 10:37:00

标签: java bytebuffer

如何在不更改或降低容量的情况下从ByteBuffer中删除第一个 n 字节数?结果应该是第0个字节是 n + 1 字节。 Java中是否有更好的数据类型来执行此类操作?

4 个答案:

答案 0 :(得分:2)

我认为您正在寻找的方法是the ByteBuffer's compact() method

即使文档说:

"缓冲区当前位置及其限制之间的字节(如果有)被复制到缓冲区的开头。也就是说,索引p = position()处的字节被复制到索引0,索引p + 1处的字节被复制到索引1,依此类推,直到索引limit() - 1处的字节被复制到索引n = limit() - 1 - p。然后将缓冲区的位置设置为n + 1,并将其限制设置为其容量。"

我不确定这种方法是否真的如此,因为当我调试它时,方法就像buffer.limit = buffer.capacity一样。

答案 1 :(得分:1)

你的意思是将所有元素转移到缓冲区的开头?像这样:

    int n = 4;
    //allocate a buffer of capacity 10 
    ByteBuffer b = ByteBuffer.allocate(10); 

    // add data to buffer
    for (int i = 0; i < b.limit(); i++) {
        b.put((byte) i);
    }

    // print buffer
    for (int i = 0; i < b.limit(); i++) {
        System.out.print(b.get(i) + " ");
    }

    //shift left the elements from the buffer
    //add zeros to the end
    for (int i = n; i < b.limit() + n; i++) {
        if (i < b.limit()) {
            b.put(i - n, b.get(i));
        } else {
            b.put(i - n, (byte) 0);
        }
    }
    //print buffer again
    System.out.println();
    for (int i = 0; i < b.limit(); i++) {
        System.out.print(b.get(i) + " ");
    }

对于n = 4,它将打印:

0 1 2 3 4 5 6 7 8 9 
4 5 6 7 8 9 0 0 0 0

答案 2 :(得分:1)

您可以尝试这样的事情:

public void removeBytesFromStart(ByteBuffer bf, int n) {
    int index = 0;
    for(int i = n; i < bf.position(); i++) {
        bf.put(index++, bf.get(i));
        bf.put(i, (byte)0);
    }
    bf.position(index);
}

或类似的东西:

public void removeBytesFromStart2(ByteBuffer bf, int n) {
    int index = 0;
    for(int i = n; i < bf.limit(); i++) {
        bf.put(index++, bf.get(i));
        bf.put(i, (byte)0);
    }
    bf.position(bf.position()-n);
}

这使用get类的绝对putByteBuffer方法,并将position设置为下一个写位置。

请注意,绝对put方法是可选的,这意味着扩展抽象类ByteBuffer的类可能无法为其提供实现,例如它可能会抛出ReadOnlyBufferException

是否选择循环直到position或直到limit取决于您如何使用缓冲区,例如,如果您手动设置position,则可能需要使用循环直到{{ 1}}。如果不这样做,那么循环直到limit足够且效率更高。

以下是一些测试:

position

答案 3 :(得分:0)

使用紧凑的方法。 E.g:

    ByteBuffer b = ByteBuffer.allocate(32);
    b.put("hello,world".getBytes());
    b.position(6);      
    b.compact();
    System.out.println(new String(b.array()));
相关问题