From a353bf5088cf6a2885c808a8394727e305db66e6 Mon Sep 17 00:00:00 2001 From: dumbbelloper Date: Tue, 5 Mar 2024 16:25:01 +0900 Subject: [PATCH] Fix non-ASCII character handling in StringUtils.uriDecode StringUtils.uriDecode now correctly handles non-ASCII characters regardless of the presence of "%" encoding. Previously, the method took two different paths depending on whether "%" was found, leading to incorrect handling of non-ASCII characters in the absence of "%" encoding. This fix ensures that all characters, including non-ASCII ones, are properly decoded using the provided Charset, improving the method's reliability and consistency across all inputs. This change addresses issues with decoding multibyte characters and ensures compatibility with a wider range of character encodings, enhancing the utility's overall functionality. #32360 --- .../src/main/java/org/springframework/util/StringUtils.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index 440753b82749..55a1362b86ca 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -819,7 +819,7 @@ public static String uriDecode(String source, Charset charset) { if (u == -1 || l == -1) { throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\""); } - baos.write((char) ((u << 4) + l)); + baos.write((byte) ((u << 4) + l)); i += 2; changed = true; } @@ -828,7 +828,9 @@ public static String uriDecode(String source, Charset charset) { } } else { - baos.write(ch); + String characterAsString = Character.toString((char) ch); + byte[] bytes = characterAsString.getBytes(charset); + baos.write(bytes, 0, bytes.length); } } return (changed ? StreamUtils.copyToString(baos, charset) : source);