Skip to content

Commit

Permalink
Issue #5287 - Changes from review & fix broken tests from NPE
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <[email protected]>
  • Loading branch information
lachlan-roberts committed Sep 23, 2020
1 parent 23c98c2 commit ae4ed15
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public GZIPContentDecoder(ByteBufferPool pool, int bufferSize)
public GZIPContentDecoder(InflaterPool inflaterPool, ByteBufferPool pool, int bufferSize)
{
_inflaterEntry = inflaterPool.acquire();
_inflater = _inflaterEntry.get();
_bufferSize = bufferSize;
_pool = pool;
reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void setCapacity(int capacity)
_capacity = capacity;
}

protected abstract T newObject();
protected abstract T newPooled();

protected abstract void end(T object);

Expand All @@ -70,15 +70,12 @@ public Entry acquire()
Entry entry = null;
if (_pool != null)
{
Pool<Entry>.Entry acquiredEntry = _pool.acquire(e -> new Entry(newObject()));
Pool<Entry>.Entry acquiredEntry = _pool.acquire(e -> new Entry(newPooled(), e));
if (acquiredEntry != null)
{
entry = acquiredEntry.getPooled();
entry.setEntry(acquiredEntry);
}
}

return (entry == null) ? new Entry(newObject()) : entry;
return (entry == null) ? new Entry(newPooled()) : entry;
}

/**
Expand Down Expand Up @@ -107,17 +104,20 @@ public void doStop() throws Exception

public class Entry implements Closeable
{
private T _value;
private Pool<Entry>.Entry _entry;
private final T _value;
private final Pool<Entry>.Entry _entry;

Entry(T value)
{
_value = value;
_entry = null;
this(value, null);
}

void setEntry(Pool<Entry>.Entry entry)
Entry(T value, Pool<Entry>.Entry entry)
{
if (entry != null && entry.getPooled() != value)
throw new IllegalArgumentException("value does not match pooled entry");

_value = value;
_entry = entry;
}

Expand All @@ -139,17 +139,13 @@ public void release()
if (_pool.remove(_entry))
close();
}

_entry = null;
}
}

@Override
public void close()
{
end(_value);
_value = null;
_entry = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public DeflaterPool(int capacity, int compressionLevel, boolean nowrap)
}

@Override
protected Deflater newObject()
protected Deflater newPooled()
{
return new Deflater(compressionLevel, nowrap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public InflaterPool(int capacity, boolean nowrap)
}

@Override
protected Inflater newObject()
protected Inflater newPooled()
{
return new Inflater(nowrap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public class PerMessageDeflateExtension extends AbstractExtension

private final TransformingFlusher outgoingFlusher;
private final TransformingFlusher incomingFlusher;
private DeflaterPool.Entry deflaterEntry;
private InflaterPool.Entry inflaterEntry;
private DeflaterPool.Entry deflaterHolder;
private InflaterPool.Entry inflaterHolder;
private boolean incomingCompressed;

private ExtensionConfig configRequested;
Expand Down Expand Up @@ -180,28 +180,34 @@ public static boolean endsWithTail(ByteBuffer buf)

public Deflater getDeflater()
{
if (deflaterEntry == null)
deflaterEntry = getDeflaterPool().acquire();
return deflaterEntry.get();
if (deflaterHolder == null)
deflaterHolder = getDeflaterPool().acquire();
return deflaterHolder.get();
}

public Inflater getInflater()
{
if (inflaterEntry == null)
inflaterEntry = getInflaterPool().acquire();
return inflaterEntry.get();
if (inflaterHolder == null)
inflaterHolder = getInflaterPool().acquire();
return inflaterHolder.get();
}

public void releaseInflater()
{
inflaterEntry.release();
inflaterEntry = null;
if (inflaterHolder != null)
{
inflaterHolder.release();
inflaterHolder = null;
}
}

public void releaseDeflater()
{
deflaterEntry.release();
deflaterEntry = null;
if (deflaterHolder != null)
{
deflaterHolder.release();
deflaterHolder = null;
}
}

@Override
Expand Down

0 comments on commit ae4ed15

Please sign in to comment.