Reduce ram usage by ~65kb / player. See #229

This commit is contained in:
md_5 2013-04-11 20:30:07 +10:00
parent 19c3c23b86
commit 88c99f071e

View File

@ -16,8 +16,18 @@ public class CipherCodec extends ByteToByteCodec
private Cipher encrypt; private Cipher encrypt;
private Cipher decrypt; private Cipher decrypt;
private byte[] heapIn = new byte[ 0 ]; private ThreadLocal<byte[]> heapInLocal = new EmptyByteThreadLocal();
private byte[] heapOut = new byte[ 0 ]; private ThreadLocal<byte[]> heapOutLocal = new EmptyByteThreadLocal();
private static class EmptyByteThreadLocal extends ThreadLocal<byte[]>
{
@Override
protected byte[] initialValue()
{
return new byte[ 0 ];
}
}
public CipherCodec(Cipher encrypt, Cipher decrypt) public CipherCodec(Cipher encrypt, Cipher decrypt)
{ {
@ -39,6 +49,7 @@ public class CipherCodec extends ByteToByteCodec
private void cipher(ByteBuf in, ByteBuf out, Cipher cipher) throws ShortBufferException private void cipher(ByteBuf in, ByteBuf out, Cipher cipher) throws ShortBufferException
{ {
byte[] heapIn = heapInLocal.get();
int readableBytes = in.readableBytes(); int readableBytes = in.readableBytes();
if ( heapIn.length < readableBytes ) if ( heapIn.length < readableBytes )
{ {
@ -46,6 +57,7 @@ public class CipherCodec extends ByteToByteCodec
} }
in.readBytes( heapIn, 0, readableBytes ); in.readBytes( heapIn, 0, readableBytes );
byte[] heapOut = heapOutLocal.get();
int outputSize = cipher.getOutputSize( readableBytes ); int outputSize = cipher.getOutputSize( readableBytes );
if ( heapOut.length < outputSize ) if ( heapOut.length < outputSize )
{ {