From 1b75fd2bd6fdae2b6a8634ff24492ff0b96c1f32 Mon Sep 17 00:00:00 2001 From: ramkrishna Date: Fri, 3 Jul 2015 07:58:48 +0530 Subject: [PATCH] HBASE-14011 - MultiByteBuffer position based reads does not work correctly (Ram) --- .../hadoop/hbase/nio/MultiByteBuffer.java | 4 +-- .../hadoop/hbase/nio/TestMultiByteBuffer.java | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/nio/MultiByteBuffer.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/nio/MultiByteBuffer.java index 5fcc34dc561..1b4ad2a1cf6 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/nio/MultiByteBuffer.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/nio/MultiByteBuffer.java @@ -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(); diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/nio/TestMultiByteBuffer.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/nio/TestMultiByteBuffer.java index 27f34841bd9..ddab391719e 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/nio/TestMultiByteBuffer.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/nio/TestMultiByteBuffer.java @@ -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); + } }