Update CipherCodec to try and fix issues.

This commit is contained in:
md_5 2013-03-12 11:21:39 +11:00
parent f9f664f9b6
commit 3b90737273

View File

@ -4,7 +4,6 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToByteCodec; import io.netty.handler.codec.ByteToByteCodec;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.ShortBufferException;
/** /**
* This class is a complete solution for encrypting and decoding bytes in a * This class is a complete solution for encrypting and decoding bytes in a
@ -35,16 +34,24 @@ public class CipherCodec extends ByteToByteCodec
cipher( decrypt, in, out ); cipher( decrypt, in, out );
} }
private void cipher(Cipher cipher, ByteBuf in, ByteBuf out) throws ShortBufferException private void cipher(Cipher cipher, ByteBuf in, ByteBuf out) throws Exception
{ {
int available = in.readableBytes(); try
int outputSize = cipher.getOutputSize( available );
if ( out.capacity() < outputSize )
{ {
out.capacity( outputSize ); int available = in.readableBytes();
int outputSize = cipher.getOutputSize( available );
int writerIndex = out.writerIndex();
if ( out.capacity() + writerIndex < outputSize )
{
out.capacity( outputSize + writerIndex );
}
int processed = cipher.update( in.nioBuffer(), out.nioBuffer( out.writerIndex(), outputSize ) );
in.readerIndex( in.readerIndex() + processed );
out.writerIndex( writerIndex + processed );
} catch ( Exception ex )
{
ex.printStackTrace();
throw ex;
} }
int processed = cipher.update( in.nioBuffer(), out.nioBuffer( out.writerIndex(), outputSize ) );
in.readerIndex( in.readerIndex() + processed );
out.writerIndex( out.writerIndex() + processed );
} }
} }