[#1800] [#1802] Correctly expand capacity of ByteBuf while preserve content

This commit is contained in:
Norman Maurer 2013-11-04 15:18:21 +01:00
parent 6cd9045b96
commit 77b4ec7e1b
5 changed files with 18 additions and 7 deletions

View File

@ -233,8 +233,8 @@ abstract class PoolArena<T> {
allocate(parent.threadCache.get(), buf, newCapacity);
if (newCapacity > oldCapacity) {
memoryCopy(
oldMemory, oldOffset + readerIndex,
buf.memory, buf.offset + readerIndex, writerIndex - readerIndex);
oldMemory, oldOffset,
buf.memory, buf.offset, oldCapacity);
} else if (newCapacity < oldCapacity) {
if (readerIndex < newCapacity) {
if (writerIndex > newCapacity) {

View File

@ -141,8 +141,8 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
if (newCapacity > oldCapacity) {
ByteBuffer oldBuffer = buffer;
ByteBuffer newBuffer = ByteBuffer.allocateDirect(newCapacity);
oldBuffer.position(readerIndex).limit(writerIndex);
newBuffer.position(readerIndex).limit(writerIndex);
oldBuffer.position(0).limit(oldBuffer.capacity());
newBuffer.position(0).limit(oldBuffer.capacity());
newBuffer.put(oldBuffer);
newBuffer.clear();
setByteBuffer(newBuffer);

View File

@ -112,7 +112,7 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
int oldCapacity = array.length;
if (newCapacity > oldCapacity) {
byte[] newArray = new byte[newCapacity];
System.arraycopy(array, readerIndex(), newArray, readerIndex(), readableBytes());
System.arraycopy(array, 0, newArray, 0, array.length);
setArray(newArray);
} else if (newCapacity < oldCapacity) {
byte[] newArray = new byte[newCapacity];

View File

@ -145,8 +145,8 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf
if (newCapacity > oldCapacity) {
ByteBuffer oldBuffer = buffer;
ByteBuffer newBuffer = ByteBuffer.allocateDirect(newCapacity);
oldBuffer.position(readerIndex).limit(writerIndex);
newBuffer.position(readerIndex).limit(writerIndex);
oldBuffer.position(0).limit(oldBuffer.capacity());
newBuffer.position(0).limit(oldBuffer.capacity());
newBuffer.put(oldBuffer);
newBuffer.clear();
setByteBuffer(newBuffer);

View File

@ -52,4 +52,15 @@ public class DuplicateByteBufTest extends AbstractByteBufTest {
super.testInternalNioBuffer();
}
// See https://github.com/netty/netty/issues/1800
@Test
public void testIncreaseCapacityWrapped() {
ByteBuf wrapped = buffer.unwrap();
wrapped.writeByte(0);
wrapped.readerIndex(wrapped.readerIndex() + 1);
buffer.writerIndex(buffer.writerIndex() + 1);
wrapped.capacity(wrapped.capacity() * 2);
assertEquals((byte) 0, buffer.readByte());
}
}