You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/05/24 15:09:10 UTC

[camel] 05/10: (chores) camel-util: use StandardCharsets in the URIScanner to avoid the costlier conversion of String to Charset

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

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

commit 7ccb2810f00c193d6114947643a0e8c4d806283d
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Tue May 23 17:11:17 2023 +0200

    (chores) camel-util: use StandardCharsets in the URIScanner to avoid the costlier conversion of String to Charset
---
 .../java/org/apache/camel/util/URIScanner.java     | 126 ++++++++++-----------
 1 file changed, 58 insertions(+), 68 deletions(-)

diff --git a/core/camel-util/src/main/java/org/apache/camel/util/URIScanner.java b/core/camel-util/src/main/java/org/apache/camel/util/URIScanner.java
index 79862603bdc..b9cdd125ddf 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/URIScanner.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/URIScanner.java
@@ -16,9 +16,9 @@
  */
 package org.apache.camel.util;
 
-import java.io.UnsupportedEncodingException;
-import java.net.URISyntaxException;
 import java.net.URLDecoder;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.LinkedHashMap;
@@ -33,10 +33,7 @@ import static org.apache.camel.util.URISupport.RAW_TOKEN_START;
  * RAW syntax aware URI scanner that provides various URI manipulations.
  */
 class URIScanner {
-
-    // TODO: when upgrading to JDK11 as minimum then use java.nio.Charset
-    private static final String CHARSET = "UTF-8";
-
+    private static final Charset CHARSET = StandardCharsets.UTF_8;
     private static final char END = '\u0000';
 
     private final StringBuilder key;
@@ -57,82 +54,75 @@ class URIScanner {
         this.isRaw = false;
     }
 
-    public Map<String, Object> parseQuery(String uri, boolean useRaw) throws URISyntaxException {
+    public Map<String, Object> parseQuery(String uri, boolean useRaw) {
         // need to parse the uri query parameters manually as we cannot rely on splitting by &,
         // as & can be used in a parameter value as well.
 
-        try {
-            // use a linked map so the parameters is in the same order
-            Map<String, Object> answer = new LinkedHashMap<>();
-
-            // parse the uri parameters char by char
-            int len = uri.length();
-            for (int i = 0; i < len; i++) {
-                // current char
-                char ch = uri.charAt(i);
-                // look ahead of the next char
-                char next;
-                if (i <= len - 2) {
-                    next = uri.charAt(i + 1);
-                } else {
-                    next = END;
-                }
-
-                if (keyMode) {
-                    // if there is a = sign then the key ends and we are in value mode
-                    if (ch == '=') {
-                        keyMode = false;
-                        continue;
-                    }
+        // use a linked map so the parameters is in the same order
+        Map<String, Object> answer = new LinkedHashMap<>();
+
+        // parse the uri parameters char by char
+        int len = uri.length();
+        for (int i = 0; i < len; i++) {
+            // current char
+            char ch = uri.charAt(i);
+            // look ahead of the next char
+            char next;
+            if (i <= len - 2) {
+                next = uri.charAt(i + 1);
+            } else {
+                next = END;
+            }
 
-                    if (ch != '&') {
-                        // regular char so add it to the key
-                        key.append(ch);
-                    }
-                } else {
-                    // are we a raw value
-                    isRaw = checkRaw();
-
-                    // if we are in raw mode, then we keep adding until we hit the end marker
-                    if (isRaw) {
-                        value.append(ch);
-
-                        if (isAtEnd(ch, next)) {
-                            // raw value end, so add that as a parameter, and reset flags
-                            addParameter(answer, useRaw || isRaw);
-                            initState();
-                            // skip to next as we are in raw mode and have already added the value
-                            i++;
-                        }
-                        continue;
-                    }
+            if (keyMode) {
+                // if there is a = sign then the key ends and we are in value mode
+                if (ch == '=') {
+                    keyMode = false;
+                    continue;
+                }
 
-                    if (ch != '&') {
-                        // regular char so add it to the value
-                        value.append(ch);
+                if (ch != '&') {
+                    // regular char so add it to the key
+                    key.append(ch);
+                }
+            } else {
+                // are we a raw value
+                isRaw = checkRaw();
+
+                // if we are in raw mode, then we keep adding until we hit the end marker
+                if (isRaw) {
+                    value.append(ch);
+
+                    if (isAtEnd(ch, next)) {
+                        // raw value end, so add that as a parameter, and reset flags
+                        addParameter(answer, useRaw || isRaw);
+                        initState();
+                        // skip to next as we are in raw mode and have already added the value
+                        i++;
                     }
+                    continue;
                 }
 
-                // the & denote parameter is ended
-                if (ch == '&') {
-                    // parameter is ended, as we hit & separator
-                    addParameter(answer, useRaw || isRaw);
-                    initState();
+                if (ch != '&') {
+                    // regular char so add it to the value
+                    value.append(ch);
                 }
             }
 
-            // any left over parameters, then add that
-            if (key.length() > 0) {
+            // the & denote parameter is ended
+            if (ch == '&') {
+                // parameter is ended, as we hit & separator
                 addParameter(answer, useRaw || isRaw);
+                initState();
             }
+        }
 
-            return answer;
-
-        } catch (UnsupportedEncodingException e) {
-            URISyntaxException se = new URISyntaxException(e.toString(), "Invalid encoding");
-            se.initCause(e);
-            throw se;
+        // any left over parameters, then add that
+        if (key.length() > 0) {
+            addParameter(answer, useRaw || isRaw);
         }
+
+        return answer;
     }
 
     private boolean checkRaw() {
@@ -165,7 +155,7 @@ class URIScanner {
         return ch == rawTokenEnd && (next == '&' || next == END);
     }
 
-    private void addParameter(Map<String, Object> answer, boolean isRaw) throws UnsupportedEncodingException {
+    private void addParameter(Map<String, Object> answer, boolean isRaw) {
         String name = URLDecoder.decode(key.toString(), CHARSET);
         String text;
         if (isRaw) {