You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2010/04/21 10:49:21 UTC

svn commit: r936203 - in /mina/trunk/core/src/main/java/org/apache/mina/filter/codec/textline: LineDelimiter.java TextLineDecoder.java

Author: elecharny
Date: Wed Apr 21 08:49:20 2010
New Revision: 936203

URL: http://svn.apache.org/viewvc?rev=936203&view=rev
Log:
o Improved the classes Javadoc, adding missing ones
o Cleaned up the code
o Fixed DIRMINA-778 by initializing the delimBuf in the constructor
o Added a configurable parameter for the buffer that is used to decode the line
o Many minor fixes

Modified:
    mina/trunk/core/src/main/java/org/apache/mina/filter/codec/textline/LineDelimiter.java
    mina/trunk/core/src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java

Modified: mina/trunk/core/src/main/java/org/apache/mina/filter/codec/textline/LineDelimiter.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/codec/textline/LineDelimiter.java?rev=936203&r1=936202&r2=936203&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/filter/codec/textline/LineDelimiter.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/filter/codec/textline/LineDelimiter.java Wed Apr 21 08:49:20 2010
@@ -24,16 +24,21 @@ import java.io.PrintWriter;
 
 /**
  * A delimiter which is appended to the end of a text line, such as
- * <tt>CR/LF</tt>.
+ * <tt>CR/LF</tt>. This class defines default delimiters for various
+ * OS :
+ * <ul>
+ * <li><b>Unix/Linux</b> : LineDelimiter.UNIX ("\n")</li>
+ * <li><b>Windows</b> : LineDelimiter.WINDOWS ("\r\n")</li>
+ * <li><b>MAC</b> : LineDelimiter.MAC ("\r")</li>
+ * </ul>
  *
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  */
 public class LineDelimiter {
-    /**
-     * the line delimiter constant of the current O/S.
-     */
+    /** the line delimiter constant of the current O/S. */
     public static final LineDelimiter DEFAULT;
 
+    /** Compute the default delimiter on he current OS */
     static {
         ByteArrayOutputStream bout = new ByteArrayOutputStream();
         PrintWriter out = new PrintWriter(bout);
@@ -75,6 +80,7 @@ public class LineDelimiter {
      */
     public static final LineDelimiter NUL = new LineDelimiter("\0");
 
+    /** Stores the selected Line delimiter */
     private final String value;
 
     /**
@@ -84,6 +90,7 @@ public class LineDelimiter {
         if (value == null) {
             throw new NullPointerException("delimiter");
         }
+        
         this.value = value;
     }
 
@@ -94,33 +101,49 @@ public class LineDelimiter {
         return value;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public int hashCode() {
         return value.hashCode();
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public boolean equals(Object o) {
+        if ( this == o) {
+            return true;
+        }
+        
         if (!(o instanceof LineDelimiter)) {
             return false;
         }
-
+        
         LineDelimiter that = (LineDelimiter) o;
+        
         return this.value.equals(that.value);
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public String toString() {
-        StringBuffer buf = new StringBuffer();
-        buf.append("delimiter:");
         if (value.length() == 0) {
-            buf.append(" auto");
+            return "delimiter: auto";
         } else {
+            StringBuilder buf = new StringBuilder();
+            buf.append("delimiter:");
+
             for (int i = 0; i < value.length(); i++) {
                 buf.append(" 0x");
                 buf.append(Integer.toHexString(value.charAt(i)));
             }
+
+            return buf.toString();
         }
-        return buf.toString();
     }
 }

Modified: mina/trunk/core/src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java?rev=936203&r1=936202&r2=936203&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java Wed Apr 21 08:49:20 2010
@@ -42,12 +42,18 @@ public class TextLineDecoder implements 
 
     private final Charset charset;
 
+    /** The delimiter used to determinate when a line has been fully decoded */
     private final LineDelimiter delimiter;
 
+    /** An IoBuffer containing the delimiter */
     private IoBuffer delimBuf;
 
+    /** The default maximum Line length. Default to 1024. */
     private int maxLineLength = 1024;
 
+    /** The default maximum buffer length. Default to 128 chars. */
+    private int bufferLength = 128;
+
     /**
      * Creates a new instance with the current default {@link Charset}
      * and {@link LineDelimiter#AUTO} delimiter.
@@ -94,14 +100,29 @@ public class TextLineDecoder implements 
      */
     public TextLineDecoder(Charset charset, LineDelimiter delimiter) {
         if (charset == null) {
-            throw new NullPointerException("charset");
+            throw new IllegalArgumentException("charset parameter shuld not be null");
         }
+        
         if (delimiter == null) {
-            throw new NullPointerException("delimiter");
+            throw new IllegalArgumentException("delimiter parameter should not be null");
         }
 
         this.charset = charset;
         this.delimiter = delimiter;
+        
+        // Convert delimiter to ByteBuffer if not done yet.
+        if (delimBuf == null) {
+            IoBuffer tmp = IoBuffer.allocate(2).setAutoExpand(true);
+            
+            try{
+                tmp.putString(delimiter.getValue(), charset.newEncoder());
+            } catch (CharacterCodingException cce) {
+                
+            }
+            
+            tmp.flip();
+            delimBuf = tmp;
+        }
     }
 
     /**
@@ -122,13 +143,41 @@ public class TextLineDecoder implements 
      */
     public void setMaxLineLength(int maxLineLength) {
         if (maxLineLength <= 0) {
-            throw new IllegalArgumentException("maxLineLength: "
-                    + maxLineLength);
+            throw new IllegalArgumentException("maxLineLength ("
+                    + maxLineLength + ") should be a positive value");
         }
 
         this.maxLineLength = maxLineLength;
     }
+    
+    /**
+     * Sets the default buffer size. This buffer is used in the Context
+     * to store the decoded line.
+     *
+     * @param bufferLength The default bufer size
+     */
+    public void setBufferLength(int bufferLength) {
+        if ( bufferLength <= 0) {
+            throw new IllegalArgumentException("bufferLength ("
+                + maxLineLength + ") should be a positive value");
+            
+        }
+        
+        this.bufferLength = bufferLength;
+    }
+    
+    /**
+     * Returns the allowed buffer size used to store the decoded line
+     * in the Context instance.
+     */
+    public int getBufferLength() {
+        return bufferLength;
+    }
+
 
+    /**
+     * {@inheritDoc}
+     */
     public void decode(IoSession session, IoBuffer in,
             ProtocolDecoderOutput out) throws Exception {
         Context ctx = getContext(session);
@@ -140,52 +189,70 @@ public class TextLineDecoder implements 
         }
     }
 
+    /**
+     * Return the context for this session
+     */
     private Context getContext(IoSession session) {
         Context ctx;
         ctx = (Context) session.getAttribute(CONTEXT);
+        
         if (ctx == null) {
-            ctx = new Context();
+            ctx = new Context(bufferLength);
             session.setAttribute(CONTEXT, ctx);
         }
+        
         return ctx;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public void finishDecode(IoSession session, ProtocolDecoderOutput out)
             throws Exception {
         // Do nothing
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public void dispose(IoSession session) throws Exception {
         Context ctx = (Context) session.getAttribute(CONTEXT);
+        
         if (ctx != null) {
             session.removeAttribute(CONTEXT);
         }
     }
 
+    /**
+     * Decode a line using the default delimiter on the current system
+     */
     private void decodeAuto(Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out)
             throws CharacterCodingException, ProtocolDecoderException {
-
         int matchCount = ctx.getMatchCount();
 
         // Try to find a match
         int oldPos = in.position();
         int oldLimit = in.limit();
+
         while (in.hasRemaining()) {
             byte b = in.get();
             boolean matched = false;
+            
             switch (b) {
-            case '\r':
-                // Might be Mac, but we don't auto-detect Mac EOL
-                // to avoid confusion.
-                matchCount++;
-                break;
-            case '\n':
-                // UNIX
-                matchCount++;
-                matched = true;
-                break;
-            default:
-                matchCount = 0;
+                case '\r':
+                    // Might be Mac, but we don't auto-detect Mac EOL
+                    // to avoid confusion.
+                    matchCount++;
+                    break;
+                    
+                case '\n':
+                    // UNIX
+                    matchCount++;
+                    matched = true;
+                    break;
+                    
+                default:
+                    matchCount = 0;
             }
 
             if (matched) {
@@ -203,6 +270,7 @@ public class TextLineDecoder implements 
                     IoBuffer buf = ctx.getBuffer();
                     buf.flip();
                     buf.limit(buf.limit() - matchCount);
+                    
                     try {
                         writeText(session, buf.getString(ctx.getDecoder()), out);
                     } finally {
@@ -227,26 +295,23 @@ public class TextLineDecoder implements 
         ctx.setMatchCount(matchCount);
     }
 
+    /**
+     * Decode a line using the delimiter defined by the caller
+     */
     private void decodeNormal(Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out)
             throws CharacterCodingException, ProtocolDecoderException {
-
         int matchCount = ctx.getMatchCount();
 
-        // Convert delimiter to ByteBuffer if not done yet.
-        if (delimBuf == null) {
-            IoBuffer tmp = IoBuffer.allocate(2).setAutoExpand(true);
-            tmp.putString(delimiter.getValue(), charset.newEncoder());
-            tmp.flip();
-            delimBuf = tmp;
-        }
-
         // Try to find a match
         int oldPos = in.position();
         int oldLimit = in.limit();
+        
         while (in.hasRemaining()) {
             byte b = in.get();
+            
             if (delimBuf.get(matchCount) == b) {
                 matchCount++;
+                
                 if (matchCount == delimBuf.limit()) {
                     // Found a match.
                     int pos = in.position();
@@ -257,10 +322,12 @@ public class TextLineDecoder implements 
 
                     in.limit(oldLimit);
                     in.position(pos);
+                    
                     if (ctx.getOverflowPosition() == 0) {
                         IoBuffer buf = ctx.getBuffer();
                         buf.flip();
                         buf.limit(buf.limit() - matchCount);
+                        
                         try {
                             writeText(session, buf.getString(ctx.getDecoder()), out);
                         } finally {
@@ -303,15 +370,30 @@ public class TextLineDecoder implements 
         out.write(text);
     }
 
+    /**
+     * A Context used during the decoding of a lin. It stores the decoder, 
+     * the temporary buffer containing the decoded line, and other status flags.
+     *
+     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+     * @version $Rev$, $Date$
+     */
     private class Context {
+        /** The decoder */
         private final CharsetDecoder decoder;
+        
+        /** The temporary buffer containing the decoded line */
         private final IoBuffer buf;
+        
+        /** The number of lines found so far */
         private int matchCount = 0;
+        
+        /** A counter to signal that the line is too long */
         private int overflowPosition = 0;
 
-        private Context() {
+        /** Create a new Context object with a default buffer */
+        private Context(int bufferLength) {
             decoder = charset.newDecoder();
-            buf = IoBuffer.allocate(80).setAutoExpand(true);
+            buf = IoBuffer.allocate(bufferLength).setAutoExpand(true);
         }
 
         public CharsetDecoder getDecoder() {
@@ -358,6 +440,7 @@ public class TextLineDecoder implements 
             } else {
                 overflowPosition += in.remaining();
             }
+            
             in.position(in.limit());
         }
     }