我目前正在使用 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();
目前,batch_size 设置为 2。我的问题是,在第二轮中,我得到一个 IndexOutofBoundsException,鉴于此我无法解释,打印出以下详细信息:
System.out.println(pos + " " + batch.capacity() + " " + batch.position() + " " + batch.remaining());
我得到:0 200 0 200(第 0 轮)
100 200 100 100(第一轮)
这是人们所期望的。现在,根据文档,绑定检查似乎确实成立:
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
如何完全填满缓冲区?(同时保持底层缓冲区的长度为 tuple_size * batch_size?)