From c0bbde48b78163a394ae3fd63f98b6b6c56acb25 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 18 Sep 2013 20:47:57 +0200 Subject: [PATCH] [#1852] Fix bug in UnpooledDirectByteBuf.nioBuffer(...) implementation --- .../netty/buffer/UnpooledDirectByteBuf.java | 2 +- .../buffer/BigEndianDirectByteBufTest.java | 6 +++- .../BigEndianUnsafeDirectByteBufTest.java | 34 +++++++++++++++++++ .../buffer/LittleEndianDirectByteBufTest.java | 6 +++- .../LittleEndianUnsafeDirectByteBufTest.java | 33 ++++++++++++++++++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 buffer/src/test/java/io/netty/buffer/BigEndianUnsafeDirectByteBufTest.java create mode 100644 buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeDirectByteBufTest.java diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java index 7284fc5a69..721e28fddf 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java @@ -506,7 +506,7 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { @Override public ByteBuffer nioBuffer(int index, int length) { - return (ByteBuffer) buffer.duplicate().position(index).position(index + length); + return (ByteBuffer) buffer.duplicate().position(index).limit(index + length); } @Override diff --git a/buffer/src/test/java/io/netty/buffer/BigEndianDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/BigEndianDirectByteBufTest.java index 98f29bf721..c4ba9f5d23 100644 --- a/buffer/src/test/java/io/netty/buffer/BigEndianDirectByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/BigEndianDirectByteBufTest.java @@ -28,7 +28,7 @@ public class BigEndianDirectByteBufTest extends AbstractByteBufTest { @Override protected ByteBuf newBuffer(int length) { - buffer = Unpooled.directBuffer(length); + buffer = newDirectBuffer(length); assertSame(ByteOrder.BIG_ENDIAN, buffer.order()); assertEquals(0, buffer.writerIndex()); return buffer; @@ -38,4 +38,8 @@ public class BigEndianDirectByteBufTest extends AbstractByteBufTest { protected ByteBuf[] components() { return new ByteBuf[] { buffer }; } + + protected ByteBuf newDirectBuffer(int length) { + return new UnpooledDirectByteBuf(UnpooledByteBufAllocator.DEFAULT, length, Integer.MAX_VALUE); + } } diff --git a/buffer/src/test/java/io/netty/buffer/BigEndianUnsafeDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/BigEndianUnsafeDirectByteBufTest.java new file mode 100644 index 0000000000..6b04c9ae84 --- /dev/null +++ b/buffer/src/test/java/io/netty/buffer/BigEndianUnsafeDirectByteBufTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013 The Netty Project + * + * The Netty Project 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 io.netty.buffer; + + +import io.netty.util.internal.PlatformDependent; +import org.junit.Assume; +import org.junit.Before; + +public class BigEndianUnsafeDirectByteBufTest extends BigEndianDirectByteBufTest { + + @Before + public void checkHasUnsafe() { + Assume.assumeTrue("sun.misc.Unsafe not found, skip tests", PlatformDependent.hasUnsafe()); + } + + @Override + protected ByteBuf newBuffer(int length) { + return new UnpooledUnsafeDirectByteBuf(UnpooledByteBufAllocator.DEFAULT, length, Integer.MAX_VALUE); + } +} diff --git a/buffer/src/test/java/io/netty/buffer/LittleEndianDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/LittleEndianDirectByteBufTest.java index b8162513b5..637327faa5 100644 --- a/buffer/src/test/java/io/netty/buffer/LittleEndianDirectByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/LittleEndianDirectByteBufTest.java @@ -28,7 +28,7 @@ public class LittleEndianDirectByteBufTest extends AbstractByteBufTest { @Override protected ByteBuf newBuffer(int length) { - buffer = Unpooled.directBuffer(length).order(ByteOrder.LITTLE_ENDIAN); + buffer = newDirectBuffer(length).order(ByteOrder.LITTLE_ENDIAN); assertSame(ByteOrder.LITTLE_ENDIAN, buffer.order()); assertEquals(0, buffer.writerIndex()); return buffer; @@ -38,4 +38,8 @@ public class LittleEndianDirectByteBufTest extends AbstractByteBufTest { protected ByteBuf[] components() { return new ByteBuf[] { buffer }; } + + protected ByteBuf newDirectBuffer(int length) { + return new UnpooledDirectByteBuf(UnpooledByteBufAllocator.DEFAULT, length, Integer.MAX_VALUE); + } } diff --git a/buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeDirectByteBufTest.java b/buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeDirectByteBufTest.java new file mode 100644 index 0000000000..a85b11884a --- /dev/null +++ b/buffer/src/test/java/io/netty/buffer/LittleEndianUnsafeDirectByteBufTest.java @@ -0,0 +1,33 @@ +/* + * Copyright 2013 The Netty Project + * + * The Netty Project 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 io.netty.buffer; + +import io.netty.util.internal.PlatformDependent; +import org.junit.Assume; +import org.junit.Before; + +public class LittleEndianUnsafeDirectByteBufTest extends LittleEndianDirectByteBufTest { + + @Before + public void checkHasUnsafe() { + Assume.assumeTrue("sun.misc.Unsafe not found, skip tests", PlatformDependent.hasUnsafe()); + } + + @Override + protected ByteBuf newBuffer(int length) { + return new UnpooledUnsafeDirectByteBuf(UnpooledByteBufAllocator.DEFAULT, length, Integer.MAX_VALUE); + } +}