Return-Path: X-Original-To: apmail-accumulo-commits-archive@www.apache.org Delivered-To: apmail-accumulo-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 7264B1843D for ; Wed, 20 Jan 2016 00:05:54 +0000 (UTC) Received: (qmail 6776 invoked by uid 500); 20 Jan 2016 00:05:54 -0000 Delivered-To: apmail-accumulo-commits-archive@accumulo.apache.org Received: (qmail 6741 invoked by uid 500); 20 Jan 2016 00:05:54 -0000 Mailing-List: contact commits-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@accumulo.apache.org Delivered-To: mailing list commits@accumulo.apache.org Received: (qmail 6732 invoked by uid 99); 20 Jan 2016 00:05:54 -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; Wed, 20 Jan 2016 00:05:54 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id F3B5ADFCB5; Wed, 20 Jan 2016 00:05:53 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: kturner@apache.org To: commits@accumulo.apache.org Message-Id: <97333e677ac0490489f01b0158591a21@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: accumulo git commit: ACCUMULO-4098 Fixed bug with ByteBuffers thats do not start at 0 Date: Wed, 20 Jan 2016 00:05:53 +0000 (UTC) Repository: accumulo Updated Branches: refs/heads/1.6 8c6866efb -> a2c2d38aa ACCUMULO-4098 Fixed bug with ByteBuffers thats do not start at 0 Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/a2c2d38a Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/a2c2d38a Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/a2c2d38a Branch: refs/heads/1.6 Commit: a2c2d38aa248056c1cf592e8a2a0ada17eb518e2 Parents: 8c6866e Author: Keith Turner Authored: Tue Jan 19 15:55:34 2016 -0500 Committer: Keith Turner Committed: Tue Jan 19 15:55:34 2016 -0500 ---------------------------------------------------------------------- .../core/util/UnsynchronizedBuffer.java | 4 +- .../core/util/UnsynchronizedBufferTest.java | 56 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/a2c2d38a/core/src/main/java/org/apache/accumulo/core/util/UnsynchronizedBuffer.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/UnsynchronizedBuffer.java b/core/src/main/java/org/apache/accumulo/core/util/UnsynchronizedBuffer.java index 6947d64..f353613 100644 --- a/core/src/main/java/org/apache/accumulo/core/util/UnsynchronizedBuffer.java +++ b/core/src/main/java/org/apache/accumulo/core/util/UnsynchronizedBuffer.java @@ -118,8 +118,8 @@ public class UnsynchronizedBuffer { } public Reader(ByteBuffer buffer) { - if (buffer.hasArray()) { - offset = buffer.arrayOffset(); + if (buffer.hasArray() && buffer.array().length == buffer.arrayOffset() + buffer.limit()) { + offset = buffer.arrayOffset() + buffer.position(); data = buffer.array(); } else { data = new byte[buffer.remaining()]; http://git-wip-us.apache.org/repos/asf/accumulo/blob/a2c2d38a/core/src/test/java/org/apache/accumulo/core/util/UnsynchronizedBufferTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/util/UnsynchronizedBufferTest.java b/core/src/test/java/org/apache/accumulo/core/util/UnsynchronizedBufferTest.java new file mode 100644 index 0000000..6416219 --- /dev/null +++ b/core/src/test/java/org/apache/accumulo/core/util/UnsynchronizedBufferTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.accumulo.core.util; + +import java.nio.ByteBuffer; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.google.common.base.Charsets; + +public class UnsynchronizedBufferTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testByteBufferConstructor() { + byte[] test = "0123456789".getBytes(Charsets.UTF_8); + + ByteBuffer bb1 = ByteBuffer.wrap(test); + UnsynchronizedBuffer.Reader ub = new UnsynchronizedBuffer.Reader(bb1); + byte[] buf = new byte[10]; + ub.readBytes(buf); + Assert.assertEquals("0123456789", new String(buf, Charsets.UTF_8)); + + ByteBuffer bb2 = ByteBuffer.wrap(test, 3, 5); + + ub = new UnsynchronizedBuffer.Reader(bb2); + buf = new byte[5]; + // should read data from offset 3 where the byte buffer starts + ub.readBytes(buf); + Assert.assertEquals("34567", new String(buf, Charsets.UTF_8)); + + buf = new byte[6]; + // the byte buffer has the extra byte, but should not be able to read it... + thrown.expect(ArrayIndexOutOfBoundsException.class); + ub.readBytes(buf); + } +}