diff --git a/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java index b2c8baeee1..5547117268 100644 --- a/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java @@ -304,7 +304,9 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer { public ChannelBuffer slice(int index, int length) { if (index == 0 && length == capacity()) { - return duplicate(); + ChannelBuffer slice = duplicate(); + slice.setIndex(0, length); + return slice; } else { if (index >= 0 && length == 0) { return ChannelBuffers.EMPTY_BUFFER; diff --git a/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java index 07a0383910..8bc888a1c6 100644 --- a/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java @@ -192,7 +192,9 @@ public abstract class HeapChannelBuffer extends AbstractChannelBuffer { return ChannelBuffers.EMPTY_BUFFER; } if (length == array.length) { - return duplicate(); + ChannelBuffer slice = duplicate(); + slice.setIndex(0, length); + return slice; } else { return new TruncatedChannelBuffer(this, length); } diff --git a/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java b/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java index b8b9085b63..dcd5751285 100644 --- a/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java +++ b/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java @@ -1472,8 +1472,24 @@ public abstract class AbstractChannelBufferTest { } @Test - public void testSlice() throws Exception { + public void testSliceEndianness() throws Exception { + assertEquals(buffer.order(), buffer.slice(0, buffer.capacity()).order()); + assertEquals(buffer.order(), buffer.slice(0, buffer.capacity() - 1).order()); assertEquals(buffer.order(), buffer.slice(1, buffer.capacity() - 1).order()); + assertEquals(buffer.order(), buffer.slice(1, buffer.capacity() - 2).order()); + } + + @Test + public void testSliceIndex() throws Exception { + assertEquals(0, buffer.slice(0, buffer.capacity()).readerIndex()); + assertEquals(0, buffer.slice(0, buffer.capacity() - 1).readerIndex()); + assertEquals(0, buffer.slice(1, buffer.capacity() - 1).readerIndex()); + assertEquals(0, buffer.slice(1, buffer.capacity() - 2).readerIndex()); + + assertEquals(buffer.capacity(), buffer.slice(0, buffer.capacity()).writerIndex()); + assertEquals(buffer.capacity() - 1, buffer.slice(0, buffer.capacity() - 1).writerIndex()); + assertEquals(buffer.capacity() - 1, buffer.slice(1, buffer.capacity() - 1).writerIndex()); + assertEquals(buffer.capacity() - 2, buffer.slice(1, buffer.capacity() - 2).writerIndex()); } @Test