@Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        Object out = msg;
        int writeIdx = 0;
        int bytesWritten = 0;

        if (msg instanceof HttpResponse && (((HttpResponse) msg).headers().contains(HttpHeaderNames.CONTENT_ENCODING))) {
            ctx.attr(DEFLATER).set(new Deflater());
        }

        if (msg instanceof HttpContent) {
            final ByteBuf chunk = ((HttpContent) msg).content();
            final Deflater compressor = ctx.attr(DEFLATER).get();
            if (null == compressor) {
                super.write(ctx, msg, promise);
                return;
            }

            compressor.setInput(ByteBufUtil.getBytes(chunk)); // need to copy bytes as it may be in a direct buffer.
            if (msg instanceof LastHttpContent) { compressor.finish(); }

            final int compressedSizeEstimate = chunk.readableBytes() / 8;
            byte[] outBuf = new byte[Math.max(compressedSizeEstimate, MIN_BUFFER_SIZE)];
            // Need to SYNC_FLUSH to ensure that each chunk's data is completely compressed.
            bytesWritten = compressor.deflate(outBuf, writeIdx, outBuf.length, Deflater.SYNC_FLUSH);

            while (bytesWritten == (outBuf.length - writeIdx)) {
                writeIdx += bytesWritten;
                outBuf = Arrays.copyOf(outBuf, outBuf.length * 2);
                bytesWritten = compressor.deflate(outBuf, writeIdx, outBuf.length - writeIdx, Deflater.SYNC_FLUSH);
            }

            writeIdx += bytesWritten;
            out = ((HttpContent) msg).replace(Unpooled.wrappedBuffer(outBuf).writerIndex(writeIdx));
            ReferenceCountUtil.release(msg);

            if (out instanceof LastHttpContent) { ctx.attr(DEFLATER).getAndSet(null).end(); }
        }

        super.write(ctx, out, promise);
    }
Handles compression of content when the response contains the "Content-Encoding" header. Currently supports "deflate".
This class is specifically meant to work with serialized GraphBinary chunks as they need to be deflated per chunk such
that the chunk ends with a compressed Marker.END_OF_STREAM.
gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/handler/HttpContentCompressionHandler.java