package net.md_5.bungee.jni.zlib;

import io.netty.buffer.ByteBuf;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class JavaZlib implements BungeeZlib
{

    private final byte[] buffer = new byte[ 8192 ];
    //
    private boolean compress;
    private Deflater deflater;
    private Inflater inflater;

    @Override
    public void init(boolean compress, int level)
    {
        this.compress = compress;
        free();

        if ( compress )
        {
            deflater = new Deflater( level );
        } else
        {
            inflater = new Inflater();
        }
    }

    @Override
    public void free()
    {
        if ( deflater != null )
        {
            deflater.end();
        }
        if ( inflater != null )
        {
            inflater.end();
        }
    }

    @Override
    public void process(ByteBuf in, ByteBuf out) throws DataFormatException
    {
        byte[] inData = new byte[ in.readableBytes() ];
        in.readBytes( inData );

        if ( compress )
        {
            deflater.setInput( inData );
            deflater.finish();

            while ( !deflater.finished() )
            {
                int count = deflater.deflate( buffer );
                out.writeBytes( buffer, 0, count );
            }

            deflater.reset();
        } else
        {
            inflater.setInput( inData );

            while ( !inflater.finished() )
            {
                int count = inflater.inflate( buffer );
                out.writeBytes( buffer, 0, count );
            }

            inflater.reset();
        }
    }
}

@@ -228,46 +228,194 @@
 ate 
-final byte%5B%5D buffer = new byte%5B 8192 %5D
+static final ThreadLocal%3Cbyte%5B%5D%3E heapOutLocal = new ThreadLocal%3Cbyte%5B%5D%3E() %7B%0A        @Override%0A        protected byte%5B%5D initialValue() %7B%0A            return new byte%5B8192%5D;%0A        %7D%0A    %7D
 ;%0A  
@@ -1181,32 +1181,77 @@
 tes( inData );%0A%0A
+        byte%5B%5D buffer = heapOutLocal.get();%0A%0A
         if ( com

Implement ThreadLocal for JavaZlib's temporary output buffer.