You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2023/06/14 11:05:55 UTC

[tomcat] 01/04: Code clean-up - formatting. No functional change.

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit a6534bca9ff82044da749ec01b64ef167dd0c6a9
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Jun 14 11:56:07 2023 +0100

    Code clean-up - formatting. No functional change.
---
 .../tomcat/util/http/FastHttpDateFormat.java       |  12 +-
 java/org/apache/tomcat/util/http/MimeHeaders.java  | 181 +++++++++++----------
 java/org/apache/tomcat/util/http/Parameters.java   |   8 +-
 .../tomcat/util/http/Rfc6265CookieProcessor.java   |   6 +-
 4 files changed, 105 insertions(+), 102 deletions(-)

diff --git a/java/org/apache/tomcat/util/http/FastHttpDateFormat.java b/java/org/apache/tomcat/util/http/FastHttpDateFormat.java
index f33aa851a3..f48913bad8 100644
--- a/java/org/apache/tomcat/util/http/FastHttpDateFormat.java
+++ b/java/org/apache/tomcat/util/http/FastHttpDateFormat.java
@@ -34,8 +34,8 @@ public final class FastHttpDateFormat {
     // -------------------------------------------------------------- Variables
 
 
-    private static final int CACHE_SIZE = Integer
-            .getInteger("org.apache.tomcat.util.http.FastHttpDateFormat.CACHE_SIZE", 1000).intValue();
+    private static final int CACHE_SIZE =
+            Integer.getInteger("org.apache.tomcat.util.http.FastHttpDateFormat.CACHE_SIZE", 1000).intValue();
 
 
     // HTTP date formats
@@ -57,8 +57,8 @@ public final class FastHttpDateFormat {
         FORMAT_OBSOLETE_RFC850 = new ConcurrentDateFormat(DATE_OBSOLETE_RFC850, Locale.US, tz);
         FORMAT_OBSOLETE_ASCTIME = new ConcurrentDateFormat(DATE_OBSOLETE_ASCTIME, Locale.US, tz);
 
-        httpParseFormats = new ConcurrentDateFormat[] { FORMAT_RFC5322, FORMAT_OBSOLETE_RFC850,
-                FORMAT_OBSOLETE_ASCTIME };
+        httpParseFormats =
+                new ConcurrentDateFormat[] { FORMAT_RFC5322, FORMAT_OBSOLETE_RFC850, FORMAT_OBSOLETE_ASCTIME };
     }
 
     /**
@@ -76,13 +76,13 @@ public final class FastHttpDateFormat {
     /**
      * Formatter cache.
      */
-    private static final Map<Long, String> formatCache = new ConcurrentHashMap<>(CACHE_SIZE);
+    private static final Map<Long,String> formatCache = new ConcurrentHashMap<>(CACHE_SIZE);
 
 
     /**
      * Parser cache.
      */
-    private static final Map<String, Long> parseCache = new ConcurrentHashMap<>(CACHE_SIZE);
+    private static final Map<String,Long> parseCache = new ConcurrentHashMap<>(CACHE_SIZE);
 
 
     // --------------------------------------------------------- Public Methods
diff --git a/java/org/apache/tomcat/util/http/MimeHeaders.java b/java/org/apache/tomcat/util/http/MimeHeaders.java
index 3f1992dc3c..eb9a06534a 100644
--- a/java/org/apache/tomcat/util/http/MimeHeaders.java
+++ b/java/org/apache/tomcat/util/http/MimeHeaders.java
@@ -65,26 +65,22 @@ import org.apache.tomcat.util.res.StringManager;
 
 
 /**
- *  Memory-efficient repository for Mime Headers. When the object is recycled, it
- *  will keep the allocated headers[] and all the MimeHeaderField - no GC is generated.
- *  <p>
- *  For input headers it is possible to use the MessageByte for Fields - so no GC
- *  will be generated.
+ * Memory-efficient repository for Mime Headers. When the object is recycled, it will keep the allocated headers[] and
+ * all the MimeHeaderField - no GC is generated.
  * <p>
- *  The only garbage is generated when using the String for header names/values -
- *  this can't be avoided when the servlet calls header methods, but is easy
- *  to avoid inside tomcat. The goal is to use _only_ MessageByte-based Fields,
- *  and reduce to 0 the memory overhead of tomcat.
- *  <p>
- *
- *  TODO:
- *  <ul>
- *  <li>one-buffer parsing - for http (other protocols don't need that)</li>
- *  <li>remove unused methods</li>
- *  <li>External enumerations, with 0 GC.</li>
- *  <li>use HeaderName ID</li>
- *  </ul>
- *
+ * For input headers it is possible to use the MessageByte for Fields - so no GC will be generated.
+ * <p>
+ * The only garbage is generated when using the String for header names/values - this can't be avoided when the servlet
+ * calls header methods, but is easy to avoid inside tomcat. The goal is to use _only_ MessageByte-based Fields, and
+ * reduce to 0 the memory overhead of tomcat.
+ * <p>
+ * TODO:
+ * <ul>
+ * <li>one-buffer parsing - for http (other protocols don't need that)</li>
+ * <li>remove unused methods</li>
+ * <li>External enumerations, with 0 GC.</li>
+ * <li>use HeaderName ID</li>
+ * </ul>
  *
  * @author dac@eng.sun.com
  * @author James Todd [gonzo@eng.sun.com]
@@ -97,16 +93,14 @@ public class MimeHeaders {
      * <p>
      * TODO make it configurable (fine-tuning of web-apps)
      */
-    public static final int DEFAULT_HEADER_SIZE=8;
+    public static final int DEFAULT_HEADER_SIZE = 8;
 
-    private static final StringManager sm =
-            StringManager.getManager("org.apache.tomcat.util.http");
+    private static final StringManager sm = StringManager.getManager("org.apache.tomcat.util.http");
 
     /**
      * The header fields.
      */
-    private MimeHeaderField[] headers = new
-        MimeHeaderField[DEFAULT_HEADER_SIZE];
+    private MimeHeaderField[] headers = new MimeHeaderField[DEFAULT_HEADER_SIZE];
 
     /**
      * The current number of header fields.
@@ -127,6 +121,7 @@ public class MimeHeaders {
 
     /**
      * Set limit on the number of header fields.
+     *
      * @param limit The new limit
      */
     public void setLimit(int limit) {
@@ -158,7 +153,7 @@ public class MimeHeaders {
     }
 
     /**
-     * EXPENSIVE!!!  only for debugging.
+     * EXPENSIVE!!! only for debugging.
      */
     @Override
     public String toString() {
@@ -199,8 +194,9 @@ public class MimeHeaders {
 
     /**
      * @param n The header index
-     * @return the Nth header name, or null if there is no such header.
-     * This may be used to iterate through all header fields.
+     *
+     * @return the Nth header name, or null if there is no such header. This may be used to iterate through all header
+     *             fields.
      */
     public MessageBytes getName(int n) {
         return n >= 0 && n < count ? headers[n].getName() : null;
@@ -208,8 +204,9 @@ public class MimeHeaders {
 
     /**
      * @param n The header index
-     * @return the Nth header value, or null if there is no such header.
-     * This may be used to iterate through all header fields.
+     *
+     * @return the Nth header value, or null if there is no such header. This may be used to iterate through all header
+     *             fields.
      */
     public MessageBytes getValue(int n) {
         return n >= 0 && n < count ? headers[n].getValue() : null;
@@ -217,13 +214,15 @@ public class MimeHeaders {
 
     /**
      * Find the index of a header with the given name.
-     * @param name The header name
+     *
+     * @param name     The header name
      * @param starting Index on which to start looking
+     *
      * @return the header index
      */
-    public int findHeader( String name, int starting ) {
+    public int findHeader(String name, int starting) {
         // We can use a hash - but it's not clear how much
-        // benefit you can get - there is an  overhead
+        // benefit you can get - there is an overhead
         // and the number of headers is small (4-5 ?)
         // Another problem is that we'll pay the overhead
         // of constructing the hashtable
@@ -240,9 +239,9 @@ public class MimeHeaders {
     // -------------------- --------------------
 
     /**
-     * Returns an enumeration of strings representing the header field names.
-     * Field names may appear multiple times in this enumeration, indicating
-     * that multiple fields with that name exist in this header.
+     * Returns an enumeration of strings representing the header field names. Field names may appear multiple times in
+     * this enumeration, indicating that multiple fields with that name exist in this header.
+     *
      * @return the enumeration
      */
     public Enumeration<String> names() {
@@ -257,13 +256,11 @@ public class MimeHeaders {
 
 
     /**
-     * Adds a partially constructed field to the header.  This
-     * field has not had its name or value initialized.
+     * Adds a partially constructed field to the header. This field has not had its name or value initialized.
      */
     private MimeHeaderField createHeader() {
         if (limit > -1 && count >= limit) {
-            throw new IllegalStateException(sm.getString(
-                    "headers.maxCountFail", Integer.valueOf(limit)));
+            throw new IllegalStateException(sm.getString("headers.maxCountFail", Integer.valueOf(limit)));
         }
         MimeHeaderField mh;
         int len = headers.length;
@@ -285,43 +282,46 @@ public class MimeHeaders {
     }
 
     /**
-     * Create a new named header , return the MessageBytes
-     * container for the new value
+     * Create a new named header , return the MessageBytes container for the new value
+     *
      * @param name The header name
+     *
      * @return the message bytes container for the value
      */
-    public MessageBytes addValue( String name ) {
+    public MessageBytes addValue(String name) {
         MimeHeaderField mh = createHeader();
         mh.getName().setString(name);
         return mh.getValue();
     }
 
     /**
-     * Create a new named header using un-translated byte[].
-     * The conversion to chars can be delayed until
-     * encoding is known.
-     * @param b The header name bytes
+     * Create a new named header using un-translated byte[]. The conversion to chars can be delayed until encoding is
+     * known.
+     *
+     * @param b      The header name bytes
      * @param startN Offset
-     * @param len Length
+     * @param len    Length
+     *
      * @return the message bytes container for the value
      */
     public MessageBytes addValue(byte b[], int startN, int len) {
-        MimeHeaderField mhf=createHeader();
+        MimeHeaderField mhf = createHeader();
         mhf.getName().setBytes(b, startN, len);
         return mhf.getValue();
     }
 
     /**
-     * Allow "set" operations, which removes all current values
-     * for this header.
+     * Allow "set" operations, which removes all current values for this header.
+     *
      * @param name The header name
+     *
      * @return the message bytes container for the value
      */
-    public MessageBytes setValue( String name ) {
-        for ( int i = 0; i < count; i++ ) {
-            if(headers[i].getName().equalsIgnoreCase(name)) {
-                for ( int j=i+1; j < count; j++ ) {
-                    if(headers[j].getName().equalsIgnoreCase(name)) {
+    public MessageBytes setValue(String name) {
+        for (int i = 0; i < count; i++) {
+            if (headers[i].getName().equalsIgnoreCase(name)) {
+                for (int j = i + 1; j < count; j++) {
+                    if (headers[j].getName().equalsIgnoreCase(name)) {
                         removeHeader(j--);
                     }
                 }
@@ -333,12 +333,13 @@ public class MimeHeaders {
         return mh.getValue();
     }
 
-    //-------------------- Getting headers --------------------
+    // -------------------- Getting headers --------------------
     /**
-     * Finds and returns a header field with the given name.  If no such
-     * field exists, null is returned.  If more than one such field is
-     * in the header, an arbitrary one is returned.
+     * Finds and returns a header field with the given name. If no such field exists, null is returned. If more than one
+     * such field is in the header, an arbitrary one is returned.
+     *
      * @param name The header name
+     *
      * @return the value
      */
     public MessageBytes getValue(String name) {
@@ -351,11 +352,13 @@ public class MimeHeaders {
     }
 
     /**
-     * Finds and returns a unique header field with the given name. If no such
-     * field exists, null is returned. If the specified header field is not
-     * unique then an {@link IllegalArgumentException} is thrown.
+     * Finds and returns a unique header field with the given name. If no such field exists, null is returned. If the
+     * specified header field is not unique then an {@link IllegalArgumentException} is thrown.
+     *
      * @param name The header name
+     *
      * @return the value if unique
+     *
      * @throws IllegalArgumentException if the header has multiple values
      */
     public MessageBytes getUniqueValue(String name) {
@@ -381,8 +384,8 @@ public class MimeHeaders {
 
     // -------------------- Removing --------------------
     /**
-     * Removes a header field with the specified name.  Does nothing
-     * if such a field could not be found.
+     * Removes a header field with the specified name. Does nothing if such a field could not be found.
+     *
      * @param name the name of the header field to be removed
      */
     public void removeHeader(String name) {
@@ -398,6 +401,7 @@ public class MimeHeaders {
 
     /**
      * Reset, move to the end and then reduce count by 1.
+     *
      * @param idx the index of the header to remove.
      */
     public void removeHeader(int idx) {
@@ -412,7 +416,7 @@ public class MimeHeaders {
         mh.recycle();
 
         // Move the remaining headers
-        System.arraycopy(headers, idx + 1, headers, idx, count - idx -1);
+        System.arraycopy(headers, idx + 1, headers, idx, count - idx - 1);
 
         // Place the removed header at the end
         headers[count - 1] = mh;
@@ -424,9 +428,8 @@ public class MimeHeaders {
 }
 
 /**
- * Enumerate the distinct header names. Each nextElement() is O(n) ( a
- * comparison is done with all previous elements ). This is less frequent than
- * add() - we want to keep add O(1).
+ * Enumerate the distinct header names. Each nextElement() is O(n) ( a comparison is done with all previous elements ).
+ * This is less frequent than add() - we want to keep add O(1).
  */
 class NamesEnumerator implements Enumeration<String> {
     private int pos;
@@ -435,24 +438,24 @@ class NamesEnumerator implements Enumeration<String> {
     private final MimeHeaders headers;
 
     NamesEnumerator(MimeHeaders headers) {
-        this.headers=headers;
-        pos=0;
+        this.headers = headers;
+        pos = 0;
         size = headers.size();
         findNext();
     }
 
     private void findNext() {
-        next=null;
-        for(; pos< size; pos++ ) {
-            next=headers.getName( pos ).toString();
-            for( int j=0; j<pos ; j++ ) {
-                if( headers.getName( j ).equalsIgnoreCase( next )) {
+        next = null;
+        for (; pos < size; pos++) {
+            next = headers.getName(pos).toString();
+            for (int j = 0; j < pos; j++) {
+                if (headers.getName(j).equalsIgnoreCase(next)) {
                     // duplicate.
-                    next=null;
+                    next = null;
                     break;
                 }
             }
-            if( next!=null ) {
+            if (next != null) {
                 // it's not a duplicate
                 break;
             }
@@ -464,12 +467,12 @@ class NamesEnumerator implements Enumeration<String> {
 
     @Override
     public boolean hasMoreElements() {
-        return next!=null;
+        return next != null;
     }
 
     @Override
     public String nextElement() {
-        String current=next;
+        String current = next;
         findNext();
         return current;
     }
@@ -486,19 +489,19 @@ class ValuesEnumerator implements Enumeration<String> {
     private final String name;
 
     ValuesEnumerator(MimeHeaders headers, String name) {
-        this.name=name;
-        this.headers=headers;
-        pos=0;
+        this.name = name;
+        this.headers = headers;
+        pos = 0;
         size = headers.size();
         findNext();
     }
 
     private void findNext() {
-        next=null;
-        for(; pos< size; pos++ ) {
-            MessageBytes n1=headers.getName( pos );
-            if( n1.equalsIgnoreCase( name )) {
-                next=headers.getValue( pos );
+        next = null;
+        for (; pos < size; pos++) {
+            MessageBytes n1 = headers.getName(pos);
+            if (n1.equalsIgnoreCase(name)) {
+                next = headers.getValue(pos);
                 break;
             }
         }
@@ -507,12 +510,12 @@ class ValuesEnumerator implements Enumeration<String> {
 
     @Override
     public boolean hasMoreElements() {
-        return next!=null;
+        return next != null;
     }
 
     @Override
     public String nextElement() {
-        MessageBytes current=next;
+        MessageBytes current = next;
         findNext();
         return current.toString();
     }
diff --git a/java/org/apache/tomcat/util/http/Parameters.java b/java/org/apache/tomcat/util/http/Parameters.java
index debf968acf..e56793b3de 100644
--- a/java/org/apache/tomcat/util/http/Parameters.java
+++ b/java/org/apache/tomcat/util/http/Parameters.java
@@ -44,7 +44,7 @@ public final class Parameters {
 
     private static final StringManager sm = StringManager.getManager("org.apache.tomcat.util.http");
 
-    private final Map<String, ArrayList<String>> paramHashValues = new LinkedHashMap<>();
+    private final Map<String,ArrayList<String>> paramHashValues = new LinkedHashMap<>();
     private boolean didQueryParameters = false;
 
     private MessageBytes queryMB;
@@ -418,8 +418,8 @@ public final class Parameters {
                     } else if (log.isInfoEnabled()) {
                         UserDataHelper.Mode logMode = userDataLog.getNextMode();
                         if (logMode != null) {
-                            String message = sm.getString("parameters.decodeFail.info", tmpName.toString(),
-                                    tmpValue.toString());
+                            String message =
+                                    sm.getString("parameters.decodeFail.info", tmpName.toString(), tmpValue.toString());
                             switch (logMode) {
                                 case INFO_THEN_DEBUG:
                                     message += sm.getString("parameters.fallToDebug");
@@ -487,7 +487,7 @@ public final class Parameters {
     @Override
     public String toString() {
         StringBuilder sb = new StringBuilder();
-        for (Map.Entry<String, ArrayList<String>> e : paramHashValues.entrySet()) {
+        for (Map.Entry<String,ArrayList<String>> e : paramHashValues.entrySet()) {
             sb.append(e.getKey()).append('=');
             StringUtils.join(e.getValue(), ',', sb);
             sb.append('\n');
diff --git a/java/org/apache/tomcat/util/http/Rfc6265CookieProcessor.java b/java/org/apache/tomcat/util/http/Rfc6265CookieProcessor.java
index 03281b82e5..9e3c5227db 100644
--- a/java/org/apache/tomcat/util/http/Rfc6265CookieProcessor.java
+++ b/java/org/apache/tomcat/util/http/Rfc6265CookieProcessor.java
@@ -38,8 +38,8 @@ public class Rfc6265CookieProcessor extends CookieProcessorBase {
 
     private static final Log log = LogFactory.getLog(Rfc6265CookieProcessor.class);
 
-    private static final StringManager sm = StringManager
-            .getManager(Rfc6265CookieProcessor.class.getPackage().getName());
+    private static final StringManager sm =
+            StringManager.getManager(Rfc6265CookieProcessor.class.getPackage().getName());
 
     private static final BitSet domainValid = new BitSet(128);
 
@@ -178,7 +178,7 @@ public class Rfc6265CookieProcessor extends CookieProcessorBase {
         }
 
         // Add the remaining attributes
-        for (Map.Entry<String, String> entry : cookie.getAttributes().entrySet()) {
+        for (Map.Entry<String,String> entry : cookie.getAttributes().entrySet()) {
             switch (entry.getKey()) {
                 case Constants.COOKIE_COMMENT_ATTR:
                 case Constants.COOKIE_DOMAIN_ATTR:


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