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.common.BufferDataException; 25 import org.apache.mina.filter.codec.ProtocolCodecFactory; 26 import org.apache.mina.filter.codec.ProtocolDecoder; 27 import org.apache.mina.filter.codec.ProtocolEncoder; 28 import org.apache.mina.util.CharsetUtil; 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 Directory Project (mina-dev@directory.apache.org) 36 * @version $Rev: 555855 $, $Date: 2007-07-13 05:19:00 +0200 (Fri, 13 Jul 2007) $ 37 */ 38 public class TextLineCodecFactory implements ProtocolCodecFactory { 39 private final TextLineEncoder encoder; 40 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.forName(CharsetUtil.getDefaultCharsetName())); 48 } 49 50 /** 51 * Creates a new instance with the specified {@link Charset}. 52 */ 53 public TextLineCodecFactory(Charset charset) { 54 encoder = new TextLineEncoder(charset, LineDelimiter.UNIX); 55 decoder = new TextLineDecoder(charset, LineDelimiter.AUTO); 56 } 57 58 public ProtocolEncoder getEncoder() { 59 return encoder; 60 } 61 62 public ProtocolDecoder getDecoder() { 63 return decoder; 64 } 65 66 /** 67 * Returns the allowed maximum size of the encoded line. 68 * If the size of the encoded line exceeds this value, the encoder 69 * will throw a {@link IllegalArgumentException}. The default value 70 * is {@link Integer#MAX_VALUE}. 71 * <p> 72 * This method does the same job with {@link TextLineEncoder#getMaxLineLength()}. 73 */ 74 public int getEncoderMaxLineLength() { 75 return encoder.getMaxLineLength(); 76 } 77 78 /** 79 * Sets the allowed maximum size of the encoded line. 80 * If the size of the encoded line exceeds this value, the encoder 81 * will throw a {@link IllegalArgumentException}. The default value 82 * is {@link Integer#MAX_VALUE}. 83 * <p> 84 * This method does the same job with {@link TextLineEncoder#setMaxLineLength(int)}. 85 */ 86 public void setEncoderMaxLineLength(int maxLineLength) { 87 encoder.setMaxLineLength(maxLineLength); 88 } 89 90 /** 91 * Returns the allowed maximum size of the line to be decoded. 92 * If the size of the line to be decoded exceeds this value, the 93 * decoder will throw a {@link BufferDataException}. The default 94 * value is <tt>1024</tt> (1KB). 95 * <p> 96 * This method does the same job with {@link TextLineDecoder#getMaxLineLength()}. 97 */ 98 public int getDecoderMaxLineLength() { 99 return decoder.getMaxLineLength(); 100 } 101 102 /** 103 * Sets the allowed maximum size of the line to be decoded. 104 * If the size of the line to be decoded exceeds this value, the 105 * decoder will throw a {@link BufferDataException}. The default 106 * value is <tt>1024</tt> (1KB). 107 * <p> 108 * This method does the same job with {@link TextLineDecoder#setMaxLineLength(int)}. 109 */ 110 public void setDecoderMaxLineLength(int maxLineLength) { 111 decoder.setMaxLineLength(maxLineLength); 112 } 113 }