Encodings { public static String decodeURIComponent(String value) { int length = value.length(); int index = 0; for (; index < length; index++) { int ch = value.charAt(index); if (ch == '%') break; } if (index == length) return value; byte[] buffer = new byte[length]; for (int i = 0; i < index; i++) buffer[i] = (byte) value.charAt(i); int position = index; for (; index < length; index++) { char ch = value.charAt(index); if (ch == '%') { if ((index + 2) >= length) throw new IllegalArgumentException("invalid uri encoding, value=" + value.substring(index)); char hex1 = value.charAt(index + 1); char hex2 = value.charAt(index + 2); int u = Character.digit(hex1, 16); int l = Character.digit(hex2, 16); if (u == -1 || l == -1) throw new IllegalArgumentException("invalid uri encoding, value=" + value.substring(index)); buffer[position] = (byte) ((u << 4) + l); index += 2; } else { buffer[position] = (byte) ch; } position++; } return new String(buffer, 0, position, StandardCharsets.UTF_8); }  static String uriComponent(String value); static String decodeURIComponent(String value); static String base64(String value); static String base64(byte[] value); static byte[] decodeBase64(String value); static String base64URLSafe(byte[] value); static byte[] decodeBase64URLSafe(String value); static String hex(byte[] bytes);  }
@Test void decodeURIComponent() { assertThat(Encodings.decodeURIComponent("%E2%9C%93")).as("decode utf-8").isEqualTo("✓"); assertThat(Encodings.decodeURIComponent("a%20b")).isEqualTo("a b"); assertThat(Encodings.decodeURIComponent("a+b")).isEqualTo("a+b"); assertThat(Encodings.decodeURIComponent("a=b")).isEqualTo("a=b"); assertThat(Encodings.decodeURIComponent("a%3Fb")).isEqualTo("a?b"); assertThat(Encodings.decodeURIComponent("a%3fb")).isEqualTo("a?b"); assertThat(Encodings.decodeURIComponent("a%2Fb")).isEqualTo("a/b"); assertThat(Encodings.decodeURIComponent("a&b")).isEqualTo("a&b"); assertThat(Encodings.decodeURIComponent("a%25b")).isEqualTo("a%b"); }