From 7a04b3eb19820c66b65d026cfd972a49689dc582 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Tue, 8 Sep 2020 17:10:36 +1000 Subject: [PATCH] Issue #5198 - use ByteBuffer API for inflater/deflaters for GzipHandler Signed-off-by: Lachlan Roberts --- .../jetty/http/GZIPContentDecoder.java | 19 ++--------- .../gzip/GzipHttpOutputInterceptor.java | 34 +++---------------- 2 files changed, 7 insertions(+), 46 deletions(-) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/GZIPContentDecoder.java b/jetty-http/src/main/java/org/eclipse/jetty/http/GZIPContentDecoder.java index 8a2cd758c86..dc30bfd2899 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/GZIPContentDecoder.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/GZIPContentDecoder.java @@ -216,8 +216,7 @@ public class GZIPContentDecoder implements Destroyable try { - int length = _inflater.inflate(buffer.array(), buffer.arrayOffset(), buffer.capacity()); - buffer.limit(length); + _inflater.inflate(buffer); } catch (DataFormatException x) { @@ -235,23 +234,10 @@ public class GZIPContentDecoder implements Destroyable { if (!compressed.hasRemaining()) return; - if (compressed.hasArray()) - { - _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); - } + _inflater.setInput(compressed); } else if (_inflater.finished()) { - int remaining = _inflater.getRemaining(); - compressed.position(compressed.limit() - remaining); _state = State.CRC; _size = 0; _value = 0; @@ -395,7 +381,6 @@ public class GZIPContentDecoder implements Destroyable if (_value != (_inflater.getBytesWritten() & UINT_MAX)) throw new ZipException("Invalid input size"); - // TODO ByteBuffer result = output == null ? BufferUtil.EMPTY_BUFFER : ByteBuffer.wrap(output); reset(); return; } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHttpOutputInterceptor.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHttpOutputInterceptor.java index 7a39f160b95..48271d6f6bb 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHttpOutputInterceptor.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHttpOutputInterceptor.java @@ -196,7 +196,6 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor contentLength = content.remaining(); _deflater = _factory.getDeflater(_channel.getRequest(), contentLength); - if (_deflater == null) { LOG.debug("{} exclude no deflater", this); @@ -334,39 +333,16 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor } else { - // If there is more content available to compress, we have to make sure - // it is available in an array for the current deflator API, maybe slicing - // of content. - ByteBuffer slice; - 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)) + // 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(); } } // deflate the content into the available space in the buffer - int off = _buffer.arrayOffset() + _buffer.limit(); - 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); + _deflater.deflate(_buffer, _syncFlush ? Deflater.SYNC_FLUSH : Deflater.NO_FLUSH); } // If we have finished deflation and there is room for the trailer.