Details
Description
FIXMessageEncoder#encode() may throws protocol handler exception: java.nio.BufferOverflowException, if message contains Chinese characters.
we use "UTF-8" as default charset instead of "ISO-8859-1". So charsetEncoding = "UTF-8".
FIXMessageEncoder#encode() :
ByteBuffer buffer = ByteBuffer.allocate(fixMessageString.length());
try
catch (UnsupportedEncodingException e)
{ throw new ProtocolCodecException(e); }if message contains Chinese characters, fixMessageString.length() will not equal to fixMessageString.getBytes(charsetEncoding).length. Because A Chinese character will use "three" Bytes in "UTF-8". But in String, it's length is still "1".
we let: byte[] src = fixMessageString.getBytes(charsetEncoding);
when a message contains "one" Chinese character,
if fixMessageString.length() = 190, then src.length = 192, buffer.capacity() = 256. So buffer.put(src) will not throw java.nio.BufferOverflowException;
if fixMessageString.length() = 255, then src.length = 257, buffer.capacity() = 256. At this time, buffer.put(src) will throw java.nio.BufferOverflowException;
solution:
use "fixMessageString.getBytes(charsetEncoding).length" to allocate buffer space instead of "fixMessageString.length()".
ByteBuffer buffer;
try
catch (UnsupportedEncodingException e)
{ throw new ProtocolCodecException(e); }