001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.filter.codec.textline;
021:
022: import java.nio.charset.Charset;
023: import java.nio.charset.CharsetEncoder;
024:
025: import org.apache.mina.common.AttributeKey;
026: import org.apache.mina.common.IoBuffer;
027: import org.apache.mina.common.IoSession;
028: import org.apache.mina.filter.codec.ProtocolEncoder;
029: import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
030: import org.apache.mina.filter.codec.ProtocolEncoderOutput;
031:
032: /**
033: * A {@link ProtocolEncoder} which encodes a string into a text line
034: * which ends with the delimiter.
035: *
036: * @author The Apache MINA Project (dev@mina.apache.org)
037: * @version $Rev: 591182 $, $Date: 2007-11-01 18:59:27 -0600 (Thu, 01 Nov 2007) $,
038: */
039: public class TextLineEncoder extends ProtocolEncoderAdapter {
040: private final AttributeKey ENCODER = new AttributeKey(getClass(),
041: "encoder");
042:
043: private final Charset charset;
044:
045: private final LineDelimiter delimiter;
046:
047: private int maxLineLength = Integer.MAX_VALUE;
048:
049: /**
050: * Creates a new instance with the current default {@link Charset}
051: * and {@link LineDelimiter#UNIX} delimiter.
052: */
053: public TextLineEncoder() {
054: this (Charset.defaultCharset(), LineDelimiter.UNIX);
055: }
056:
057: /**
058: * Creates a new instance with the current default {@link Charset}
059: * and the specified <tt>delimiter</tt>.
060: */
061: public TextLineEncoder(String delimiter) {
062: this (new LineDelimiter(delimiter));
063: }
064:
065: /**
066: * Creates a new instance with the current default {@link Charset}
067: * and the specified <tt>delimiter</tt>.
068: */
069: public TextLineEncoder(LineDelimiter delimiter) {
070: this (Charset.defaultCharset(), delimiter);
071: }
072:
073: /**
074: * Creates a new instance with the spcified <tt>charset</tt>
075: * and {@link LineDelimiter#UNIX} delimiter.
076: */
077: public TextLineEncoder(Charset charset) {
078: this (charset, LineDelimiter.UNIX);
079: }
080:
081: /**
082: * Creates a new instance with the spcified <tt>charset</tt>
083: * and the specified <tt>delimiter</tt>.
084: */
085: public TextLineEncoder(Charset charset, String delimiter) {
086: this (charset, new LineDelimiter(delimiter));
087: }
088:
089: /**
090: * Creates a new instance with the spcified <tt>charset</tt>
091: * and the specified <tt>delimiter</tt>.
092: */
093: public TextLineEncoder(Charset charset, LineDelimiter delimiter) {
094: if (charset == null) {
095: throw new NullPointerException("charset");
096: }
097: if (delimiter == null) {
098: throw new NullPointerException("delimiter");
099: }
100: if (LineDelimiter.AUTO.equals(delimiter)) {
101: throw new IllegalArgumentException(
102: "AUTO delimiter is not allowed for encoder.");
103: }
104:
105: this .charset = charset;
106: this .delimiter = delimiter;
107: }
108:
109: /**
110: * Returns the allowed maximum size of the encoded line.
111: * If the size of the encoded line exceeds this value, the encoder
112: * will throw a {@link IllegalArgumentException}. The default value
113: * is {@link Integer#MAX_VALUE}.
114: */
115: public int getMaxLineLength() {
116: return maxLineLength;
117: }
118:
119: /**
120: * Sets the allowed maximum size of the encoded line.
121: * If the size of the encoded line exceeds this value, the encoder
122: * will throw a {@link IllegalArgumentException}. The default value
123: * is {@link Integer#MAX_VALUE}.
124: */
125: public void setMaxLineLength(int maxLineLength) {
126: if (maxLineLength <= 0) {
127: throw new IllegalArgumentException("maxLineLength: "
128: + maxLineLength);
129: }
130:
131: this .maxLineLength = maxLineLength;
132: }
133:
134: public void encode(IoSession session, Object message,
135: ProtocolEncoderOutput out) throws Exception {
136: CharsetEncoder encoder = (CharsetEncoder) session
137: .getAttribute(ENCODER);
138: if (encoder == null) {
139: encoder = charset.newEncoder();
140: session.setAttribute(ENCODER, encoder);
141: }
142:
143: String value = message.toString();
144: IoBuffer buf = IoBuffer.allocate(value.length()).setAutoExpand(
145: true);
146: buf.putString(value, encoder);
147: if (buf.position() > maxLineLength) {
148: throw new IllegalArgumentException("Line length: "
149: + buf.position());
150: }
151: buf.putString(delimiter.getValue(), encoder);
152: buf.flip();
153: out.write(buf);
154: }
155:
156: public void dispose() throws Exception {
157: }
158: }
|