1 package org.apache.mina.filter.codec.prefixedstring; 2 3 import org.apache.mina.common.IoSession; 4 import org.apache.mina.common.BufferDataException; 5 import org.apache.mina.filter.codec.ProtocolCodecFactory; 6 import org.apache.mina.filter.codec.ProtocolDecoder; 7 import org.apache.mina.filter.codec.ProtocolEncoder; 8 9 import java.nio.charset.Charset; 10 11 /** 12 * A {@link ProtocolCodecFactory} that performs encoding and decoding 13 * of a Java String object using a fixed-length length prefix. 14 * 15 * @author The Apache MINA Project (dev@mina.apache.org) 16 * @version $Rev: 596187 $, $Date: 2007-11-19 04:41:14 +0100 (ma, 19 nov 2007) $ 17 */ 18 public class PrefixedStringCodecFactory implements ProtocolCodecFactory { 19 20 private final PrefixedStringEncoder encoder; 21 22 private final PrefixedStringDecoder decoder; 23 24 public PrefixedStringCodecFactory(Charset charset) { 25 encoder = new PrefixedStringEncoder(charset); 26 decoder = new PrefixedStringDecoder(charset); 27 } 28 29 public PrefixedStringCodecFactory() { 30 this(Charset.defaultCharset()); 31 } 32 33 /** 34 * Returns the allowed maximum size of an encoded string. 35 * If the size of the encoded String exceeds this value, the encoder 36 * will throw a {@link IllegalArgumentException}. 37 * The default value is {@link PrefixedStringEncoder#DEFAULT_MAX_DATA_LENGTH}. 38 * <p/> 39 * This method does the same job as {@link PrefixedStringEncoder#setMaxDataLength(int)}. 40 * 41 * @return the allowed maximum size of an encoded string. 42 */ 43 public int getEncoderMaxDataLength() { 44 return encoder.getMaxDataLength(); 45 } 46 47 /** 48 * Sets the allowed maximum size of an encoded String. 49 * If the size of the encoded String exceeds this value, the encoder 50 * will throw a {@link IllegalArgumentException}. 51 * The default value is {@link PrefixedStringEncoder#DEFAULT_MAX_DATA_LENGTH}. 52 * <p/> 53 * This method does the same job as {@link PrefixedStringEncoder#getMaxDataLength()}. 54 * 55 * @param maxDataLength allowed maximum size of an encoded String. 56 */ 57 public void setEncoderMaxDataLength(int maxDataLength) { 58 encoder.setMaxDataLength(maxDataLength); 59 } 60 61 /** 62 * Returns the allowed maximum size of a decoded string. 63 * <p> 64 * This method does the same job as {@link PrefixedStringEncoder#setMaxDataLength(int)}. 65 * </p> 66 * 67 * @return the allowed maximum size of an encoded string. 68 * @see #setDecoderMaxDataLength(int) 69 */ 70 public int getDecoderMaxDataLength() { 71 return decoder.getMaxDataLength(); 72 } 73 74 /** 75 * Sets the maximum allowed value specified as data length in the decoded data 76 * <p> 77 * Useful for preventing an OutOfMemory attack by the peer. 78 * The decoder will throw a {@link BufferDataException} when data length 79 * specified in the incoming data is greater than maxDataLength 80 * The default value is {@link PrefixedStringDecoder#DEFAULT_MAX_DATA_LENGTH}. 81 * 82 * This method does the same job as {@link PrefixedStringDecoder#setMaxDataLength(int)}. 83 * </p> 84 * 85 * @param maxDataLength maximum allowed value specified as data length in the incoming data 86 */ 87 public void setDecoderMaxDataLength(int maxDataLength) { 88 decoder.setMaxDataLength(maxDataLength); 89 } 90 91 /** 92 * Sets the length of the prefix used by the decoder 93 * 94 * @param prefixLength the length of the length prefix (1, 2, or 4) 95 */ 96 public void setDecoderPrefixLength(int prefixLength) { 97 decoder.setPrefixLength(prefixLength); 98 } 99 100 /** 101 * Gets the length of the length prefix (1, 2, or 4) used by the decoder 102 * 103 * @return length of the length prefix 104 */ 105 public int getDecoderPrefixLength() { 106 return decoder.getPrefixLength(); 107 } 108 109 /** 110 * Sets the length of the prefix used by the encoder 111 * 112 * @param prefixLength the length of the length prefix (1, 2, or 4) 113 */ 114 public void setEncoderPrefixLength(int prefixLength) { 115 encoder.setPrefixLength(prefixLength); 116 } 117 118 /** 119 * Gets the length of the length prefix (1, 2, or 4) used by the encoder 120 * 121 * @return length of the length prefix 122 */ 123 public int getEncoderPrefixLength() { 124 return encoder.getPrefixLength(); 125 } 126 127 public ProtocolEncoder getEncoder(IoSession session) throws Exception { 128 return encoder; 129 } 130 131 public ProtocolDecoder getDecoder(IoSession session) throws Exception { 132 return decoder; 133 } 134 }