diff --git a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java index 21351f8a13..41ff7988ce 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java @@ -39,7 +39,7 @@ public abstract class AbstractByteBuf implements ByteBuf { private int markedReaderIndex; private int markedWriterIndex; - private final int maxCapacity; + private int maxCapacity; private SwappedByteBuf swappedBuf; @@ -55,6 +55,10 @@ public abstract class AbstractByteBuf implements ByteBuf { return maxCapacity; } + protected final void maxCapacity(int maxCapacity) { + this.maxCapacity = maxCapacity; + } + @Override public int readerIndex() { return readerIndex; @@ -216,7 +220,7 @@ public abstract class AbstractByteBuf implements ByteBuf { return this; } - protected void adjustMarkers(int decrement) { + protected final void adjustMarkers(int decrement) { int markedReaderIndex = this.markedReaderIndex; if (markedReaderIndex <= decrement) { this.markedReaderIndex = 0; diff --git a/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java index 10ac98c9f0..ab1fca3348 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java @@ -31,16 +31,14 @@ final class PooledDirectByteBuf extends PooledByteBuf { private static final Recycler RECYCLER = new Recycler() { @Override protected PooledDirectByteBuf newObject(Handle handle) { - return new PooledDirectByteBuf(handle, Integer.MAX_VALUE); + return new PooledDirectByteBuf(handle, 0); } }; static PooledDirectByteBuf newInstance(int maxCapacity) { - if (maxCapacity == Integer.MAX_VALUE) { - return RECYCLER.get(); - } else { - return new PooledDirectByteBuf(null, maxCapacity); - } + PooledDirectByteBuf buf = RECYCLER.get(); + buf.maxCapacity(maxCapacity); + return buf; } private PooledDirectByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) { diff --git a/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java index 5e2de4d4d8..70db1c6252 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java @@ -30,16 +30,14 @@ final class PooledHeapByteBuf extends PooledByteBuf { private static final Recycler RECYCLER = new Recycler() { @Override protected PooledHeapByteBuf newObject(Handle handle) { - return new PooledHeapByteBuf(handle, Integer.MAX_VALUE); + return new PooledHeapByteBuf(handle, 0); } }; static PooledHeapByteBuf newInstance(int maxCapacity) { - if (maxCapacity == Integer.MAX_VALUE) { - return RECYCLER.get(); - } else { - return new PooledHeapByteBuf(null, maxCapacity); - } + PooledHeapByteBuf buf = RECYCLER.get(); + buf.maxCapacity(maxCapacity); + return buf; } private PooledHeapByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) { diff --git a/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java index 561539d374..b23ad068f0 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java @@ -35,16 +35,14 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf { private static final Recycler RECYCLER = new Recycler() { @Override protected PooledUnsafeDirectByteBuf newObject(Handle handle) { - return new PooledUnsafeDirectByteBuf(handle, Integer.MAX_VALUE); + return new PooledUnsafeDirectByteBuf(handle, 0); } }; static PooledUnsafeDirectByteBuf newInstance(int maxCapacity) { - if (maxCapacity == Integer.MAX_VALUE) { - return RECYCLER.get(); - } else { - return new PooledUnsafeDirectByteBuf(null, maxCapacity); - } + PooledUnsafeDirectByteBuf buf = RECYCLER.get(); + buf.maxCapacity(maxCapacity); + return buf; } private long memoryAddress;