简体   繁体   English

Java Bytebuffer完全填满

[英]Java Bytebuffer Filling up completely

I am currently using the Java ByteBuffer 我目前正在使用Java ByteBuffer

    ByteBuffer batch = ByteBuffer.allocate(tuple_size * batch_size ) ;
    int pos = 0;
    int sent = 0;
    while ( sent++ < batch_size) {
            Event event = (Event) it.next();
            batch.put(event.getData(), pos, tuple_size);
            pos += tuple_size;

        }
    return batch.array();

Currently, batch_size is set to 2. My issue is that on the second round, I get an IndexOutofBoundsException which I cannot explain given that, printing out the follwoing details: 目前,batch_size设置为2.我的问题是,在第二轮,我得到一个IndexOutofBoundsException,我无法解释,因为打印出以下详细信息:

       System.out.println(pos + " " +  batch.capacity() +  " " + batch.position() + " " +  batch.remaining()); 

I get: 0 200 0 200 (round 0) 我得到:0 200 0 200(第0轮)

100 200 100 100 (round 1) 100 200 100 100(第1轮)

which is what one would expect. 这是人们所期望的。 Now, based on the documentation, it seems that the bound checks do hold: 现在,根据文档,似乎绑定检查确实存在:

 offset - The offset within the array of the first byte to be read; must be non-negative and no larger than array.length
 length - The number of bytes to be read from the given array; must be non-negative and no larger than array.length - offset

How do I completely fill up the buffer? 我如何完全填满缓冲区? (whilst keeping the underlying buffer to have length tuple_size * batch_size?) (同时保持底层缓冲区的长度为tuple_size * batch_size?)

I think you don't need the pos variable. 我认为你不需要pos变量。 The put method is trying to read into event.getData() at position pos , while I think you want to read event.getData() from position 0. You can simply use batch.put(event.getData()) to append the whole content of the array into the buffer. put方法试图在位置pos读入event.getData() ,而我想你想从位置0读取event.getData() 。你可以简单地使用batch.put(event.getData())来追加数组的全部内容放入缓冲区。

It is not apparent from your question if tuple_size is big enough for event.getData(). 从你的问题来看,tuple_size对于event.getData()是否足够大是不明显的。 If not, that would result in your IndexOutofBoundsException. 如果不是,那将导致您的IndexOutofBoundsException。 Perhaps it's off by one? 也许是一个人呢?

Another possibility is that your iterator it only contains one element. 另一种可能性是,你的迭代it仅包含一个元素。

Edit: According to the documentation, you should get a BufferOverflowException if your buffer runs out of space. 编辑:根据文档,如果缓冲区空间不足,应该得到BufferOverflowException。 Quote from the documentation: 从文档中引用:

This method transfers bytes into this buffer from the given source array. 此方法将字节从给定的源数组传输到此缓冲区。 If there are more bytes to be copied from the array than remain in this buffer, that is, if length > remaining(), then no bytes are transferred and a BufferOverflowException is thrown. 如果要从数组中复制的字节多于保留在此缓冲区中的字节数,即,如果length> remaining(),则不传输任何字节并抛出BufferOverflowException。

This points at that your problem isn't what you expect. 这表明你的问题不是你所期望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM