001: package org.apache.mina.filter.codec.prefixedstring;
002:
003: import org.apache.mina.common.IoBuffer;
004: import org.apache.mina.common.IoSession;
005: import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
006: import org.apache.mina.filter.codec.ProtocolEncoderOutput;
007: import org.apache.mina.filter.codec.ProtocolEncoder;
008:
009: import java.nio.charset.Charset;
010:
011: /**
012: * A {@link ProtocolEncoder} which encodes a string
013: * using a fixed-length length prefix.
014: *
015: * @author The Apache MINA Project (dev@mina.apache.org)
016: * @version $Rev: 591182 $, $Date: 2007-11-02 01:59:27 +0100 (vr, 02 nov 2007) $,
017: */
018: public class PrefixedStringEncoder extends ProtocolEncoderAdapter {
019:
020: public final static int DEFAULT_PREFIX_LENGTH = 4;
021:
022: public final static int DEFAULT_MAX_DATA_LENGTH = 2048;
023:
024: private final Charset charset;
025:
026: private int prefixLength = DEFAULT_PREFIX_LENGTH;
027:
028: private int maxDataLength = DEFAULT_MAX_DATA_LENGTH;
029:
030: public PrefixedStringEncoder(Charset charset, int prefixLength,
031: int maxDataLength) {
032: this .charset = charset;
033: this .prefixLength = prefixLength;
034: this .maxDataLength = maxDataLength;
035: }
036:
037: public PrefixedStringEncoder(Charset charset, int prefixLength) {
038: this (charset, prefixLength, DEFAULT_MAX_DATA_LENGTH);
039: }
040:
041: public PrefixedStringEncoder(Charset charset) {
042: this (charset, DEFAULT_PREFIX_LENGTH);
043: }
044:
045: public PrefixedStringEncoder() {
046: this (Charset.defaultCharset());
047: }
048:
049: /**
050: * Sets the number of bytes used by the length prefix
051: *
052: * @param prefixLength the length of the length prefix (1, 2, or 4)
053: */
054: public void setPrefixLength(int prefixLength) {
055: if (prefixLength != 1 && prefixLength != 2 && prefixLength != 4) {
056: throw new IllegalArgumentException("prefixLength: "
057: + prefixLength);
058: }
059: this .prefixLength = prefixLength;
060: }
061:
062: /**
063: * Gets the length of the length prefix (1, 2, or 4)
064: *
065: * @return length of the length prefix
066: */
067: public int getPrefixLength() {
068: return prefixLength;
069: }
070:
071: /**
072: * Sets the maximum number of bytes allowed for encoding a single String
073: * (including the prefix)
074: * <p>
075: * The encoder will throw a {@link IllegalArgumentException} when more bytes
076: * are needed to encode a String value.
077: * The default value is {@link PrefixedStringEncoder#DEFAULT_MAX_DATA_LENGTH}.
078: * </p>
079: *
080: * @param maxDataLength maximum number of bytes allowed for encoding a single String
081: */
082: public void setMaxDataLength(int maxDataLength) {
083: this .maxDataLength = maxDataLength;
084: }
085:
086: /**
087: * Gets the maximum number of bytes allowed for encoding a single String *
088: *
089: * @return maximum number of bytes allowed for encoding a single String (prefix included)
090: */
091: public int getMaxDataLength() {
092: return maxDataLength;
093: }
094:
095: public void encode(IoSession session, Object message,
096: ProtocolEncoderOutput out) throws Exception {
097: String value = (String) message;
098: IoBuffer buffer = IoBuffer.allocate(value.length())
099: .setAutoExpand(true);
100: buffer.putPrefixedString(value, prefixLength, charset
101: .newEncoder());
102: if (buffer.position() > maxDataLength) {
103: throw new IllegalArgumentException("Data length: "
104: + buffer.position());
105: }
106: buffer.flip();
107: out.write(buffer);
108: }
109: }
|