HBASE-14011 - MultiByteBuffer position based reads does not work correctly

(Ram)
This commit is contained in:
ramkrishna 2015-07-03 07:58:48 +05:30
parent 17703f0361
commit 1b75fd2bd6
2 changed files with 38 additions and 2 deletions

View File

@ -151,7 +151,7 @@ public class MultiByteBuffer {
*/
private int getItemIndex(int elemIndex) {
int index = 1;
while (elemIndex > this.itemBeginPos[index]) {
while (elemIndex >= this.itemBeginPos[index]) {
index++;
if (index == this.itemBeginPos.length) {
throw new IndexOutOfBoundsException();
@ -166,7 +166,7 @@ public class MultiByteBuffer {
*/
private int getItemIndexFromCurItemIndex(int elemIndex) {
int index = this.curItemIndex;
while (elemIndex < this.itemBeginPos[index]) {
while (elemIndex >= this.itemBeginPos[index]) {
index++;
if (index == this.itemBeginPos.length) {
throw new IndexOutOfBoundsException();

View File

@ -277,4 +277,40 @@ public class TestMultiByteBuffer {
assertEquals(l1, dup.getLong());
assertEquals(l2, dup.getLong());
}
@Test
public void testGetWithPosOnMultiBuffers() throws IOException {
byte[] b = new byte[4];
byte[] b1 = new byte[4];
ByteBuffer bb1 = ByteBuffer.wrap(b);
ByteBuffer bb2 = ByteBuffer.wrap(b1);
MultiByteBuffer mbb1 = new MultiByteBuffer(bb1, bb2);
mbb1.position(2);
mbb1.putInt(4);
int res = mbb1.getInt(2);
byte[] bres = new byte[4];
bres[0] = mbb1.get(2);
bres[1] = mbb1.get(3);
bres[2] = mbb1.get(4);
bres[3] = mbb1.get(5);
int expected = Bytes.toInt(bres);
assertEquals(res, expected);
}
@Test
public void testGetIntStrictlyForwardWithPosOnMultiBuffers() throws IOException {
byte[] b = new byte[4];
byte[] b1 = new byte[4];
ByteBuffer bb1 = ByteBuffer.wrap(b);
ByteBuffer bb2 = ByteBuffer.wrap(b1);
MultiByteBuffer mbb1 = new MultiByteBuffer(bb1, bb2);
mbb1.position(2);
mbb1.putInt(4);
mbb1.position(7);
mbb1.put((byte) 2);
mbb1.position(0);
mbb1.getIntStrictlyForward(4);
byte res = mbb1.get(7);
assertEquals((byte) 2, res);
}
}