Skip to content

Commit 3db4dc7

Browse files
committed
Refactored FIXMessageEncoder
1 parent a0aecea commit 3db4dc7

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

quickfixj-core/src/main/java/quickfix/mina/message/FIXMessageEncoder.java

+17-20
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package quickfix.mina.message;
2121

2222
import java.io.UnsupportedEncodingException;
23-
import java.util.Collections;
23+
import java.util.Arrays;
2424
import java.util.HashSet;
2525
import java.util.Set;
2626

@@ -39,16 +39,10 @@
3939
*/
4040
public class FIXMessageEncoder implements MessageEncoder<Object> {
4141

42-
private static final Set<Class<?>> TYPES;
42+
private static final Set<Class<?>> TYPES =
43+
new HashSet<>(Arrays.<Class<?>>asList(Message.class, String.class));
4344
private final String charsetEncoding;
4445

45-
static {
46-
Set<Class<?>> types = new HashSet<Class<?>>();
47-
types.add(Message.class);
48-
types.add(String.class);
49-
TYPES = Collections.unmodifiableSet(types);
50-
}
51-
5246
public FIXMessageEncoder() {
5347
charsetEncoding = CharsetSupport.getCharset();
5448
}
@@ -57,25 +51,28 @@ public static Set<Class<?>> getMessageTypes() {
5751
return TYPES;
5852
}
5953

54+
private byte[] toBytes(String str) throws ProtocolCodecException {
55+
try {
56+
return str.getBytes(charsetEncoding);
57+
} catch (UnsupportedEncodingException e) {
58+
throw new ProtocolCodecException(e);
59+
}
60+
}
61+
62+
@Override
6063
public void encode(IoSession session, Object message, ProtocolEncoderOutput out)
6164
throws ProtocolCodecException {
62-
String fixMessageString;
65+
// get message bytes
66+
byte[] bytes;
6367
if (message instanceof String) {
64-
fixMessageString = (String) message;
68+
bytes = toBytes((String) message);
6569
} else if (message instanceof Message) {
66-
fixMessageString = message.toString();
70+
bytes = toBytes(message.toString());
6771
} else {
6872
throw new ProtocolCodecException("Invalid FIX message object type: "
6973
+ message.getClass());
7074
}
71-
72-
byte[] bytes;
73-
try {
74-
bytes = fixMessageString.getBytes(charsetEncoding);
75-
} catch (UnsupportedEncodingException e) {
76-
throw new ProtocolCodecException(e);
77-
}
78-
75+
// write bytes to buffer and output it
7976
IoBuffer buffer = IoBuffer.allocate(bytes.length);
8077
buffer.put(bytes);
8178
buffer.flip();

0 commit comments

Comments
 (0)