8193034: Optimize URL.toExternalForm

Reviewed-by: chegar, alanb, clanger
This commit is contained in:
Martin Buchholz 2017-12-06 15:51:06 -08:00
parent 17b766fb1d
commit 3f0f9ef30c

View File

@ -480,39 +480,14 @@ public abstract class URLStreamHandler {
* @return a string representation of the {@code URL} argument. * @return a string representation of the {@code URL} argument.
*/ */
protected String toExternalForm(URL u) { protected String toExternalForm(URL u) {
String s;
// pre-compute length of StringBuffer return u.getProtocol()
int len = u.getProtocol().length() + 1; + ':'
if (u.getAuthority() != null && u.getAuthority().length() > 0) + (((s = u.getAuthority()) != null && s.length() > 0)
len += 2 + u.getAuthority().length(); ? "//" + s : "")
if (u.getPath() != null) { + (((s = u.getPath()) != null) ? s : "")
len += u.getPath().length(); + (((s = u.getQuery()) != null) ? '?' + s : "")
} + (((s = u.getRef()) != null) ? '#' + s : "");
if (u.getQuery() != null) {
len += 1 + u.getQuery().length();
}
if (u.getRef() != null)
len += 1 + u.getRef().length();
StringBuilder result = new StringBuilder(len);
result.append(u.getProtocol());
result.append(":");
if (u.getAuthority() != null && u.getAuthority().length() > 0) {
result.append("//");
result.append(u.getAuthority());
}
if (u.getPath() != null) {
result.append(u.getPath());
}
if (u.getQuery() != null) {
result.append('?');
result.append(u.getQuery());
}
if (u.getRef() != null) {
result.append("#");
result.append(u.getRef());
}
return result.toString();
} }
/** /**