public final void appendPartiallyEncoded(StringBuilder builder, String s) { appendEncoded(builder, s, StandardCharsets.UTF_8, true); }
|| (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || isRetained(c) || (c == '%' && isPartiallyEncoded)) { if (escapeStart != -1) { appendHex(builder, s.substring(escapeStart, i), charset); escapeStart = -1; appendHex(builder, s.substring(escapeStart, s.length()), charset);
private String decode(String s) { return s != null ? UriCodec.decode(s) : null; }
/** * Throws if {@code s} is invalid according to this encoder. */ public final String validate(String uri, int start, int end, String name) throws URISyntaxException { for (int i = start; i < end; ) { char ch = uri.charAt(i); if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || isRetained(ch)) { i++; } else if (ch == '%') { if (i + 2 >= end) { throw new URISyntaxException(uri, "Incomplete % sequence in " + name, i); } int d1 = hexToInt(uri.charAt(i + 1)); int d2 = hexToInt(uri.charAt(i + 2)); if (d1 == -1 || d2 == -1) { throw new URISyntaxException(uri, "Invalid % sequence: " + uri.substring(i, i + 3) + " in " + name, i); } i += 3; } else { throw new URISyntaxException(uri, "Illegal character in " + name, i); } } return uri.substring(start, end); }
/** * Encodes {@code s} using the {@link Charset} named by {@code charsetName}. */ public static String encode(String s, String charsetName) throws UnsupportedEncodingException { return ENCODER.encode(s, Charset.forName(charsetName)); } }
private static void appendHex(StringBuilder builder, String s, Charset charset) { for (byte b : s.getBytes(charset)) { appendHex(builder, b); } }
private boolean isValidDomainName(String host) { try { UriCodec.validateSimple(host, "-."); } catch (URISyntaxException e) { return false; } String lastLabel = null; for (String token : host.split("\\.")) { lastLabel = token; if (lastLabel.startsWith("-") || lastLabel.endsWith("-")) { return false; } } if (lastLabel == null) { return false; } if (!lastLabel.equals(host)) { char ch = lastLabel.charAt(0); if (ch >= '0' && ch <= '9') { return false; } } return true; }
result.append("//"); if (escapeIllegalCharacters) { URI.AUTHORITY_ENCODER.appendPartiallyEncoded(result, authority); } else { result.append(authority); if (fileAndQuery != null) { if (escapeIllegalCharacters) { URI.FILE_AND_QUERY_ENCODER.appendPartiallyEncoded(result, fileAndQuery); } else { result.append(fileAndQuery); result.append('#'); if (escapeIllegalCharacters) { URI.ALL_LEGAL_ENCODER.appendPartiallyEncoded(result, ref); } else { result.append(ref);
fragment = ALL_LEGAL_ENCODER.validate(uri, fragmentStart + 1, uri.length(), "fragment"); schemeSpecificPart = ALL_LEGAL_ENCODER.validate( uri, start, fragmentStart, "scheme specific part"); return; authority = AUTHORITY_ENCODER.validate(uri, authorityStart, fileStart, "authority"); path = PATH_ENCODER.validate(uri, fileStart, queryStart, "path"); query = ALL_LEGAL_ENCODER.validate(uri, queryStart + 1, fragmentStart, "query");
/** * Throws if {@code s} is invalid according to this encoder. */ public final String validate(String uri, int start, int end, String name) throws URISyntaxException { for (int i = start; i < end; ) { char ch = uri.charAt(i); if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || isRetained(ch)) { i++; } else if (ch == '%') { if (i + 2 >= end) { throw new URISyntaxException(uri, "Incomplete % sequence in " + name, i); } int d1 = hexToInt(uri.charAt(i + 1)); int d2 = hexToInt(uri.charAt(i + 2)); if (d1 == -1 || d2 == -1) { throw new URISyntaxException(uri, "Invalid % sequence: " + uri.substring(i, i + 3) + " in " + name, i); } i += 3; } else { throw new URISyntaxException(uri, "Illegal character in " + name, i); } } return uri.substring(start, end); }
/** * Equivalent to {@code encode(s, "UTF-8")}. * * @deprecated Use {@link #encode(String, String)} instead. */ @Deprecated public static String encode(String s) { return ENCODER.encode(s, StandardCharsets.UTF_8); }
private static void appendHex(StringBuilder builder, String s, Charset charset) { for (byte b : s.getBytes(charset)) { appendHex(builder, b); } }
private boolean isValidDomainName(String host) { try { UriCodec.validateSimple(host, "-."); } catch (URISyntaxException e) { return false; } String lastLabel = null; for (String token : host.split("\\.")) { lastLabel = token; if (lastLabel.startsWith("-") || lastLabel.endsWith("-")) { return false; } } if (lastLabel == null) { return false; } if (!lastLabel.equals(host)) { char ch = lastLabel.charAt(0); if (ch >= '0' && ch <= '9') { return false; } } return true; }
result.append("//"); if (escapeIllegalCharacters) { URI.AUTHORITY_ENCODER.appendPartiallyEncoded(result, authority); } else { result.append(authority); if (fileAndQuery != null) { if (escapeIllegalCharacters) { URI.FILE_AND_QUERY_ENCODER.appendPartiallyEncoded(result, fileAndQuery); } else { result.append(fileAndQuery); result.append('#'); if (escapeIllegalCharacters) { URI.ALL_LEGAL_ENCODER.appendPartiallyEncoded(result, ref); } else { result.append(ref);
fragment = ALL_LEGAL_ENCODER.validate(uri, fragmentStart + 1, uri.length(), "fragment"); schemeSpecificPart = ALL_LEGAL_ENCODER.validate( uri, start, fragmentStart, "scheme specific part"); return; authority = AUTHORITY_ENCODER.validate(uri, authorityStart, fileStart, "authority"); path = PATH_ENCODER.validate(uri, fileStart, queryStart, "path"); query = ALL_LEGAL_ENCODER.validate(uri, queryStart + 1, fragmentStart, "query");
/** * Throws if {@code s} is invalid according to this encoder. */ public final String validate(String uri, int start, int end, String name) throws URISyntaxException { for (int i = start; i < end; ) { char ch = uri.charAt(i); if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || isRetained(ch)) { i++; } else if (ch == '%') { if (i + 2 >= end) { throw new URISyntaxException(uri, "Incomplete % sequence in " + name, i); } int d1 = hexToInt(uri.charAt(i + 1)); int d2 = hexToInt(uri.charAt(i + 2)); if (d1 == -1 || d2 == -1) { throw new URISyntaxException(uri, "Invalid % sequence: " + uri.substring(i, i + 3) + " in " + name, i); } i += 3; } else { throw new URISyntaxException(uri, "Illegal character in " + name, i); } } return uri.substring(start, end); }
|| (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || isRetained(c) || (c == '%' && isPartiallyEncoded)) { if (escapeStart != -1) { appendHex(builder, s.substring(escapeStart, i), charset); escapeStart = -1; appendHex(builder, s.substring(escapeStart, s.length()), charset);
public final void appendEncoded(StringBuilder builder, String s) { appendEncoded(builder, s, StandardCharsets.UTF_8, false); }