1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.wallace.mina;
20
21 import java.io.UnsupportedEncodingException;
22
23 import net.sf.wallace.ServerResponse;
24
25 import org.apache.mina.common.ByteBuffer;
26 import org.apache.mina.protocol.ProtocolEncoder;
27 import org.apache.mina.protocol.ProtocolEncoderOutput;
28 import org.apache.mina.protocol.ProtocolSession;
29 import org.apache.mina.protocol.ProtocolViolationException;
30
31 /***
32 * IMAP4 protocol encoder.
33 *
34 * @author rnewson
35 */
36 public final class IMAP4ProtocolEncoder implements ProtocolEncoder {
37
38 public void encode(final ProtocolSession protocolSession, final Object object, final ProtocolEncoderOutput output)
39 throws ProtocolViolationException {
40 assert protocolSession != null;
41 assert object instanceof ServerResponse : "IMAP4ProtocolEncoder can only encode ServerResponse objects";
42 final ServerResponse message = (ServerResponse) object;
43
44 final String val = message.toString();
45 final ByteBuffer buffer = ByteBuffer.allocate(val.length());
46 try {
47 buffer.put(val.getBytes("US-ASCII"));
48 } catch (UnsupportedEncodingException e) {
49 throw new ProtocolViolationException("Unable to convert to ASCII.", e);
50 }
51 buffer.flip();
52 output.write(buffer);
53 }
54
55 }