@Test public void testEncodeUriComponent() { Assert.assertNull(UriUtils.encodeUriComponent(null)); Assert.assertEquals(UriUtils.encodeUriComponent(""), ""); Assert.assertEquals(UriUtils.encodeUriComponent("abcDe'fZz*_1(2)-0.~9"), "abcDe'fZz*_1(2)-0.~9"); Assert.assertEquals(UriUtils.encodeUriComponent("Hello world!"), "Hello%20world!"); Assert.assertEquals(UriUtils.encodeUriComponent("a/b+c30%"), "a%2Fb%2Bc30%25"); Assert.assertEquals(UriUtils.encodeUriComponent("euro \u20AC"), "euro%20%E2%82%AC"); Assert.assertEquals(UriUtils.encodeUriComponent("katakana ka \u30AB"), "katakana%20ka%20%E3%82%AB"); Assert.assertEquals(UriUtils.encodeUriComponent("emoji smile open mouth \uD83D\uDE04"), "emoji%20smile%20open%20mouth%20%F0%9F%98%84"); }
public static String encodeUriComponent(String uriComponent) { if (uriComponent == null) { return null; } boolean containedReserved = false; StringBuilder encoded = new StringBuilder(uriComponent.length()); boolean unwrittenReserved = false; CharArrayWriter reservedCharsBuffer = new CharArrayWriter(); for (int i = 0; i < uriComponent.length(); i++) { int c = uriComponent.charAt(i); if (c < 127 && UNRESERVED[c]) { if (unwrittenReserved) { appendPercentEncoded(reservedCharsBuffer, encoded); unwrittenReserved = false; } encoded.append((char) c); } else { containedReserved = true; unwrittenReserved = true; reservedCharsBuffer.write(c); if (Character.isHighSurrogate((char) c)) { if (i + 1 < uriComponent.length()) { char lowSurrogate = uriComponent.charAt(i + 1); if (Character.isLowSurrogate(lowSurrogate)) { reservedCharsBuffer.append(lowSurrogate); i++; } } } } } if (unwrittenReserved) { appendPercentEncoded(reservedCharsBuffer, encoded); } if (containedReserved) { uriComponent = encoded.toString(); } return uriComponent; }
UriUtils { public static String encodeUriComponent(String uriComponent) { if (uriComponent == null) { return null; } boolean containedReserved = false; StringBuilder encoded = new StringBuilder(uriComponent.length()); boolean unwrittenReserved = false; CharArrayWriter reservedCharsBuffer = new CharArrayWriter(); for (int i = 0; i < uriComponent.length(); i++) { int c = uriComponent.charAt(i); if (c < 127 && UNRESERVED[c]) { if (unwrittenReserved) { appendPercentEncoded(reservedCharsBuffer, encoded); unwrittenReserved = false; } encoded.append((char) c); } else { containedReserved = true; unwrittenReserved = true; reservedCharsBuffer.write(c); if (Character.isHighSurrogate((char) c)) { if (i + 1 < uriComponent.length()) { char lowSurrogate = uriComponent.charAt(i + 1); if (Character.isLowSurrogate(lowSurrogate)) { reservedCharsBuffer.append(lowSurrogate); i++; } } } } } if (unwrittenReserved) { appendPercentEncoded(reservedCharsBuffer, encoded); } if (containedReserved) { uriComponent = encoded.toString(); } return uriComponent; } }
UriUtils { public static String encodeUriComponent(String uriComponent) { if (uriComponent == null) { return null; } boolean containedReserved = false; StringBuilder encoded = new StringBuilder(uriComponent.length()); boolean unwrittenReserved = false; CharArrayWriter reservedCharsBuffer = new CharArrayWriter(); for (int i = 0; i < uriComponent.length(); i++) { int c = uriComponent.charAt(i); if (c < 127 && UNRESERVED[c]) { if (unwrittenReserved) { appendPercentEncoded(reservedCharsBuffer, encoded); unwrittenReserved = false; } encoded.append((char) c); } else { containedReserved = true; unwrittenReserved = true; reservedCharsBuffer.write(c); if (Character.isHighSurrogate((char) c)) { if (i + 1 < uriComponent.length()) { char lowSurrogate = uriComponent.charAt(i + 1); if (Character.isLowSurrogate(lowSurrogate)) { reservedCharsBuffer.append(lowSurrogate); i++; } } } } } if (unwrittenReserved) { appendPercentEncoded(reservedCharsBuffer, encoded); } if (containedReserved) { uriComponent = encoded.toString(); } return uriComponent; } private  UriUtils(); }
UriUtils { public static String encodeUriComponent(String uriComponent) { if (uriComponent == null) { return null; } boolean containedReserved = false; StringBuilder encoded = new StringBuilder(uriComponent.length()); boolean unwrittenReserved = false; CharArrayWriter reservedCharsBuffer = new CharArrayWriter(); for (int i = 0; i < uriComponent.length(); i++) { int c = uriComponent.charAt(i); if (c < 127 && UNRESERVED[c]) { if (unwrittenReserved) { appendPercentEncoded(reservedCharsBuffer, encoded); unwrittenReserved = false; } encoded.append((char) c); } else { containedReserved = true; unwrittenReserved = true; reservedCharsBuffer.write(c); if (Character.isHighSurrogate((char) c)) { if (i + 1 < uriComponent.length()) { char lowSurrogate = uriComponent.charAt(i + 1); if (Character.isLowSurrogate(lowSurrogate)) { reservedCharsBuffer.append(lowSurrogate); i++; } } } } } if (unwrittenReserved) { appendPercentEncoded(reservedCharsBuffer, encoded); } if (containedReserved) { uriComponent = encoded.toString(); } return uriComponent; } private  UriUtils(); static String encodeUriComponent(String uriComponent); }
UriUtils { public static String encodeUriComponent(String uriComponent) { if (uriComponent == null) { return null; } boolean containedReserved = false; StringBuilder encoded = new StringBuilder(uriComponent.length()); boolean unwrittenReserved = false; CharArrayWriter reservedCharsBuffer = new CharArrayWriter(); for (int i = 0; i < uriComponent.length(); i++) { int c = uriComponent.charAt(i); if (c < 127 && UNRESERVED[c]) { if (unwrittenReserved) { appendPercentEncoded(reservedCharsBuffer, encoded); unwrittenReserved = false; } encoded.append((char) c); } else { containedReserved = true; unwrittenReserved = true; reservedCharsBuffer.write(c); if (Character.isHighSurrogate((char) c)) { if (i + 1 < uriComponent.length()) { char lowSurrogate = uriComponent.charAt(i + 1); if (Character.isLowSurrogate(lowSurrogate)) { reservedCharsBuffer.append(lowSurrogate); i++; } } } } } if (unwrittenReserved) { appendPercentEncoded(reservedCharsBuffer, encoded); } if (containedReserved) { uriComponent = encoded.toString(); } return uriComponent; } private  UriUtils(); static String encodeUriComponent(String uriComponent);  }