Skip to content

Commit

Permalink
Issue #5198 - fix usage of inflater/deflater ByteBuffer API
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 9, 2020
1 parent 2ed89cb commit 4e6827c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ else if ((_flags & 0x2) == 0x2)

try
{
int pos = BufferUtil.flipToFill(buffer);
_inflater.inflate(buffer);
BufferUtil.flipToFlush(buffer, pos);
}
catch (DataFormatException x)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ public boolean mightCompress()

private class GzipBufferCB extends IteratingNestedCallback
{
private ByteBuffer _copy;
private final ByteBuffer _content;
private final boolean _last;

Expand All @@ -272,6 +271,10 @@ public GzipBufferCB(ByteBuffer content, boolean complete, Callback callback)
super(callback);
_content = content;
_last = complete;
_crc.update(_content.slice());
_deflater.setInput(_content);
if (_last)
_deflater.finish();
}

@Override
Expand All @@ -296,11 +299,6 @@ protected Action process() throws Exception
_channel.getByteBufferPool().release(_buffer);
_buffer = null;
}
if (_copy != null)
{
_channel.getByteBufferPool().release(_copy);
_copy = null;
}
return Action.SUCCEEDED;
}

Expand All @@ -320,29 +318,13 @@ protected Action process() throws Exception
// If the deflator is not finished, then compress more data
if (!_deflater.finished())
{
if (_deflater.needsInput())
{
// if there is no more content available to compress
// then we are either finished all content or just the current write.
if (BufferUtil.isEmpty(_content))
{
if (_last)
_deflater.finish();
else
return Action.SUCCEEDED;
}
else
{
// TODO: this might be a bad place to use ByteBuffer API, the CRC can copy the buffer anyway.
_crc.update(_content.slice());
_deflater.setInput(_content.slice());
if (_last)
_deflater.finish();
}
}
if (_deflater.needsInput() && !_last)
return Action.SUCCEEDED;

// deflate the content into the available space in the buffer
int pos = BufferUtil.flipToFill(_buffer);
_deflater.deflate(_buffer, _syncFlush ? Deflater.SYNC_FLUSH : Deflater.NO_FLUSH);
BufferUtil.flipToFlush(_buffer, pos);
}

// If we have finished deflation and there is room for the trailer.
Expand All @@ -363,11 +345,10 @@ protected Action process() throws Exception
@Override
public String toString()
{
return String.format("%s[content=%s last=%b copy=%s buffer=%s deflate=%s %s]",
return String.format("%s[content=%s last=%b buffer=%s deflate=%s %s]",
super.toString(),
BufferUtil.toDetailString(_content),
_last,
BufferUtil.toDetailString(_copy),
BufferUtil.toDetailString(_buffer),
_deflater,
_deflater != null && _deflater.finished() ? "(finished)" : "");
Expand Down

0 comments on commit 4e6827c

Please sign in to comment.