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

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

commit 1d1051ed1b3207766030b4e317d98d656be1cfc3
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Thu Feb 25 19:51:18 2021 +0100

    camel-util - Optimize to get charset from content-type header when its UTF-8
---
 .../src/main/java/org/apache/camel/util/IOHelper.java          |  5 +++++
 .../src/test/java/org/apache/camel/util/IOHelperTest.java      | 10 ++++++++++
 2 files changed, 15 insertions(+)

diff --git a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java 
b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
index 4c13957..a54cd90 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
@@ -469,6 +469,11 @@ public final class IOHelper {
         // try optimized for direct match without using splitting
         int pos = contentType.indexOf("charset=");
         if (pos != -1) {
+            // special optimization for utf-8 which is a common charset
+            if (contentType.regionMatches(true, pos + 8, "utf-8", 0, 5)) {
+                return "UTF-8";
+            }
+
             int end = contentType.indexOf(';', pos);
             String charset;
             if (end > pos) {
diff --git 
a/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java 
b/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java
index 1fbbd71..9db6672 100644
--- a/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java
+++ b/core/camel-util/src/test/java/org/apache/camel/util/IOHelperTest.java
@@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 public class IOHelperTest {
+
     @Test
     public void testLookupEnvironmentVariable() throws Exception {
         assertEquals("8081", 
IOHelper.lookupEnvironmentVariable("FOO_SERVICE_PORT"));
@@ -28,4 +29,13 @@ public class IOHelperTest {
         assertEquals("8081", 
IOHelper.lookupEnvironmentVariable("foo-service-port"));
         assertEquals("8081", 
IOHelper.lookupEnvironmentVariable("foo.service.port"));
     }
+
+    @Test
+    public void testCharset() throws Exception {
+        assertEquals("UTF-8", 
IOHelper.getCharsetNameFromContentType("charset=utf-8"));
+        assertEquals("UTF-8", 
IOHelper.getCharsetNameFromContentType("charset=UTF-8"));
+        assertEquals("UTF-8", 
IOHelper.getCharsetNameFromContentType("text/plain; charset=UTF-8"));
+        assertEquals("UTF-8", 
IOHelper.getCharsetNameFromContentType("application/json; charset=utf-8"));
+        assertEquals("iso-8859-1", 
IOHelper.getCharsetNameFromContentType("application/json; charset=iso-8859-1"));
+    }
 }

Reply via email to