diff --git a/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java index 73fcf50fef..4496ad9f41 100644 --- a/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java @@ -38,9 +38,8 @@ import java.nio.channels.ScatteringByteChannel; public class DynamicChannelBuffer extends AbstractChannelBuffer { private final ChannelBufferFactory factory; - private final int initialCapacity; private final ByteOrder endianness; - private ChannelBuffer buffer = ChannelBuffers.EMPTY_BUFFER; + private ChannelBuffer buffer; public DynamicChannelBuffer(int estimatedLength) { this(ByteOrder.BIG_ENDIAN, estimatedLength); @@ -61,8 +60,8 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer { throw new NullPointerException("factory"); } this.factory = factory; - initialCapacity = estimatedLength; this.endianness = endianness; + buffer = factory.getBuffer(order(), estimatedLength); } public ChannelBufferFactory factory() { @@ -268,10 +267,7 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer { int newCapacity; if (capacity() == 0) { - newCapacity = initialCapacity; - if (newCapacity == 0) { - newCapacity = 1; - } + newCapacity = 1; } else { newCapacity = capacity(); } diff --git a/src/test/java/org/jboss/netty/buffer/DynamicChannelBufferTest.java b/src/test/java/org/jboss/netty/buffer/DynamicChannelBufferTest.java index 96862dcbe1..2314e38a28 100644 --- a/src/test/java/org/jboss/netty/buffer/DynamicChannelBufferTest.java +++ b/src/test/java/org/jboss/netty/buffer/DynamicChannelBufferTest.java @@ -17,6 +17,8 @@ package org.jboss.netty.buffer; import static org.junit.Assert.*; +import java.nio.ByteOrder; + import org.junit.Test; /** @@ -34,14 +36,7 @@ public class DynamicChannelBufferTest extends AbstractChannelBufferTest { protected ChannelBuffer newBuffer(int length) { buffer = ChannelBuffers.dynamicBuffer(length); - // A dynamic buffer does lazy initialization. - assertEquals(0, buffer.capacity()); - - // Initialize. - buffer.writeZero(1); - buffer.clear(); - - // And validate the initial capacity + assertEquals(0, buffer.readerIndex()); assertEquals(0, buffer.writerIndex()); assertEquals(length, buffer.capacity()); @@ -57,4 +52,21 @@ public class DynamicChannelBufferTest extends AbstractChannelBufferTest { public void shouldNotAllowNullInConstructor() { new DynamicChannelBuffer(null, 0); } + + @Test + public void shouldNotFailOnInitialIndexUpdate() { + new DynamicChannelBuffer(ByteOrder.BIG_ENDIAN, 10).setIndex(0, 10); + } + + @Test + public void shouldNotFailOnInitialIndexUpdate2() { + new DynamicChannelBuffer(ByteOrder.BIG_ENDIAN, 10).writerIndex(10); + } + + @Test + public void shouldNotFailOnInitialIndexUpdate3() { + ChannelBuffer buf = new DynamicChannelBuffer(ByteOrder.BIG_ENDIAN, 10); + buf.writerIndex(10); + buf.readerIndex(10); + } }