changeset 48260:8ca86cfb126f

8193034: Optimize URL.toExternalForm Reviewed-by: chegar, alanb, clanger
author martin
date Wed, 06 Dec 2017 15:51:06 -0800
parents c0bf7d8af037
children 43edfde828ab
files src/java.base/share/classes/java/net/URLStreamHandler.java
diffstat 1 files changed, 8 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- 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 : "");
     }
 
     /**