private String decode(String s) { return s != null ? UriCodec.decode(s) : null; }
public final void appendPartiallyEncoded(StringBuilder builder, String s) { appendEncoded(builder, s, StandardCharsets.UTF_8, true); }
private static void appendHex(StringBuilder builder, String s, Charset charset) { for (byte b : s.getBytes(charset)) { appendHex(builder, b); } }
/** * 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); }
public String getContentTypeFor(String filename) { if (filename.endsWith("/")) { // a directory, return html return MimeUtils.guessMimeTypeFromExtension("html"); } int lastCharInExtension = filename.lastIndexOf('#'); if (lastCharInExtension < 0) { lastCharInExtension = filename.length(); } int firstCharInExtension = filename.lastIndexOf('.') + 1; String ext = ""; if (firstCharInExtension > filename.lastIndexOf('/')) { ext = filename.substring(firstCharInExtension, lastCharInExtension); } return MimeUtils.guessMimeTypeFromExtension(ext.toLowerCase(Locale.US)); } }
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; }
@Override protected void finalize() throws Throwable { try { if (guard != null) { guard.warnIfOpen(); } close(); } finally { super.finalize(); } } }
/** * Writes a raw packet to the desired interface. A L2 header will * be added which includes the specified destination address, our * source MAC, and the specified protocol type. The caller is responsible * for computing correct IP-header and payload checksums. */ public int write(byte[] destMac, byte[] packet, int offset, int byteCount) { if (destMac == null) { throw new NullPointerException("destMac == null"); } if (packet == null) { throw new NullPointerException("packet == null"); } Arrays.checkOffsetAndCount(packet.length, offset, byteCount); if (destMac.length != 6) { throw new IllegalArgumentException("MAC length must be 6: " + destMac.length); } return sendPacket(fd, mInterfaceName, mProtocolType, destMac, packet, offset, byteCount); }
/** * Creates a socket on the specified interface. */ public RawSocket(String interfaceName, short protocolType) throws SocketException { mInterfaceName = interfaceName; mProtocolType = protocolType; fd = new FileDescriptor(); create(fd, mProtocolType, mInterfaceName); guard.open("close"); }
public final void appendEncoded(StringBuilder builder, String s) { appendEncoded(builder, s, StandardCharsets.UTF_8, false); }
public static String decode(String s) { return decode(s, false, StandardCharsets.UTF_8, true); }
/** * 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)); } }
public final String encode(String s, Charset charset) { // Guess a bit larger for encoded form StringBuilder builder = new StringBuilder(s.length() + 16); appendEncoded(builder, s, charset, false); return builder.toString(); }
/** * Creates an instance of <code>FileURLConnection</code> for establishing * a connection to the file pointed by this <code>URL<code> * * @param url The URL this connection is connected to */ public FileURLConnection(URL url) { super(url); filename = url.getFile(); if (filename == null) { filename = ""; } filename = UriCodec.decode(filename); }
/** * Returns the textual string representation of this URI instance using the * US-ASCII encoding. * * @return the US-ASCII string representation of this URI. */ public String toASCIIString() { StringBuilder result = new StringBuilder(); ASCII_ONLY.appendEncoded(result, toString()); return result.toString(); }
/** * Decodes the argument which is assumed to be encoded in the {@code * x-www-form-urlencoded} MIME content type, assuming the given {@code charsetName}. * *'<p>+' will be converted to space, '%' and two following hex digit * characters are converted to the equivalent byte value. All other * characters are passed through unmodified. For example "A+B+C %24%25" -> * "A B C $%". * * @throws UnsupportedEncodingException if {@code charsetName} is not supported. */ public static String decode(String s, String charsetName) throws UnsupportedEncodingException { return UriCodec.decode(s, true, Charset.forName(charsetName), true); } }
URL targetURL(URL base, String name) { try { StringBuilder fileBuilder = new StringBuilder(); fileBuilder.append(base.getFile()); URI.PATH_ENCODER.appendEncoded(fileBuilder, name); String file = fileBuilder.toString(); return new URL(base.getProtocol(), base.getHost(), base.getPort(), file, null); } catch (MalformedURLException e) { return null; } }
/** * Decodes the argument which is assumed to be encoded in the {@code * x-www-form-urlencoded} MIME content type. * <p> *'+' will be converted to space, '%' and two following hex digit * characters are converted to the equivalent byte value. All other * characters are passed through unmodified. For example "A+B+C %24%25" -> * "A B C $%". * * @param s * the encoded string. * @return the decoded clear-text representation of the given string. * @deprecated Use {@link #decode(String, String)} instead. */ @Deprecated public static String decode(String s) { return UriCodec.decode(s, true, Charset.defaultCharset(), true); }
/** * Creates a new URI instance of the given unencoded component parts. * * @param scheme the URI scheme, or null for a non-absolute URI. */ public URI(String scheme, String schemeSpecificPart, String fragment) throws URISyntaxException { StringBuilder uri = new StringBuilder(); if (scheme != null) { uri.append(scheme); uri.append(':'); } if (schemeSpecificPart != null) { ALL_LEGAL_ENCODER.appendEncoded(uri, schemeSpecificPart); } if (fragment != null) { uri.append('#'); ALL_LEGAL_ENCODER.appendEncoded(uri, fragment); } parseURI(uri.toString(), false); }