@@ -800,32 +800,27 @@ public static boolean pathEquals(String path1, String path2) {
800
800
}
801
801
802
802
/**
803
- * Decode the given encoded URI component value. Based on the following rules:
804
- * <ul>
805
- * <li>Alphanumeric characters {@code "a"} through {@code "z"}, {@code "A"} through {@code "Z"},
806
- * and {@code "0"} through {@code "9"} stay the same.</li>
807
- * <li>Special characters {@code "-"}, {@code "_"}, {@code "."}, and {@code "*"} stay the same.</li>
808
- * <li>A sequence "<i>{@code %xy}</i>" is interpreted as a hexadecimal representation of the character.</li>
809
- * <li>For all other characters (including those already decoded), the output is undefined.</li>
810
- * </ul>
811
- * @param source the encoded String
812
- * @param charset the character set
803
+ * Decode the given encoded URI component value by replacing "<i>{@code %xy}</i>" sequences
804
+ * by an hexadecimal representation of the character in the specified charset, letting other
805
+ * characters unchanged.
806
+ * @param source the encoded {@code String}
807
+ * @param charset the character encoding to use to decode the "<i>{@code %xy}</i>" sequences
813
808
* @return the decoded value
814
809
* @throws IllegalArgumentException when the given source contains invalid encoded sequences
815
810
* @since 5.0
816
- * @see java.net.URLDecoder#decode(String, String)
811
+ * @see java.net.URLDecoder#decode(String, String) java.net.URLDecoder#decode for HTML form decoding
817
812
*/
818
813
public static String uriDecode (String source , Charset charset ) {
819
- Assert .notNull (charset , "Charset must not be null" );
820
814
int length = source .length ();
821
- if (length == 0 ) {
815
+ int firstPercentIndex = source .indexOf ('%' );
816
+ if (length == 0 || firstPercentIndex < 0 ) {
822
817
return source ;
823
818
}
824
819
825
820
StringBuilder output = new StringBuilder (length );
826
- boolean changed = false ;
821
+ output . append ( source , 0 , firstPercentIndex ) ;
827
822
byte [] bytes = null ;
828
- int i = 0 ;
823
+ int i = firstPercentIndex ;
829
824
while (i < length ) {
830
825
char ch = source .charAt (i );
831
826
if (ch == '%' ) {
@@ -848,7 +843,6 @@ public static String uriDecode(String source, Charset charset) {
848
843
}
849
844
850
845
output .append (new String (bytes , 0 , pos , charset ));
851
- changed = true ;
852
846
}
853
847
catch (NumberFormatException ex ) {
854
848
throw new IllegalArgumentException ("Invalid encoded sequence \" " + source .substring (i ) + "\" " );
@@ -859,7 +853,7 @@ public static String uriDecode(String source, Charset charset) {
859
853
i ++;
860
854
}
861
855
}
862
- return ( changed ? output .toString () : source );
856
+ return output .toString ();
863
857
}
864
858
865
859
/**
0 commit comments