8193034: Optimize URL.toExternalForm
authormartin
Wed, 06 Dec 2017 15:51:06 -0800
changeset 48260 8ca86cfb126f
parent 48259 c0bf7d8af037
child 48261 43edfde828ab
8193034: Optimize URL.toExternalForm Reviewed-by: chegar, alanb, clanger
src/java.base/share/classes/java/net/URLStreamHandler.java
--- a/src/java.base/share/classes/java/net/URLStreamHandler.java	Wed Dec 13 11:27:28 2017 +0100
+++ b/src/java.base/share/classes/java/net/URLStreamHandler.java	Wed Dec 06 15:51:06 2017 -0800
@@ -480,39 +480,14 @@
      * @return  a string representation of the {@code URL} argument.
      */
     protected String toExternalForm(URL u) {
-
-        // pre-compute length of StringBuffer
-        int len = u.getProtocol().length() + 1;
-        if (u.getAuthority() != null && u.getAuthority().length() > 0)
-            len += 2 + u.getAuthority().length();
-        if (u.getPath() != null) {
-            len += u.getPath().length();
-        }
-        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();
+        String s;
+        return u.getProtocol()
+            + ':'
+            + (((s = u.getAuthority()) != null && s.length() > 0)
+               ? "//" + s : "")
+            + (((s = u.getPath()) != null) ? s : "")
+            + (((s = u.getQuery()) != null) ? '?' + s : "")
+            + (((s = u.getRef()) != null) ? '#' + s : "");
     }
 
     /**