[#1852] Fix bug in UnpooledDirectByteBuf.nioBuffer(...) implementation

This commit is contained in:
Norman Maurer 2013-09-18 20:47:57 +02:00
parent f034f90fb3
commit c0bbde48b7
5 changed files with 78 additions and 3 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}