Skip to content

Commit 979fdfd

Browse files
committed
Reset markers when obtain PooledByteBuf.
Motivation: When allocate a PooledByteBuf we need to ensure to also reset the markers for the readerIndex and writerIndex. Modifications: - Correct reset the markers - Add test-case for it Result: Correctly reset markers.
1 parent dc73c15 commit 979fdfd

7 files changed

Lines changed: 90 additions & 47 deletions

buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,4 +1178,8 @@ protected final void ensureAccessible() {
11781178
throw new IllegalReferenceCountException(0);
11791179
}
11801180
}
1181+
1182+
void discardMarks() {
1183+
markedReaderIndex = markedWriterIndex = 0;
1184+
}
11811185
}

buffer/src/main/java/io/netty/buffer/PooledByteBuf.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void init(PoolChunk<T> chunk, long handle, int offset, int length, int maxLength
5050
this.length = length;
5151
this.maxLength = maxLength;
5252
setIndex(0, 0);
53+
discardMarks();
5354
tmpNioBuf = null;
5455
initThread = Thread.currentThread();
5556
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2015 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.buffer;
17+
18+
import org.junit.Test;
19+
20+
import static org.junit.Assert.*;
21+
22+
public abstract class AbstractPooledByteBufTest extends AbstractByteBufTest {
23+
24+
private ByteBuf buffer;
25+
26+
protected abstract ByteBuf alloc(int length);
27+
28+
@Override
29+
protected ByteBuf newBuffer(int length) {
30+
buffer = alloc(length);
31+
assertEquals(0, buffer.writerIndex());
32+
assertEquals(0, buffer.readerIndex());
33+
return buffer;
34+
}
35+
36+
@Override
37+
protected ByteBuf[] components() {
38+
return new ByteBuf[] { buffer };
39+
}
40+
41+
@Test
42+
public void testDiscardMarks() {
43+
ByteBuf buf = newBuffer(4);
44+
buf.writeShort(1);
45+
46+
buf.skipBytes(1);
47+
48+
buf.markReaderIndex();
49+
buf.markWriterIndex();
50+
assertTrue(buf.release());
51+
52+
ByteBuf buf2 = newBuffer(4);
53+
54+
assertSame(unwrapIfNeeded(buf), unwrapIfNeeded(buf2));
55+
56+
buf2.writeShort(1);
57+
58+
buf2.resetReaderIndex();
59+
buf2.resetWriterIndex();
60+
61+
assertEquals(0, buf2.readerIndex());
62+
assertEquals(0, buf2.writerIndex());
63+
assertTrue(buf2.release());
64+
}
65+
66+
private static ByteBuf unwrapIfNeeded(ByteBuf buf) {
67+
if (buf instanceof AdvancedLeakAwareByteBuf || buf instanceof SimpleLeakAwareByteBuf) {
68+
return buf.unwrap();
69+
}
70+
return buf;
71+
}
72+
}

buffer/src/test/java/io/netty/buffer/PooledBigEndianDirectByteBufTest.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,12 @@
2222
/**
2323
* Tests big-endian direct channel buffers
2424
*/
25-
public class PooledBigEndianDirectByteBufTest extends AbstractByteBufTest {
26-
27-
private ByteBuf buffer;
25+
public class PooledBigEndianDirectByteBufTest extends AbstractPooledByteBufTest {
2826

2927
@Override
30-
protected ByteBuf newBuffer(int length) {
31-
buffer = PooledByteBufAllocator.DEFAULT.directBuffer(length);
28+
protected ByteBuf alloc(int length) {
29+
ByteBuf buffer = PooledByteBufAllocator.DEFAULT.directBuffer(length);
3230
assertSame(ByteOrder.BIG_ENDIAN, buffer.order());
33-
assertEquals(0, buffer.writerIndex());
3431
return buffer;
3532
}
36-
37-
@Override
38-
protected ByteBuf[] components() {
39-
return new ByteBuf[] { buffer };
40-
}
4133
}

buffer/src/test/java/io/netty/buffer/PooledBigEndianHeapByteBufTest.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,13 @@
1515
*/
1616
package io.netty.buffer;
1717

18-
import static org.junit.Assert.*;
19-
2018
/**
2119
* Tests big-endian heap channel buffers
2220
*/
23-
public class PooledBigEndianHeapByteBufTest extends AbstractByteBufTest {
24-
25-
private ByteBuf buffer;
26-
27-
@Override
28-
protected ByteBuf newBuffer(int length) {
29-
buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length);
30-
assertEquals(0, buffer.writerIndex());
31-
return buffer;
32-
}
21+
public class PooledBigEndianHeapByteBufTest extends AbstractPooledByteBufTest {
3322

3423
@Override
35-
protected ByteBuf[] components() {
36-
return new ByteBuf[] { buffer };
24+
protected ByteBuf alloc(int length) {
25+
return PooledByteBufAllocator.DEFAULT.heapBuffer(length);
3726
}
3827
}

buffer/src/test/java/io/netty/buffer/PooledLittleEndianDirectByteBufTest.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,12 @@
2222
/**
2323
* Tests little-endian direct channel buffers
2424
*/
25-
public class PooledLittleEndianDirectByteBufTest extends AbstractByteBufTest {
26-
27-
private ByteBuf buffer;
25+
public class PooledLittleEndianDirectByteBufTest extends AbstractPooledByteBufTest {
2826

2927
@Override
30-
protected ByteBuf newBuffer(int length) {
31-
buffer = PooledByteBufAllocator.DEFAULT.directBuffer(length).order(ByteOrder.LITTLE_ENDIAN);
28+
protected ByteBuf alloc(int length) {
29+
ByteBuf buffer = PooledByteBufAllocator.DEFAULT.directBuffer(length).order(ByteOrder.LITTLE_ENDIAN);
3230
assertSame(ByteOrder.LITTLE_ENDIAN, buffer.order());
33-
assertEquals(0, buffer.writerIndex());
3431
return buffer;
3532
}
36-
37-
@Override
38-
protected ByteBuf[] components() {
39-
return new ByteBuf[] { buffer };
40-
}
4133
}

buffer/src/test/java/io/netty/buffer/PooledLittleEndianHeapByteBufTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,12 @@
2222
/**
2323
* Tests little-endian heap channel buffers
2424
*/
25-
public class PooledLittleEndianHeapByteBufTest extends AbstractByteBufTest {
26-
27-
private ByteBuf buffer;
25+
public class PooledLittleEndianHeapByteBufTest extends AbstractPooledByteBufTest {
2826

2927
@Override
30-
protected ByteBuf newBuffer(int length) {
31-
buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length).order(ByteOrder.LITTLE_ENDIAN);
32-
assertEquals(0, buffer.writerIndex());
28+
protected ByteBuf alloc(int length) {
29+
ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length).order(ByteOrder.LITTLE_ENDIAN);
30+
assertSame(ByteOrder.LITTLE_ENDIAN, buffer.order());
3331
return buffer;
3432
}
35-
36-
@Override
37-
protected ByteBuf[] components() {
38-
return new ByteBuf[] { buffer };
39-
}
4033
}

0 commit comments

Comments
 (0)