Restore derived buffer index/mark updates

Motivation:
As part of the revert process in https://github.com/netty/netty/pull/4138 some index and mark updates were lost.

Modifications:
- Restore the index / mark updates made in https://github.com/netty/netty/pull/3788

Result:
Slice and Duplicate buffers index / marks are correctly initialized.
This commit is contained in:
Scott Mitchell 2015-08-26 13:35:49 -07:00
parent 56cc0bbd4c
commit 4bdd8dacb9
3 changed files with 54 additions and 4 deletions

View File

@ -45,6 +45,8 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf {
}
setIndex(buffer.readerIndex(), buffer.writerIndex());
markReaderIndex();
markWriterIndex();
}
@Override

View File

@ -17,7 +17,7 @@ package io.netty.buffer;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
/**
* Tests duplicated channel buffers
@ -28,8 +28,10 @@ public class DuplicateByteBufTest extends AbstractByteBufTest {
@Override
protected ByteBuf newBuffer(int length) {
buffer = new DuplicatedByteBuf(Unpooled.buffer(length));
assertEquals(0, buffer.writerIndex());
ByteBuf wrapped = Unpooled.buffer(length);
buffer = new DuplicatedByteBuf(wrapped);
assertEquals(wrapped.writerIndex(), buffer.writerIndex());
assertEquals(wrapped.readerIndex(), buffer.readerIndex());
return buffer;
}
@ -54,4 +56,26 @@ public class DuplicateByteBufTest extends AbstractByteBufTest {
assertEquals((byte) 0, buffer.readByte());
}
@Test
public void testMarksInitialized() {
ByteBuf wrapped = Unpooled.buffer(8);
try {
wrapped.writerIndex(6);
wrapped.readerIndex(1);
ByteBuf duplicate = new DuplicatedByteBuf(wrapped);
// Test writer mark
duplicate.writerIndex(duplicate.writerIndex() + 1);
duplicate.resetWriterIndex();
assertEquals(wrapped.writerIndex(), duplicate.writerIndex());
// Test reader mark
duplicate.readerIndex(duplicate.readerIndex() + 1);
duplicate.resetReaderIndex();
assertEquals(wrapped.readerIndex(), duplicate.readerIndex());
} finally {
wrapped.release();
}
}
}

View File

@ -20,7 +20,7 @@ import org.junit.Test;
import java.io.IOException;
import java.util.Random;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
/**
* Tests sliced channel buffers
@ -113,4 +113,28 @@ public class SlicedByteBufTest extends AbstractByteBufTest {
public void testLittleEndianWithExpand() {
// ignore for SlicedByteBuf
}
@Test
public void testReaderIndexAndMarks() {
ByteBuf wrapped = Unpooled.buffer(16);
try {
wrapped.writerIndex(14);
wrapped.readerIndex(2);
wrapped.markWriterIndex();
wrapped.markReaderIndex();
ByteBuf slice = new SlicedByteBuf(wrapped, 4, 4);
assertEquals(0, slice.readerIndex());
assertEquals(4, slice.writerIndex());
slice.readerIndex(slice.readerIndex() + 1);
slice.resetReaderIndex();
assertEquals(0, slice.readerIndex());
slice.writerIndex(slice.writerIndex() - 1);
slice.resetWriterIndex();
assertEquals(0, slice.writerIndex());
} finally {
wrapped.release();
}
}
}