@Test public void testEncodeUri() { String expected = "%09%0d%0a%20!%22#$%25&'()*+,-./0123456789:;%3c=%3e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5b%5c%5d%5e_%60abcdefghijklmnopqrstuvwxyz%7b%7c%7d~%e3%81%82"; String actual = EscapeUtils.encodeUri("\t\r\n !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u3042"); System.out.println("* URI\n exp: [" + expected + "]\n act: [" + actual + "]"); assertThat(actual, equalTo(expected)); }
public static String encodeUri(String s) { StringBuilder result = new StringBuilder(); Matcher matcher = URI_RE.matcher(s); int index = 0; while (matcher.find(index)) { int start = matcher.start(); if (index < start) result.append(s, index, start); for (byte b : matcher.group().getBytes(StandardCharsets.UTF_8)) result.append(String.format("%%%02x", b)); index = matcher.end(); } if (index < s.length()) result.append(s, index, s.length()); return result.toString(); }
EscapeUtils { public static String encodeUri(String s) { StringBuilder result = new StringBuilder(); Matcher matcher = URI_RE.matcher(s); int index = 0; while (matcher.find(index)) { int start = matcher.start(); if (index < start) result.append(s, index, start); for (byte b : matcher.group().getBytes(StandardCharsets.UTF_8)) result.append(String.format("%%%02x", b)); index = matcher.end(); } if (index < s.length()) result.append(s, index, s.length()); return result.toString(); } }
EscapeUtils { public static String encodeUri(String s) { StringBuilder result = new StringBuilder(); Matcher matcher = URI_RE.matcher(s); int index = 0; while (matcher.find(index)) { int start = matcher.start(); if (index < start) result.append(s, index, start); for (byte b : matcher.group().getBytes(StandardCharsets.UTF_8)) result.append(String.format("%%%02x", b)); index = matcher.end(); } if (index < s.length()) result.append(s, index, s.length()); return result.toString(); } private  EscapeUtils(); }
EscapeUtils { public static String encodeUri(String s) { StringBuilder result = new StringBuilder(); Matcher matcher = URI_RE.matcher(s); int index = 0; while (matcher.find(index)) { int start = matcher.start(); if (index < start) result.append(s, index, start); for (byte b : matcher.group().getBytes(StandardCharsets.UTF_8)) result.append(String.format("%%%02x", b)); index = matcher.end(); } if (index < s.length()) result.append(s, index, s.length()); return result.toString(); } private  EscapeUtils(); static String escapeHtml(String s, boolean newline); static String encodeUri(String s); static String escapeJSString(String s); }
EscapeUtils { public static String encodeUri(String s) { StringBuilder result = new StringBuilder(); Matcher matcher = URI_RE.matcher(s); int index = 0; while (matcher.find(index)) { int start = matcher.start(); if (index < start) result.append(s, index, start); for (byte b : matcher.group().getBytes(StandardCharsets.UTF_8)) result.append(String.format("%%%02x", b)); index = matcher.end(); } if (index < s.length()) result.append(s, index, s.length()); return result.toString(); } private  EscapeUtils(); static String escapeHtml(String s, boolean newline); static String encodeUri(String s); static String escapeJSString(String s); static final Pattern HTML_RE; static final Map<String, String> HTML_ESC_MAP; }