package test.net.mina.codec; import org.apache.mina.core.buffer.IoBuffer; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.CumulativeProtocolDecoder; import org.apache.mina.filter.codec.ProtocolDecoderOutput; public class MyMinaDecoder extends CumulativeProtocolDecoder { public static final int MSG_HEADER_SIZE = 14; @Override protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { // try to read the message header if (in.remaining() >= MSG_HEADER_SIZE) { out.write(readsUnsignedBytesToString(in, MSG_HEADER_SIZE)); return true; } else { // not enough data return false; } } private String readsUnsignedBytesToString(IoBuffer in, int length) { char[] unsignedChars = new char[length]; for (int i = 0; i < length; i++) { unsignedChars[i] = (char) in.getUnsigned(); } return new String(unsignedChars); } }