直接下载的Quickfix 是不直接支持Fix 消息里面含有中文的,仔细研究一下只要修改几处地方,就可以成功支持中文。
1. quickfix.Field
/*package*/ int getLength() {
calculate();
//change 因为不同的编码方式对应的长度字节数不一样,所以应该用字节长度。
// //return data.length()+1;
//to
return (data.getBytes().length+1);
}
2. quickfix.mina.message.FIXMessageEncoder
public void encode(IoSession session, Object message, ProtocolEncoderOutput out)
throws ProtocolCodecException {
String fixMessageString;
if (message instanceof String) {
fixMessageString = (String) message;
} else {
try {
fixMessageString = ((Message) message).toString();
} catch (ClassCastException e) {
throw new ProtocolCodecException("Invalid FIX message object type: "
+ message.getClass(), e);
}
}
//分配字节数,要用字符串字节数组来分配。
//ByteBuffer buffer = ByteBuffer.allocate(fixMessageString.length());
ByteBuffer buffer =null;
try {
buffer= ByteBuffer.allocate(fixMessageString.getBytes(charsetEncoding).length);
buffer.put(fixMessageString.getBytes(charsetEncoding));
//buffer = ByteBuffer.wrap(fixMessageString.getBytes(charsetEncoding));
} catch (UnsupportedEncodingException e) {
throw new ProtocolCodecException(e);
}
buffer.flip();
out.write(buffer);
}
3.org.quickfixj.CharsetSupport
public static String getDefaultCharset() {
//return "ISO-8859-1";
//换成中文编码
return "GBK";
}