As we thought, what should I do if the content of a text cannot be parsed at a time? The answer is to use an object to save these unresolved objects for parsing. This object is generally stored in the session.
1. Text Encoder
Package COM. boonya. protocol. codec. self; import Java. NIO. charset. charset; import Org. apache. mina. core. buffer. iobuffer; import Org. apache. mina. core. session. iosession; import Org. apache. mina. filter. codec. protocolencoder; import Org. apache. mina. filter. codec. protocolencoderoutput; public class mytextlinecodecencoder implements protocolencoder {private charset; // encoding format private string delimiter; // text delimiter public mytextlinecodecencoder (charset, string delimiter) {This. charset = charset; this. delimiter = delimiter;} public void encode (iosession session, object message, protocolencoderoutput out) throws exception {If (delimiter = NULL | "". equals (delimiter) {// if the text line break is not specified, use the default value delimiter = "\ r \ n";} If (charset = NULL) {charset = charset. forname ("UTF-8");} string value = message. tostring (); iobuffer Buf = iobuffer. allocate (value. length ()). setautoexpand (true); Buf. putstring (value, charset. newencoder (); // real data Buf. putstring (delimiter, charset. newencoder (); // text line break Buf. flip (); out. write (BUF);} public void dispose (iosession session) throws exception {}}
2. Text decoder [decoding is a Singleton, so session should be used to process Decoding of different objects]
Package COM. boonya. protocol. codec. self; import Java. NIO. charset. charactercodingexception; import Java. NIO. charset. charset; import Java. NIO. charset. charsetdecoder; import Org. apache. mina. core. buffer. iobuffer; import Org. apache. mina. core. session. iosession; import Org. apache. mina. filter. codec. protocoldecoder; import Org. apache. mina. filter. codec. protocoldecoderoutput; public class mytextlinecodecdecoder implements protocoldecoder {private charset; // encoding format private string delimiter; // text separator private iobuffer delimbuf; // variables matching text delimiters // defines the constant value, which is used as the key value for saving the decoding task in each iosession. Private Static string context = mytextlinecodecdecoder. class. getname () + ". context "; // constructor. You must specify the charset and text separator public mytextlinecodecdecoder (charset, string delimiter) {This. charset = charset; this. delimiter = delimiter;} public void decode (iosession session, iobuffer in, protocoldecoderoutput out) throws exception {context CTX = getcontext (session); If (delimiter = NULL | "". equals (delimiter) {// if the text line break is not specified, use the default value delimiter = "\ r \ n";} If (charset = NULL) {charset = charset. forname ("UTF-8");} decodenormal (CTX, in, out);} // obtain the context object private context getcontext (iosession session) {context CTX from iosession; CTX = (context) session. getattribute (context); If (CTX = NULL) {CTX = new context (); Session. setattribute (context, CTX) ;}return CTX ;}// decode private void decodenormal (context CTX, iobuffer in, protocoldecoderoutput out) throws charactercodingexception {// retrieves the number of text linefeeds that have been matched in the unfinished task int matchcount = CTX. getmatchcount (); // set the iobuffer variable if (delimbuf = NULL) {iobuffer TMP = iobuffer. allocate (2 ). setautoexpand (true); TMP. putstring (delimiter, charset. newencoder (); TMP. flip (); delimbuf = TMP;} int oldpos = in. position (); // the original data information in the decoded iobuffer int oldlimit = in. limit (); While (in. hasremaining () {// variable decoded iobuffer byte B = in. get (); If (delimbuf. get (matchcount) = B) {// match the matchcount linefeed successful matchcount ++; If (matchcount = delimbuf. limit () {// The number of bytes currently matched is the same as the number of bytes of the text line break. The match ends with int Pos = in. position (); // obtain the currently matched position (all data before position is valid) in. limit (POS); In. position (oldpos); // position returns to the original position CTX. append (in); // append to the end of the incomplete data in the context object. limit (oldlimit); // the remaining data after the match in. position (POS); iobuffer Buf = CTX. getbuf (); Buf. flip (); Buf. limit (BUF. limit ()-matchcount); // remove the text line break try {out. write (BUF. getstring (CTX. getdecoder (); // output decoded content} finally {Buf. clear (); // release cache space} oldpos = Pos; matchcount = 0 ;}} else {// If matchcount = 0, continue matching // If matchcount> 0, it indicates that the next byte that matches the successful byte before the text line break is not matched. // jump to the character that fails to match, and set matchcount = 0 to continue matching in. position (in. position ()-matchcount); matchcount = 0; // after the match is successful, matchcount is left blank} // put undecoded content in the in statement into the Buf. position (oldpos); CTX. append (in); CTX. setmatchcount (matchcount);} public void dispose (iosession session) throws exception {} public void finishdecode (iosession session, protocoldecoderoutput out) throws exception {} // internal class, private class context {private charsetdecoder decoder; private iobuffer Buf; // Save the actual decoding content private int matchcount = 0; // The number of matched text linefeeds private context () {decoder = charset. newdecoder (); Buf = iobuffer. allocate (80 ). setautoexpand (true);} // reset @ suppresswarnings ("UNUSED") Public void reset () {matchcount = 0; decoder. reset ();} // append public void append (iobuffer in) {getbuf (). put (in );} // ======= get/Set Method ================================== public charsetdecoder getdecoder () {return decoder;} public iobuffer getbuf () {return Buf;} public int getmatchcount () {return matchcount;} public void setmatchcount (INT matchcount) {This. matchcount = matchcount; }}// end class context ;}
We recommend that you check whether the decoded object is the current object and avoid any errors in the decoding process.