From 4bdd8dacb9a4bbc4097ba8fda4124bc26f67734c Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Wed, 26 Aug 2015 13:35:49 -0700 Subject: [PATCH] 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. --- .../io/netty/buffer/DuplicatedByteBuf.java | 2 ++ .../io/netty/buffer/DuplicateByteBufTest.java | 30 +++++++++++++++++-- .../io/netty/buffer/SlicedByteBufTest.java | 26 +++++++++++++++- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java b/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java index d6f89644cd..5798b723c3 100644 --- a/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java @@ -45,6 +45,8 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf { } setIndex(buffer.readerIndex(), buffer.writerIndex()); + markReaderIndex(); + markWriterIndex(); } @Override diff --git a/buffer/src/test/java/io/netty/buffer/DuplicateByteBufTest.java b/buffer/src/test/java/io/netty/buffer/DuplicateByteBufTest.java index 5dad9f13cb..dbca9069ed 100644 --- a/buffer/src/test/java/io/netty/buffer/DuplicateByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/DuplicateByteBufTest.java @@ -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(); + } + } } diff --git a/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java b/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java index e303176310..7aecaadd62 100644 --- a/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java @@ -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(); + } + } }