Resolved issue: NETTY-254 Do not do lazy-initialization in dynamic buffer

* No lazy initialization anymore for DynamicChannelBuffer - it just causes confusion to users!
This commit is contained in:
Trustin Lee 2009-11-19 08:38:46 +00:00
parent 6795fc0627
commit 9e44dbada5
2 changed files with 23 additions and 15 deletions

View File

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

View File

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