20
20
package quickfix .mina .message ;
21
21
22
22
import java .io .UnsupportedEncodingException ;
23
- import java .util .Collections ;
23
+ import java .util .Arrays ;
24
24
import java .util .HashSet ;
25
25
import java .util .Set ;
26
26
39
39
*/
40
40
public class FIXMessageEncoder implements MessageEncoder <Object > {
41
41
42
- private static final Set <Class <?>> TYPES ;
42
+ private static final Set <Class <?>> TYPES =
43
+ new HashSet <>(Arrays .<Class <?>>asList (Message .class , String .class ));
43
44
private final String charsetEncoding ;
44
45
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
-
52
46
public FIXMessageEncoder () {
53
47
charsetEncoding = CharsetSupport .getCharset ();
54
48
}
@@ -57,25 +51,28 @@ public static Set<Class<?>> getMessageTypes() {
57
51
return TYPES ;
58
52
}
59
53
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
60
63
public void encode (IoSession session , Object message , ProtocolEncoderOutput out )
61
64
throws ProtocolCodecException {
62
- String fixMessageString ;
65
+ // get message bytes
66
+ byte [] bytes ;
63
67
if (message instanceof String ) {
64
- fixMessageString = ( String ) message ;
68
+ bytes = toBytes (( String ) message ) ;
65
69
} else if (message instanceof Message ) {
66
- fixMessageString = message .toString ();
70
+ bytes = toBytes ( message .toString () );
67
71
} else {
68
72
throw new ProtocolCodecException ("Invalid FIX message object type: "
69
73
+ message .getClass ());
70
74
}
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
79
76
IoBuffer buffer = IoBuffer .allocate (bytes .length );
80
77
buffer .put (bytes );
81
78
buffer .flip ();
0 commit comments