1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.mina.filter.codec.textline; 21 22 import java.nio.charset.Charset; 23 24 import org.apache.mina.core.buffer.BufferDataException; 25 import org.apache.mina.core.session.IoSession; 26 import org.apache.mina.filter.codec.ProtocolCodecFactory; 27 import org.apache.mina.filter.codec.ProtocolDecoder; 28 import org.apache.mina.filter.codec.ProtocolEncoder; 29 30 /** 31 * A {@link ProtocolCodecFactory} that performs encoding and decoding between 32 * a text line data and a Java string object. This codec is useful especially 33 * when you work with a text-based protocols such as SMTP and IMAP. 34 * 35 * @author The Apache MINA Project (dev@mina.apache.org) 36 * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $ 37 */ 38 public class TextLineCodecFactory implements ProtocolCodecFactory { 39 40 private final TextLineEncoder encoder; 41 private final TextLineDecoder decoder; 42 43 /** 44 * Creates a new instance with the current default {@link Charset}. 45 */ 46 public TextLineCodecFactory() { 47 this(Charset.defaultCharset()); 48 } 49 50 /** 51 * Creates a new instance with the specified {@link Charset}. The 52 * encoder uses a UNIX {@link LineDelimiter} and the decoder uses 53 * the AUTO {@link LineDelimiter}. 54 * 55 * @param charset 56 * The charset to use in the encoding and decoding 57 */ 58 public TextLineCodecFactory(Charset charset) { 59 encoder = new TextLineEncoder(charset, LineDelimiter.UNIX); 60 decoder = new TextLineDecoder(charset, LineDelimiter.AUTO); 61 } 62 63 /** 64 * Creates a new instance of TextLineCodecFactory. This constructor 65 * provides more flexibility for the developer. 66 * 67 * @param charset 68 * The charset to use in the encoding and decoding 69 * @param encodingDelimiter 70 * The line delimeter for the encoder 71 * @param decodingDelimiter 72 * The line delimeter for the decoder 73 */ 74 public TextLineCodecFactory(Charset charset, 75 String encodingDelimiter, String decodingDelimiter) { 76 encoder = new TextLineEncoder(charset, encodingDelimiter); 77 decoder = new TextLineDecoder(charset, decodingDelimiter); 78 } 79 80 /** 81 * Creates a new instance of TextLineCodecFactory. This constructor 82 * provides more flexibility for the developer. 83 * 84 * @param charset 85 * The charset to use in the encoding and decoding 86 * @param encodingDelimiter 87 * The line delimeter for the encoder 88 * @param decodingDelimiter 89 * The line delimeter for the decoder 90 */ 91 public TextLineCodecFactory(Charset charset, 92 LineDelimiter encodingDelimiter, LineDelimiter decodingDelimiter) { 93 encoder = new TextLineEncoder(charset, encodingDelimiter); 94 decoder = new TextLineDecoder(charset, decodingDelimiter); 95 } 96 97 public ProtocolEncoder getEncoder(IoSession session) { 98 return encoder; 99 } 100 101 public ProtocolDecoder getDecoder(IoSession session) { 102 return decoder; 103 } 104 105 /** 106 * Returns the allowed maximum size of the encoded line. 107 * If the size of the encoded line exceeds this value, the encoder 108 * will throw a {@link IllegalArgumentException}. The default value 109 * is {@link Integer#MAX_VALUE}. 110 * <p> 111 * This method does the same job with {@link TextLineEncoder#getMaxLineLength()}. 112 */ 113 public int getEncoderMaxLineLength() { 114 return encoder.getMaxLineLength(); 115 } 116 117 /** 118 * Sets the allowed maximum size of the encoded line. 119 * If the size of the encoded line exceeds this value, the encoder 120 * will throw a {@link IllegalArgumentException}. The default value 121 * is {@link Integer#MAX_VALUE}. 122 * <p> 123 * This method does the same job with {@link TextLineEncoder#setMaxLineLength(int)}. 124 */ 125 public void setEncoderMaxLineLength(int maxLineLength) { 126 encoder.setMaxLineLength(maxLineLength); 127 } 128 129 /** 130 * Returns the allowed maximum size of the line to be decoded. 131 * If the size of the line to be decoded exceeds this value, the 132 * decoder will throw a {@link BufferDataException}. The default 133 * value is <tt>1024</tt> (1KB). 134 * <p> 135 * This method does the same job with {@link TextLineDecoder#getMaxLineLength()}. 136 */ 137 public int getDecoderMaxLineLength() { 138 return decoder.getMaxLineLength(); 139 } 140 141 /** 142 * Sets the allowed maximum size of the line to be decoded. 143 * If the size of the line to be decoded exceeds this value, the 144 * decoder will throw a {@link BufferDataException}. The default 145 * value is <tt>1024</tt> (1KB). 146 * <p> 147 * This method does the same job with {@link TextLineDecoder#setMaxLineLength(int)}. 148 */ 149 public void setDecoderMaxLineLength(int maxLineLength) { 150 decoder.setMaxLineLength(maxLineLength); 151 } 152 }