我目前使用的是Java字節緩衝區的Java字節緩衝區填滿完全
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異常,打印出follwoing細節:
System.out.println(pos + " " + batch.capacity() + " " + batch.position() + " " + batch.remaining());
我得到: 0 200 0 200(圓0)
100 200 100 100(第1輪)
這是人們所期望的。現在,根據文檔,似乎綁定檢查確實可以保留:
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?)
閱讀文檔,這非常合理。這不是我期望的相對投入操作的工作方式。 – Buhb
不幸的是,數組沒有相對的'put()'函數。如果你需要這樣的話,你必須首先調用'position()',然後調用'put()'。 – Flavio
完美。謝謝。正如Buhb所說,我並不期望以這種方式工作。但寧可抵消是相對於ByteBuffer。 – user1018513