Encodings { public static String uriComponent(String value) { byte[] bytes = Strings.bytes(value); int length = bytes.length; for (int i = 0; i < length; i++) { byte b1 = bytes[i]; if (b1 < 0 || !URI_UNESCAPED.get(b1)) { StringBuilder builder = new StringBuilder(length * 2); if (i > 0) builder.append(value, 0, i); for (int j = i; j < length; j++) { byte b2 = bytes[j]; if (b2 >= 0 && URI_UNESCAPED.get(b2)) { builder.append((char) b2); } else { builder.append('%'); builder.append(HEX_CHARS[(b2 >> 4) & 0xF]); builder.append(HEX_CHARS[b2 & 0xF]); } } return builder.toString(); } } return value; }  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 uriComponent() { assertThat(Encodings.uriComponent("✓")).as("encode utf-8").isEqualTo("%E2%9C%93"); assertThat(Encodings.uriComponent("a b")).isEqualTo("a%20b"); assertThat(Encodings.uriComponent("a+b")).isEqualTo("a%2Bb"); assertThat(Encodings.uriComponent("a=b")).isEqualTo("a%3Db"); assertThat(Encodings.uriComponent("a?b")).isEqualTo("a%3Fb"); assertThat(Encodings.uriComponent("a/b")).isEqualTo("a%2Fb"); assertThat(Encodings.uriComponent("a&b")).isEqualTo("a%26b"); assertThat(Encodings.uriComponent("a%b")).isEqualTo("a%25b"); }