4848public class FIXMessageDecoder implements MessageDecoder {
4949
5050 private static final char SOH = '\001' ;
51- private static final String FIELD_DELIMITER = String .valueOf (SOH );
5251
5352 private final Logger log = LoggerFactory .getLogger (getClass ());
5453
@@ -78,11 +77,11 @@ private void resetState() {
7877 }
7978
8079 public FIXMessageDecoder () throws UnsupportedEncodingException {
81- this (CharsetSupport .getCharset (), FIELD_DELIMITER );
80+ this (CharsetSupport .getCharset ());
8281 }
8382
8483 public FIXMessageDecoder (String charset ) throws UnsupportedEncodingException {
85- this (charset , FIELD_DELIMITER );
84+ this (charset , String . valueOf ( SOH ) );
8685 }
8786
8887 public FIXMessageDecoder (String charset , String delimiter ) throws UnsupportedEncodingException {
@@ -93,12 +92,14 @@ public FIXMessageDecoder(String charset, String delimiter) throws UnsupportedEnc
9392 resetState ();
9493 }
9594
95+ @ Override
9696 public MessageDecoderResult decodable (IoSession session , IoBuffer in ) {
9797 boolean hasHeader = HEADER_PATTERN .find (in , in .position ()) != -1L ;
9898 return hasHeader ? MessageDecoderResult .OK :
9999 (in .remaining () > MAX_UNDECODED_DATA_LENGTH ? MessageDecoderResult .NOT_OK : MessageDecoderResult .NEED_DATA );
100100 }
101101
102+ @ Override
102103 public MessageDecoderResult decode (IoSession session , IoBuffer in , ProtocolDecoderOutput out )
103104 throws ProtocolCodecException {
104105 int messageCount = 0 ;
@@ -148,9 +149,9 @@ private boolean parseMessage(IoBuffer in, ProtocolDecoderOutput out)
148149
149150 if (state == PARSING_LENGTH ) {
150151 byte ch = 0 ;
151- while (hasRemaining ( in )) {
152- ch = get (in );
153- if (! Character . isDigit (( char ) ch )) {
152+ while (position < in . limit ( )) { // while data remains
153+ ch = in . get (position ++ );
154+ if (ch < '0' || ch > '9' ) { // if not digit
154155 break ;
155156 }
156157 bodyLength = bodyLength * 10 + (ch - '0' );
@@ -161,7 +162,7 @@ private boolean parseMessage(IoBuffer in, ProtocolDecoderOutput out)
161162 log .debug ("body length = " + bodyLength + ": " + getBufferDebugInfo (in ));
162163 }
163164 } else {
164- if (hasRemaining ( in )) {
165+ if (position < in . limit ( )) { // if data remains
165166 String messageString = getMessageStringForError (in );
166167 handleError (in , in .position () + 1 , "Length format error in message (last character:" + ch + "): " + messageString ,
167168 false );
@@ -173,7 +174,7 @@ private boolean parseMessage(IoBuffer in, ProtocolDecoderOutput out)
173174 }
174175
175176 if (state == READING_BODY ) {
176- if (remaining ( in ) < bodyLength ) {
177+ if (in . limit () - position < bodyLength ) { // if remaining data is less than body
177178 break ;
178179 }
179180 position += bodyLength ;
@@ -223,9 +224,7 @@ private boolean parseMessage(IoBuffer in, ProtocolDecoderOutput out)
223224 }
224225 return messageFound ;
225226 } catch (Throwable t ) {
226- state = SEEKING_HEADER ;
227- position = 0 ;
228- bodyLength = 0 ;
227+ resetState ();
229228 if (t instanceof ProtocolCodecException ) {
230229 throw (ProtocolCodecException ) t ;
231230 } else {
@@ -234,23 +233,11 @@ private boolean parseMessage(IoBuffer in, ProtocolDecoderOutput out)
234233 }
235234 }
236235
237- private int remaining (IoBuffer in ) {
238- return in .limit () - position ;
239- }
240-
241236 private String getBufferDebugInfo (IoBuffer in ) {
242237 return "pos=" + in .position () + ",lim=" + in .limit () + ",rem=" + in .remaining ()
243238 + ",offset=" + position + ",state=" + state ;
244239 }
245240
246- private byte get (IoBuffer in ) {
247- return in .get (position ++);
248- }
249-
250- private boolean hasRemaining (IoBuffer in ) {
251- return position < in .limit ();
252- }
253-
254241 private String getMessageString (IoBuffer buffer ) throws UnsupportedEncodingException {
255242 byte [] data = new byte [position - buffer .position ()];
256243 buffer .get (data );
@@ -282,7 +269,8 @@ private boolean isLogon(IoBuffer buffer) {
282269 return LOGON_PATTERN .find (buffer , buffer .position ()) != -1L ;
283270 }
284271
285- public void finishDecode (IoSession arg0 , ProtocolDecoderOutput arg1 ) throws Exception {
272+ @ Override
273+ public void finishDecode (IoSession session , ProtocolDecoderOutput out ) throws Exception {
286274 // empty
287275 }
288276
@@ -309,6 +297,7 @@ public interface MessageListener {
309297 public List <String > extractMessages (File file ) throws IOException , ProtocolCodecException {
310298 final List <String > messages = new ArrayList <String >();
311299 extractMessages (file , new MessageListener () {
300+ @ Override
312301 public void onMessage (String message ) {
313302 messages .add (message );
314303 }
@@ -331,22 +320,23 @@ public void extractMessages(File file, final MessageListener listener) throws IO
331320 ProtocolCodecException {
332321 // Set up a read-only memory-mapped file
333322 RandomAccessFile fileIn = new RandomAccessFile (file , "r" );
334- FileChannel readOnlyChannel = fileIn .getChannel ();
335- MappedByteBuffer memoryMappedBuffer = readOnlyChannel .map (FileChannel .MapMode .READ_ONLY , 0 ,
336- (int ) readOnlyChannel .size ());
337-
338- decode (null , IoBuffer .wrap (memoryMappedBuffer ), new ProtocolDecoderOutput () {
339-
340- public void write (Object message ) {
341- listener .onMessage ((String ) message );
342- }
343-
344- public void flush (IoFilter .NextFilter nextFilter , IoSession ioSession ) {
345- // ignored
346- }
347- });
348- readOnlyChannel .close ();
349- fileIn .close ();
323+ try {
324+ FileChannel readOnlyChannel = fileIn .getChannel ();
325+ MappedByteBuffer memoryMappedBuffer = readOnlyChannel .map (FileChannel .MapMode .READ_ONLY , 0 ,
326+ (int ) readOnlyChannel .size ());
327+ decode (null , IoBuffer .wrap (memoryMappedBuffer ), new ProtocolDecoderOutput () {
328+ @ Override
329+ public void write (Object message ) {
330+ listener .onMessage ((String ) message );
331+ }
332+ @ Override
333+ public void flush (IoFilter .NextFilter nextFilter , IoSession ioSession ) {
334+ // ignored
335+ }
336+ });
337+ } finally {
338+ fileIn .close ();
339+ }
350340 }
351341
352342}
0 commit comments