Repository: accumulo Updated Branches: refs/heads/master 702ea54f6 -> e59649bff
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/master Commit: a2c2d38aa248056c1cf592e8a2a0ada17eb518e2 Parents: 8c6866e Author: Keith Turner <ktur...@apache.org> Authored: Tue Jan 19 15:55:34 2016 -0500 Committer: Keith Turner <ktur...@apache.org> 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); + } +}