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
The following commit(s) were added to refs/heads/master by this push: new 50c9b25 camel-core - Optimize IOHelper to extract charset from content-type header. 50c9b25 is described below commit 50c9b25cd258366ce0dea46aa9bfea1aaa320f08 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Feb 16 07:55:42 2021 +0100 camel-core - Optimize IOHelper to extract charset from content-type header. --- .../main/java/org/apache/camel/util/IOHelper.java | 32 +++++++++++++++------- 1 file changed, 22 insertions(+), 10 deletions(-) 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 85023ef..4c13957 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 @@ -462,27 +462,36 @@ public final class IOHelper { /** * Get the charset name from the content type string * - * @param contentType + * @param contentType the content type * @return the charset name, or <tt>UTF-8</tt> if no found */ public static String getCharsetNameFromContentType(String contentType) { - String[] values = contentType.split(";"); - String charset = ""; + // try optimized for direct match without using splitting + int pos = contentType.indexOf("charset="); + if (pos != -1) { + int end = contentType.indexOf(';', pos); + String charset; + if (end > pos) { + charset = contentType.substring(pos + 8, end); + } else { + charset = contentType.substring(pos + 8); + } + return normalizeCharset(charset); + } + String[] values = contentType.split(";"); for (String value : values) { value = value.trim(); // Perform a case insensitive "startsWith" check that works for different locales String prefix = "charset="; if (value.regionMatches(true, 0, prefix, 0, prefix.length())) { // Take the charset name - charset = value.substring(8); + String charset = value.substring(8); + return normalizeCharset(charset); } } - if ("".equals(charset)) { - charset = "UTF-8"; - } - return normalizeCharset(charset); - + // use UTF-8 as default + return "UTF-8"; } /** @@ -490,14 +499,17 @@ public final class IOHelper { */ public static String normalizeCharset(String charset) { if (charset != null) { + boolean trim = false; String answer = charset.trim(); if (answer.startsWith("'") || answer.startsWith("\"")) { answer = answer.substring(1); + trim = true; } if (answer.endsWith("'") || answer.endsWith("\"")) { answer = answer.substring(0, answer.length() - 1); + trim = true; } - return answer.trim(); + return trim ? answer.trim() : answer; } else { return null; }