[QFJ-282] FIXMessageEncoder#encode() may throws java.nio.BufferOverflowException if message contains Chinese characters Created: 27/Dec/07  Updated: 02/Apr/15  Resolved: 09/Jun/14

Status: Closed
Project: QuickFIX/J
Component/s: Engine
Affects Version/s: 1.2.1
Fix Version/s: 1.6.0

Type: Bug Priority: Default
Reporter: CaiQi Assignee: amichair
Resolution: Duplicate Votes: 0
Labels: encoding

Issue Links:
Duplicate
duplicates QFJ-666 FIXMessageEncoder got BufferOverflowE... Closed
Relates
relates to QFJ-382 Foreign Language Support - Multibyte ... Closed

 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

{ buffer.put(fixMessageString.getBytes(charsetEncoding)); }

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

{ byte[] src = fixMessageString.getBytes(charsetEncoding); buffer = ByteBuffer.allocate(src.length); buffer.put(src); }

catch (UnsupportedEncodingException e)

{ throw new ProtocolCodecException(e); }

 Comments   
Comment by Jan Amoyo [ 29/Apr/08 ]

One can also use the CharsetEncoder in java.nio.charset:

Charset charset = Charset.forName("UTF-8");
CharsetEncoder encoder = charset.newEncoder();

CharBuffer cb = CharBuffer.wrap("fixMessageString");
ByteBuffer buffer = encoder.encode(cb);

Comment by Jay Walters [ 03/Feb/09 ]

See QFJ-382 for the patches to resolve this issue.

Generated at Sat Nov 23 10:58:06 UTC 2024 using JIRA 7.5.2#75007-sha1:9f5725bb824792b3230a5d8716f0c13e296a3cae.