There are some decoders in Apache Mina.
Single byte decoder singlebytedecodingstate
Short-integer Decoder shortintegerdecodingstate
Integer decoder integerdecodingstate
Fixed length byte decoder fixedlengthdecodingstate
Server-side code:
public class Testserver{public static void Main (string[] args) throws ioexception{ioacceptor acceptor = new NIOSOCKETACCEP Tor (); Acceptor.getfilterchain (). AddLast ("Logger", New Loggingfilter ()), Acceptor.getfilterchain (). AddLast ("codec" , New Protocolcodecfilter (New Fixedlengthencoder (), New Fixedlengthdecoder ())); Acceptor.sethandler (new Fixedlengthiohandleradapter ()); Acceptor.bind (new Inetsocketaddress (9999));}}
Client code:
public class Testclient{public static void Main (string[] args) {niosocketconnector connector = new Niosocketconnector (); Connector.setconnecttimeoutmillis (10000); Connector.getfilterchain (). AddLast ("Codec", New Protocolcodecfilter (new Fixedlengthencoder (), New Fixedlengthdecoder ()); Connector.getfilterchain (). AddLast ("Logger", New Loggingfilter ()) ; Connector.sethandler (New Fixedlengthiohandleradapter ()); Iosession session;for (;;) {Try{connectfuture future = Connector.connect (new inetsocketaddress ("localhost", 9999)); future.awaituninterruptibly (); session = Future.getsession (); break;} catch (Runtimeioexception e) {e.printstacktrace (); Try{thread.sleep (5000);} catch (Interruptedexception E1) {//TODO auto-generated catch Blocke1.printstacktrace (); }}} for (int i = 0; i < i++) {session.write (i);} Wait until the summation is Donesession.getclosefuture (). awaituninterruptibly (); Connector.dispose ();}}
Fixed byte length decoder:
public class Fixedlengthdecoder extends cumulativeprotocoldecoder{private static final String prefix_decodingstate = " Prefix_decodingstate "; @Overrideprotected boolean Dodecode (iosession session, Iobuffer in, protocoldecoderoutput out) Throws Exception{fixedlengthdecodingstateimpl state = (Fixedlengthdecodingstateimpl) session.getattribute (PREFIX_ Decodingstate); if (state ==null) {State =new Fixedlengthdecodingstateimpl (4); Session.setattributeifabsent (prefix_decodingstate, State); } if (state!=null) { State.decode (in, out); if (State.getstate (). Equals (fixedlengthdecodingstateimpl.success)) { Session.removeattribute (prefix_ Decodingstate); State =null; return true; } } return false;}}
public class Fixedlengthdecodingstateimpl extends fixedlengthdecodingstate{public String state = "";p ublic final static S Tring SUCCESS = "SUCCESS";p ublic fixedlengthdecodingstateimpl (int length) {super (length);} Public String GetState () {return state;} public void SetState (String state) {this.state = state;} @Overrideprotected decodingstate Finishdecode (iobuffer product, protocoldecoderoutput out) throws exception{int i = Product.getint (); Out.write (i); this.state =fixedlengthdecodingstateimpl.success;return this;}}
Encoder:
public class Fixedlengthencoder extends protocolencoderadapter{@Overridepublic void encode (iosession session, Object Message, protocolencoderoutput out) throws exception{ Integer i = (integer) message; Iobuffer buf = iobuffer.allocate (4). Setautoexpand (true); Buf.putint (i); Buf.flip (); Out.write (BUF);}}
Business Logic Processing classes:
public class Fixedlengthiohandleradapter extends iohandleradapter{@Overridepublic void messagereceived (iosession Session, Object message) throws Exception{system.out.println ("messagereceived:" +message);}}
Apache Mina: Implementing fixed byte length encoding and decoding