Make PooledByteBuf recyclable regardless its maxCapacity

- Make AbstractByteBuf.maxCapacity internally mutable so that PooledByteBuf is completely recyclable
This commit is contained in:
Trustin Lee 2013-06-12 04:18:40 +09:00
parent 9396246fe9
commit 2d7c6f8ee1
4 changed files with 18 additions and 20 deletions

View File

@ -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;

View File

@ -31,16 +31,14 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
private static final Recycler<PooledDirectByteBuf> RECYCLER = new Recycler<PooledDirectByteBuf>() {
@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) {

View File

@ -30,16 +30,14 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
private static final Recycler<PooledHeapByteBuf> RECYCLER = new Recycler<PooledHeapByteBuf>() {
@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) {

View File

@ -35,16 +35,14 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
private static final Recycler<PooledUnsafeDirectByteBuf> RECYCLER = new Recycler<PooledUnsafeDirectByteBuf>() {
@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;