1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.filter.codec.textline;
21
22 import java.nio.charset.Charset;
23 import java.nio.charset.CharsetEncoder;
24
25 import org.apache.mina.core.buffer.IoBuffer;
26 import org.apache.mina.core.session.AttributeKey;
27 import org.apache.mina.core.session.IoSession;
28 import org.apache.mina.filter.codec.ProtocolEncoder;
29 import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
30 import org.apache.mina.filter.codec.ProtocolEncoderOutput;
31
32
33
34
35
36
37
38 public class TextLineEncoder extends ProtocolEncoderAdapter {
39 private static final AttributeKeyAttributeKey.html#AttributeKey">AttributeKey ENCODER = new AttributeKey(TextLineEncoder.class, "encoder");
40
41 private final Charset charset;
42
43 private final LineDelimiter delimiter;
44
45 private int maxLineLength = Integer.MAX_VALUE;
46
47
48
49
50
51 public TextLineEncoder() {
52 this(Charset.defaultCharset(), LineDelimiter.UNIX);
53 }
54
55
56
57
58
59
60
61 public TextLineEncoder(String delimiter) {
62 this(new LineDelimiter(delimiter));
63 }
64
65
66
67
68
69
70
71 public TextLineEncoder(LineDelimiter delimiter) {
72 this(Charset.defaultCharset(), delimiter);
73 }
74
75
76
77
78
79
80
81 public TextLineEncoder(Charset charset) {
82 this(charset, LineDelimiter.UNIX);
83 }
84
85
86
87
88
89
90
91
92 public TextLineEncoder(Charset charset, String delimiter) {
93 this(charset, new LineDelimiter(delimiter));
94 }
95
96
97
98
99
100
101
102
103 public TextLineEncoder(Charset charset, LineDelimiter delimiter) {
104 if (charset == null) {
105 throw new IllegalArgumentException("charset");
106 }
107 if (delimiter == null) {
108 throw new IllegalArgumentException("delimiter");
109 }
110 if (LineDelimiter.AUTO.equals(delimiter)) {
111 throw new IllegalArgumentException("AUTO delimiter is not allowed for encoder.");
112 }
113
114 this.charset = charset;
115 this.delimiter = delimiter;
116 }
117
118
119
120
121
122
123
124 public int getMaxLineLength() {
125 return maxLineLength;
126 }
127
128
129
130
131
132
133
134
135
136 public void setMaxLineLength(int maxLineLength) {
137 if (maxLineLength <= 0) {
138 throw new IllegalArgumentException("maxLineLength: " + maxLineLength);
139 }
140
141 this.maxLineLength = maxLineLength;
142 }
143
144
145
146
147 @Override
148 public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
149 CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER);
150
151 if (encoder == null) {
152 encoder = charset.newEncoder();
153 session.setAttribute(ENCODER, encoder);
154 }
155
156 String value = message == null ? "" : message.toString();
157 IoBuffer buf = IoBuffer.allocate(value.length()).setAutoExpand(true);
158 buf.putString(value, encoder);
159
160 if (buf.position() > maxLineLength) {
161 throw new IllegalArgumentException("Line length: " + buf.position());
162 }
163
164 buf.putString(delimiter.getValue(), encoder);
165 buf.flip();
166 out.write(buf);
167 }
168
169
170
171
172
173
174 public void dispose() throws Exception {
175
176 }
177 }