Modified: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/CharChunk.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/CharChunk.java?rev=1821319&r1=1821318&r2=1821319&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/CharChunk.java 
(original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/buf/CharChunk.java Tue Jan 
16 21:48:41 2018
@@ -17,7 +17,6 @@
 package org.apache.tomcat.util.buf;
 
 import java.io.IOException;
-import java.io.Serializable;
 
 /**
  * Utilities to manipulate char chunks. While String is the easiest way to
@@ -29,17 +28,21 @@ import java.io.Serializable;
  * @author Costin Manolache
  * @author Remy Maucherat
  */
-public final class CharChunk implements Cloneable, Serializable, CharSequence {
+public final class CharChunk extends AbstractChunk implements CharSequence {
 
     private static final long serialVersionUID = 1L;
 
-    // Input interface, used when the buffer is emptied.
+    /**
+     * Input interface, used when the buffer is empty.
+     */
     public static interface CharInputChannel {
 
         /**
-         * Read new bytes ( usually the internal conversion buffer ). The
-         * implementation is allowed to ignore the parameters, and mutate the
-         * chunk if it wishes to implement its own buffering.
+         * Read new characters.
+         *
+         * @return The number of characters read
+         *
+         * @throws IOException If an I/O error occurs during reading
          */
         public int realReadChars(char cbuf[], int off, int len) throws 
IOException;
     }
@@ -53,23 +56,19 @@ public final class CharChunk implements
         /**
          * Send the bytes ( usually the internal conversion buffer ). Expect 8k
          * output if the buffer is full.
+         *
+         * @param buf characters that will be written
+         * @param off offset in the characters array
+         * @param len length that will be written
+         * @throws IOException If an I/O occurs while writing the characters
          */
-        public void realWriteChars(char cbuf[], int off, int len) throws 
IOException;
+        public void realWriteChars(char buf[], int off, int len) throws 
IOException;
     }
 
     // --------------------
 
-    private int hashCode = 0;
-    // did we compute the hashcode ?
-    private boolean hasHashCode = false;
-
     // char[]
-    private char buff[];
-
-    private int start;
-    private int end;
-
-    private boolean isSet = false; // XXX
+    private char[] buff;
 
     // -1: grow indefinitely
     // maximum amount to be cached
@@ -88,8 +87,8 @@ public final class CharChunk implements
     }
 
 
-    public CharChunk(int size) {
-        allocate(size, -1);
+    public CharChunk(int initial) {
+        allocate(initial, -1);
     }
 
 
@@ -108,26 +107,6 @@ public final class CharChunk implements
     }
 
 
-    public boolean isNull() {
-        if (end > 0) {
-            return false;
-        }
-        return !isSet; // XXX
-    }
-
-
-    /**
-     * Resets the message bytes to an uninitialized state.
-     */
-    public void recycle() {
-        // buff=null;
-        isSet = false; // XXX
-        hasHashCode = false;
-        start = 0;
-        end = 0;
-    }
-
-
     /**
      * @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
      */
@@ -156,6 +135,13 @@ public final class CharChunk implements
     }
 
 
