Skip to content

Commit a353bf5

Browse files
committed
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. spring-projects#32360
1 parent 1cb2dfa commit a353bf5

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Diff for: spring-core/src/main/java/org/springframework/util/StringUtils.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ public static String uriDecode(String source, Charset charset) {
819819
if (u == -1 || l == -1) {
820820
throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\"");
821821
}
822-
baos.write((char) ((u << 4) + l));
822+
baos.write((byte) ((u << 4) + l));
823823
i += 2;
824824
changed = true;
825825
}
@@ -828,7 +828,9 @@ public static String uriDecode(String source, Charset charset) {
828828
}
829829
}
830830
else {
831-
baos.write(ch);
831+
String characterAsString = Character.toString((char) ch);
832+
byte[] bytes = characterAsString.getBytes(charset);
833+
baos.write(bytes, 0, bytes.length);
832834
}
833835
}
834836
return (changed ? StreamUtils.copyToString(baos, charset) : source);

0 commit comments

Comments
 (0)