1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.mina.examples.reverser;
20
21 import org.apache.mina.common.ByteBuffer;
22 import org.apache.mina.protocol.ProtocolDecoder;
23 import org.apache.mina.protocol.ProtocolDecoderOutput;
24 import org.apache.mina.protocol.ProtocolSession;
25 import org.apache.mina.protocol.ProtocolViolationException;
26
27 /***
28 * Decodes a text line into a string.
29 *
30 * @author The Apache Directory Project (dev@directory.apache.org)
31 * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $,
32 */
33 public class TextLineDecoder implements ProtocolDecoder
34 {
35
36 private StringBuffer decodeBuf = new StringBuffer();
37
38 public void decode( ProtocolSession session, ByteBuffer in,
39 ProtocolDecoderOutput out )
40 throws ProtocolViolationException
41 {
42 do
43 {
44 byte b = in.get();
45 switch( b )
46 {
47 case '\r':
48 break;
49 case '\n':
50 String result = decodeBuf.toString();
51 decodeBuf.delete( 0, decodeBuf.length() );
52 out.write( result );
53 break;
54 default:
55 decodeBuf.append( ( char ) b );
56 }
57
58
59 if( decodeBuf.length() > 256 )
60 {
61 decodeBuf.delete( 0, decodeBuf.length() );
62 throw new ProtocolViolationException( "The line is too long." );
63 }
64 }
65 while( in.hasRemaining() );
66 }
67 }