+    /**
+     * Sets the buffer to the specified subarray of characters.
+     *
+     * @param c the characters
+     * @param off the start offset of the characters
+     * @param len the length of the characters
+     */
     public void setChars(char[] c, int off, int len) {
         buff = c;
         start = off;
@@ -166,6 +152,22 @@ public final class CharChunk implements
 
 
     /**
+     * @return the buffer.
+     */
+    public char[] getChars() {
+        return getBuffer();
+    }
+
+
+    /**
+     * @return the buffer.
+     */
+    public char[] getBuffer() {
+        return buff;
+    }
+
+
+    /**
      * Maximum amount of data in this buffer.
      *
      * If -1 or not set, the buffer will grow indefinitely. Can be smaller than
@@ -201,58 +203,7 @@ public final class CharChunk implements
     }
 
 
-    // compat
-    public char[] getChars() {
-        return getBuffer();
-    }
-
-
-    public char[] getBuffer() {
-        return buff;
-    }
-
-
-    /**
-     * Returns the start offset of the bytes. For output this is the end of the
-     * buffer.
-     */
-    public int getStart() {
-        return start;
-    }
-
-
-    public int getOffset() {
-        return start;
-    }
-
-
-    /**
-     * Returns the start offset of the bytes.
-     */
-    public void setOffset(int off) {
-        start = off;
-    }
-
-
-    /**
-     * Returns the length of the bytes.
-     */
-    public int getLength() {
-        return end - start;
-    }
-
-
-    public int getEnd() {
-        return end;
-    }
-
-
-    public void setEnd(int i) {
-        end = i;
-    }
-
-
-    // -------------------- Adding data --------------------
+    // -------------------- Adding data to the buffer --------------------
 
     public void append(char b) throws IOException {
         makeSpace(1);
@@ -286,8 +237,9 @@ public final class CharChunk implements
         }
 
         // Optimize on a common case.
-        // If the source is going to fill up all the space in buffer, may
-        // as well write it directly to the output, and avoid an extra copy
+        // If the buffer is empty and the source is going to fill up all the
+        // space in buffer, may as well write it directly to the output,
+        // and avoid an extra copy
         if (optimizedWrite && len == limit && end == start && out != null) {
             out.realWriteChars(src, off, len);
             return;
@@ -341,7 +293,7 @@ public final class CharChunk implements
 
 
     /**
-     * Add data to the buffer.
+     * Append a string to the buffer
      *
      * @deprecated Unused. Will be removed in Tomcat 8.0.x onwards.
      */
@@ -419,19 +371,10 @@ public final class CharChunk implements
     // -------------------- Removing data from the buffer --------------------
 
     public int substract() throws IOException {
-
-        if ((end - start) == 0) {
-            if (in == null) {
-                return -1;
-            }
-            int n = in.realReadChars(buff, end, buff.length - end);
-            if (n < 0) {
-                return -1;
-            }
+        if (checkEof()) {
+            return -1;
         }
-
-        return (buff[start++]);
-
+        return buff[start++];
     }
 
 
@@ -460,17 +403,9 @@ public final class CharChunk implements
 
 
     public int substract(char src[], int off, int len) throws IOException {
-
-        if ((end - start) == 0) {
-            if (in == null) {
-                return -1;
-            }
-            int n = in.realReadChars(buff, end, buff.length - end);
-            if (n < 0) {
-                return -1;
-            }
+        if (checkEof()) {
+            return -1;
         }
-
         int n = len;
         if (len > getLength()) {
             n = getLength();
@@ -478,10 +413,29 @@ public final class CharChunk implements
         System.arraycopy(buff, start, src, off, n);
         start += n;
         return n;
+    }
 
+
+    private boolean checkEof() throws IOException {
+        if ((end - start) == 0) {
+            if (in == null) {
+                return true;
+            }
+            int n = in.realReadChars(buff, end, buff.length - end);
+            if (n < 0) {
+                return true;
+            }
+        }
+        return false;
     }
 
 
+    /**
+     * Send the buffer to the sink. Called by append() when the limit is
+     * reached. You can also call it explicitly to force the data to be 
written.
+     *
+     * @throws IOException Writing overflow data to the output channel failed
+     */
     public void flushBuffer() throws IOException {
         // assert out!=null
         if (out == null) {
@@ -534,6 +488,7 @@ public final class CharChunk implements
             tmp = new char[newSize];
         }
 
+        // Some calling code assumes buffer will not be compacted
         System.arraycopy(buff, 0, tmp, 0, end);
         buff = tmp;
         tmp = null;
@@ -633,11 +588,13 @@ public final class CharChunk implements
             return true;
         }
 
-        if (b1 == null || b2 == null || end - start != len2) {
+        int len = end - start;
+        if (len != len2 || b1 == null || b2 == null) {
             return false;
         }
+
         int off1 = start;
-        int len = end - start;
+
         while (len-- > 0) {
             if (b1[off1++] != b2[off2++]) {
                 return false;
@@ -694,9 +651,12 @@ public final class CharChunk implements
 
 
     /**
-     * Returns true if the message bytes starts with the specified string.
+     * Returns true if the buffer starts with the specified string.
      *
      * @param s the string
+     * @param pos The position
+     *
+     * @return <code>true</code> if the start matches
      */
     public boolean startsWithIgnoreCase(String s, int pos) {
         char[] c = buff;
@@ -735,29 +695,34 @@ public final class CharChunk implements
     }
 
 
-    // -------------------- Hash code --------------------
+    public int indexOf(String src, int srcOff, int srcLen, int myOff) {
+        char first = src.charAt(srcOff);
 
-    @Override
-    public int hashCode() {
-        if (hasHashCode) {
-            return hashCode;
-        }
-        int code = 0;
+        // Look for first char
+        int srcEnd = srcOff + srcLen;
 
-        code = hash();
-        hashCode = code;
-        hasHashCode = true;
-        return code;
+        for (int i = myOff + start; i <= (end - srcLen); i++) {
+            if (buff[i] != first) {
+                continue;
+            }
+            // found first char, now look for a match
+            int myPos = i + 1;
+            for (int srcPos = srcOff + 1; srcPos < srcEnd;) {
+                if (buff[myPos++] != src.charAt(srcPos++)) {
+                    break;
+                }
+                if (srcPos == srcEnd) {
+                    return i - start; // found it
+                }
+            }
+        }
+        return -1;
     }
 
 
-    // normal hash.
-    public int hash() {
-        int code = 0;
-        for (int i = start; i < start + end - start; i++) {
-            code = code * 37 + buff[i];
-        }
-        return code;
+    @Override
+    protected int getBufferElement(int index) {
+        return buff[index];
     }
 
 
@@ -780,9 +745,14 @@ public final class CharChunk implements
 
 
     /**
-     * Returns true if the message bytes starts with the specified string.
+     * Returns the first instance of the given character in this CharChunk
+     * starting at the specified char. If the character is not found, -1 is
+     * returned. <br>
      *
-     * @param c the character
+     * @param c The character
+     * @param starting The start position
+     * @return The position of the first instance of the character or -1 if the
+     *         character is not found.
      */
     public int indexOf(char c, int starting) {
         int ret = indexOf(buff, start + starting, end, c);
@@ -790,38 +760,26 @@ public final class CharChunk implements
     }
 
 
-    public static int indexOf(char chars[], int off, int cend, char qq) {
-        while (off < cend) {
-            char b = chars[off];
-            if (b == qq) {
-                return off;
-            }
-            off++;
-        }
-        return -1;
-    }
-
-
-    public int indexOf(String src, int srcOff, int srcLen, int myOff) {
-        char first = src.charAt(srcOff);
-
-        // Look for first char
-        int srcEnd = srcOff + srcLen;
-
-        for (int i = myOff + start; i <= (end - srcLen); i++) {
-            if (buff[i] != first) {
-                continue;
-            }
-            // found first char, now look for a match
-            int myPos = i + 1;
-            for (int srcPos = srcOff + 1; srcPos < srcEnd;) {
-                if (buff[myPos++] != src.charAt(srcPos++)) {
-                    break;
-                }
-                if (srcPos == srcEnd) {
-                    return i - start; // found it
-                }
+    /**
+     * Returns the first instance of the given character in the given char 
array
+     * between the specified start and end. <br>
+     *
+     * @param chars The array to search
+     * @param start The point to start searching from in the array
+     * @param end The point to stop searching in the array
+     * @param s The character to search for
+     * @return The position of the first instance of the character or -1 if the
+     *         character is not found.
+     */
+    public static int indexOf(char chars[], int start, int end, char s) {
+        int offset = start;
+
+        while (offset < end) {
+            char c = chars[offset];
+            if (c == s) {
+                return offset;
             }
+            offset++;
         }
         return -1;
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to