Return-Path: X-Original-To: apmail-hbase-commits-archive@www.apache.org Delivered-To: apmail-hbase-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8B1BD18C1E for ; Mon, 4 Jan 2016 16:56:52 +0000 (UTC) Received: (qmail 75315 invoked by uid 500); 4 Jan 2016 16:56:48 -0000 Delivered-To: apmail-hbase-commits-archive@hbase.apache.org Received: (qmail 75206 invoked by uid 500); 4 Jan 2016 16:56:48 -0000 Mailing-List: contact commits-help@hbase.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hbase.apache.org Delivered-To: mailing list commits@hbase.apache.org Received: (qmail 73290 invoked by uid 99); 4 Jan 2016 16:56:47 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Jan 2016 16:56:47 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 22DCCE0667; Mon, 4 Jan 2016 16:56:47 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: syuanjiang@apache.org To: commits@hbase.apache.org Date: Mon, 04 Jan 2016 16:57:10 -0000 Message-Id: <62a20cd9d46e447cb78429536908229c@git.apache.org> In-Reply-To: <51767155c1144ae68c5c50a871d63d25@git.apache.org> References: <51767155c1144ae68c5c50a871d63d25@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [25/29] hbase git commit: HBASE-15063 Bug in MultiByteBuf#toBytes. (deepankar) HBASE-15063 Bug in MultiByteBuf#toBytes. (deepankar) Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/f01a388a Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/f01a388a Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/f01a388a Branch: refs/heads/hbase-12439 Commit: f01a388a35e03b432b1f3a1ecab04ab2c99e9444 Parents: 92abf8a Author: anoopsjohn Authored: Fri Jan 1 23:11:59 2016 +0530 Committer: anoopsjohn Committed: Fri Jan 1 23:11:59 2016 +0530 ---------------------------------------------------------------------- .../apache/hadoop/hbase/nio/MultiByteBuff.java | 16 +-------- .../hadoop/hbase/nio/TestMultiByteBuff.java | 36 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/f01a388a/hbase-common/src/main/java/org/apache/hadoop/hbase/nio/MultiByteBuff.java ---------------------------------------------------------------------- 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 06652b8..ab2b5ea 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 @@ -1065,21 +1065,7 @@ public class MultiByteBuff extends ByteBuff { @Override public byte[] toBytes(int offset, int length) { byte[] output = new byte[length]; - int itemIndex = getItemIndex(offset); - ByteBuffer item = this.items[itemIndex]; - int toRead = item.limit() - offset; - int destinationOffset = 0; - while (length > 0) { - toRead = Math.min(length, toRead); - ByteBufferUtils.copyFromBufferToArray(output, item, offset, destinationOffset, toRead); - length -= toRead; - if (length == 0) - break; - destinationOffset += toRead; - offset = 0; - item = items[++itemIndex]; - toRead = item.remaining(); - } + this.get(offset, output, 0, length); return output; } http://git-wip-us.apache.org/repos/asf/hbase/blob/f01a388a/hbase-common/src/test/java/org/apache/hadoop/hbase/nio/TestMultiByteBuff.java ---------------------------------------------------------------------- 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 193fcff..800c8e1 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 @@ -342,4 +342,40 @@ public class TestMultiByteBuff { assertEquals(2, dst[0]); assertEquals(12, mbb1.position()); } + + @Test + public void testToBytes() throws Exception { + byte[] b = new byte[4]; + byte[] b1 = new byte[8]; + for (int i = 0; i < b.length; i++) { + b[i] = (byte) i; + } + for (int i = 0; i < b1.length; i++) { + b1[i] = (byte) (b1.length + i); + } + ByteBuffer bb1 = ByteBuffer.wrap(b); + ByteBuffer bb2 = ByteBuffer.wrap(b1); + MultiByteBuff mbb1 = new MultiByteBuff(bb1, bb2); + + // Test 1 Offset hitting exclusive second element + byte[] actual = mbb1.toBytes(6, 4); + assertTrue(Bytes.equals(actual, 0, actual.length, + b1, 2, 4)); + // Test 2 offset hitting exclusive second element + // but continuing to the end of the second one + actual = mbb1.toBytes(5, 7); + assertTrue(Bytes.equals(actual, 0, actual.length, + b1, 1, 7)); + // Test 3 with offset hitting in first element, + // continuing to next + actual = mbb1.toBytes(2, 7); + byte[] expected = new byte[7]; + System.arraycopy(b, 2, expected, 0, 2); + System.arraycopy(b1, 0, expected, 2, 5); + assertTrue(Bytes.equals(actual, expected)); + // Test 4 hitting only in first exclusively + actual = mbb1.toBytes(1, 3); + assertTrue(Bytes.equals(actual, 0, actual.length, + b, 1, 3)); + } }