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.filter.codec.ProtocolEncoderOutput; 26 import org.apache.mina.filter.codec.demux.MessageEncoder; 27 28 35 public abstract class AbstractMessageEncoder implements MessageEncoder { 36 private final int type; 37 38 protected AbstractMessageEncoder(int type) { 39 this.type = type; 40 } 41 42 public void encode(IoSession session, Object message, 43 ProtocolEncoderOutput out) throws Exception { 44 AbstractMessage m = (AbstractMessage) message; 45 ByteBuffer buf = ByteBuffer.allocate(16); 46 buf.setAutoExpand(true); 48 buf.putShort((short) type); 50 buf.putInt(m.getSequence()); 51 52 encodeBody(session, m, buf); 54 buf.flip(); 55 out.write(buf); 56 } 57 58 protected abstract void encodeBody(IoSession session, 59 AbstractMessage message, ByteBuffer out); 60 } 61 | Popular Tags |