Issue #5198 - use ByteBuffer API for inflater/deflaters for GzipHandler

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-09-08 17:10:36 +10:00
parent 3bdd82eb5e
commit 7a04b3eb19
2 changed files with 7 additions and 46 deletions

View File

@ -216,8 +216,7 @@ public class GZIPContentDecoder implements Destroyable
try try
{ {
int length = _inflater.inflate(buffer.array(), buffer.arrayOffset(), buffer.capacity()); _inflater.inflate(buffer);
buffer.limit(length);
} }
catch (DataFormatException x) catch (DataFormatException x)
{ {
@ -235,23 +234,10 @@ public class GZIPContentDecoder implements Destroyable
{ {
if (!compressed.hasRemaining()) if (!compressed.hasRemaining())
return; return;
if (compressed.hasArray()) _inflater.setInput(compressed);
{
_inflater.setInput(compressed.array(), compressed.arrayOffset() + compressed.position(), compressed.remaining());
compressed.position(compressed.limit());
}
else
{
// TODO use the pool
byte[] input = new byte[compressed.remaining()];
compressed.get(input);
_inflater.setInput(input);
}
} }
else if (_inflater.finished()) else if (_inflater.finished())
{ {
int remaining = _inflater.getRemaining();
compressed.position(compressed.limit() - remaining);
_state = State.CRC; _state = State.CRC;
_size = 0; _size = 0;
_value = 0; _value = 0;
@ -395,7 +381,6 @@ public class GZIPContentDecoder implements Destroyable
if (_value != (_inflater.getBytesWritten() & UINT_MAX)) if (_value != (_inflater.getBytesWritten() & UINT_MAX))
throw new ZipException("Invalid input size"); throw new ZipException("Invalid input size");
// TODO ByteBuffer result = output == null ? BufferUtil.EMPTY_BUFFER : ByteBuffer.wrap(output);
reset(); reset();
return; return;
} }

View File

@ -196,7 +196,6 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
contentLength = content.remaining(); contentLength = content.remaining();
_deflater = _factory.getDeflater(_channel.getRequest(), contentLength); _deflater = _factory.getDeflater(_channel.getRequest(), contentLength);
if (_deflater == null) if (_deflater == null)
{ {
LOG.debug("{} exclude no deflater", this); LOG.debug("{} exclude no deflater", this);
@ -334,39 +333,16 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
} }
else else
{ {
// If there is more content available to compress, we have to make sure // TODO: this might be a bad place to use ByteBuffer API, the CRC can copy the buffer anyway.
// it is available in an array for the current deflator API, maybe slicing _crc.update(_content.slice());
// of content. _deflater.setInput(_content.slice());
ByteBuffer slice; if (_last)
if (_content.hasArray())
slice = _content;
else
{
if (_copy == null)
_copy = _channel.getByteBufferPool().acquire(_bufferSize, false);
else
BufferUtil.clear(_copy);
slice = _copy;
BufferUtil.append(_copy, _content);
}
// transfer the data from the slice to the the deflator
byte[] array = slice.array();
int off = slice.arrayOffset() + slice.position();
int len = slice.remaining();
_crc.update(array, off, len);
_deflater.setInput(array, off, len); // TODO use ByteBuffer API in Jetty-10
slice.position(slice.position() + len);
if (_last && BufferUtil.isEmpty(_content))
_deflater.finish(); _deflater.finish();
} }
} }
// deflate the content into the available space in the buffer // deflate the content into the available space in the buffer
int off = _buffer.arrayOffset() + _buffer.limit(); _deflater.deflate(_buffer, _syncFlush ? Deflater.SYNC_FLUSH : Deflater.NO_FLUSH);
int len = BufferUtil.space(_buffer);
int produced = _deflater.deflate(_buffer.array(), off, len, _syncFlush ? Deflater.SYNC_FLUSH : Deflater.NO_FLUSH);
_buffer.limit(_buffer.limit() + produced);
} }
// If we have finished deflation and there is room for the trailer. // If we have finished deflation and there is room for the trailer.