.../org/apache/hadoop/hbase/nio/MultiByteBuff.java | 12 +++++-- .../apache/hadoop/hbase/nio/TestMultiByteBuff.java | 37 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/nio/MultiByteBuff.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/nio/MultiByteBuff.java index 948321d..2318c18 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/nio/MultiByteBuff.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/nio/MultiByteBuff.java @@ -517,7 +517,9 @@ public class MultiByteBuff extends ByteBuff { } this.curItemIndex++; this.curItem = this.items[this.curItemIndex]; - return this.curItem.getShort(); + if (this.curItem.remaining() >= Bytes.SIZEOF_SHORT) { + return this.curItem.getShort(); + } } short n = 0; n ^= get() & 0xFF; @@ -544,7 +546,9 @@ public class MultiByteBuff extends ByteBuff { } this.curItemIndex++; this.curItem = this.items[this.curItemIndex]; - return this.curItem.getInt(); + if (this.curItem.remaining() >= Bytes.SIZEOF_INT) { + return this.curItem.getInt(); + } } // Get available bytes from this item and remaining from next int n = 0; @@ -574,7 +578,9 @@ public class MultiByteBuff extends ByteBuff { } this.curItemIndex++; this.curItem = this.items[this.curItemIndex]; - return this.curItem.getLong(); + if (this.curItem.remaining() >= Bytes.SIZEOF_LONG) { + return this.curItem.getLong(); + } } // Get available bytes from this item and remaining from next long l = 0; diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/nio/TestMultiByteBuff.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/nio/TestMultiByteBuff.java index af4c464..3a318b4 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/nio/TestMultiByteBuff.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/nio/TestMultiByteBuff.java @@ -403,4 +403,41 @@ public class TestMultiByteBuff { mbb1.get(); // Now we have reached the limit assertFalse(mbb1.hasRemaining()); } + + @Test + public void testRelativeGets() { + ByteBuffer b1 = ByteBuffer.allocate(4); + ByteBuffer b2 = ByteBuffer.allocate(4); + ByteBuffer b3 = ByteBuffer.allocate(4); + ByteBuffer b4 = ByteBuffer.allocate(1); + ByteBuffer b5 = ByteBuffer.allocate(1); + ByteBuffer b6 = ByteBuffer.allocate(1); + ByteBuffer b7 = ByteBuffer.allocate(1); + ByteBuffer b8 = ByteBuffer.allocate(4); + ByteBuffer b9 = ByteBuffer.allocate(4); + ByteBuffer b10 = ByteBuffer.allocate(4); + ByteBuffer b11 = ByteBuffer.allocate(4); + MultiByteBuff mbb1 = new MultiByteBuff(b1, b2, b3, b4, b5, b6, b7); + mbb1.putInt(100); + mbb1.putInt(500); + mbb1.putInt(100); + mbb1.putInt(500); + assertEquals(16, mbb1.position()); + mbb1.position(0); + assertEquals(100, mbb1.getInt()); + assertEquals(500, mbb1.getInt()); + assertEquals(100, mbb1.getInt()); + assertEquals(500, mbb1.getInt()); + mbb1 = new MultiByteBuff(b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11); + mbb1.putInt(100); + mbb1.putInt(500); + mbb1.putLong(100l); + mbb1.putLong(500l); + assertEquals(24, mbb1.position()); + mbb1.position(0); + assertEquals(100, mbb1.getInt()); + assertEquals(500, mbb1.getInt()); + assertEquals(100, mbb1.getLong()); + assertEquals(500, mbb1.getLong()); + } }