1 20 package org.apache.mina.example.sumup.codec; 21 22 import org.apache.mina.common.ByteBuffer; 23 import org.apache.mina.common.IoSession; 24 import org.apache.mina.example.sumup.message.AbstractMessage; 25 import org.apache.mina.example.sumup.message.ResultMessage; 26 import org.apache.mina.filter.codec.ProtocolDecoderOutput; 27 import org.apache.mina.filter.codec.demux.MessageDecoder; 28 29 35 public class ResultMessageDecoder extends AbstractMessageDecoder { 36 private int code; 37 38 private boolean readCode; 39 40 public ResultMessageDecoder() { 41 super(Constants.RESULT); 42 } 43 44 protected AbstractMessage decodeBody(IoSession session, ByteBuffer in) { 45 if (!readCode) { 46 if (in.remaining() < Constants.RESULT_CODE_LEN) { 47 return null; } 49 50 code = in.getShort(); 51 readCode = true; 52 } 53 54 if (code == Constants.RESULT_OK) { 55 if (in.remaining() < Constants.RESULT_VALUE_LEN) { 56 return null; 57 } 58 59 ResultMessage m = new ResultMessage(); 60 m.setOk(true); 61 m.setValue(in.getInt()); 62 readCode = false; 63 return m; 64 } else { 65 ResultMessage m = new ResultMessage(); 66 m.setOk(false); 67 readCode = false; 68 return m; 69 } 70 } 71 72 public void finishDecode(IoSession session, ProtocolDecoderOutput out) 73 throws Exception { 74 } 75 } 76 | Popular Tags |