8146686: Create the schemeSpecificPart for non-opaque URIs lazily
authorredestad
Sun, 10 Jan 2016 14:14:13 +0100
changeset 34939 4fd867517aec
parent 34938 7346fb82f58f
child 34940 8119cd84edbb
8146686: Create the schemeSpecificPart for non-opaque URIs lazily Reviewed-by: alanb, chegar
jdk/src/java.base/share/classes/java/net/URI.java
--- a/jdk/src/java.base/share/classes/java/net/URI.java	Sat Jan 09 11:19:32 2016 -0400
+++ b/jdk/src/java.base/share/classes/java/net/URI.java	Sun Jan 10 14:14:13 2016 +0100
@@ -1146,13 +1146,30 @@
         if (part != null) {
             return part;
         }
-        StringBuilder sb = new StringBuilder();
-        appendSchemeSpecificPart(sb, null, getAuthority(), getUserInfo(),
+
+        String s = string;
+        if (s != null) {
+            // if string is defined, components will have been parsed
+            int start = 0;
+            int end = s.length();
+            if (scheme != null) {
+                start = scheme.length() + 1;
+            }
+            if (fragment != null) {
+                end -= fragment.length() + 1;
+            }
+            if (path != null && path.length() == end - start) {
+                part = path;
+            } else {
+                part = s.substring(start, end);
+            }
+        } else {
+            StringBuilder sb = new StringBuilder();
+            appendSchemeSpecificPart(sb, null, getAuthority(), getUserInfo(),
                                  host, port, getPath(), getQuery());
-        if (sb.length() == 0) {
-            return null;
+            part = sb.toString();
         }
-        return schemeSpecificPart = sb.toString();
+        return schemeSpecificPart = part;
     }
 
     /**
@@ -3056,7 +3073,6 @@
         //
         void parse(boolean rsa) throws URISyntaxException {
             requireServerAuthority = rsa;
-            int ssp;                    // Start of scheme-specific part
             int n = input.length();
             int p = scan(0, n, "/?#", ":");
             if ((p >= 0) && at(p, n, ':')) {
@@ -3066,21 +3082,20 @@
                 checkChars(1, p, L_SCHEME, H_SCHEME, "scheme name");
                 scheme = input.substring(0, p);
                 p++;                    // Skip ':'
-                ssp = p;
                 if (at(p, n, '/')) {
                     p = parseHierarchical(p, n);
                 } else {
+                    // opaque; need to create the schemeSpecificPart
                     int q = scan(p, n, "#");
                     if (q <= p)
                         failExpecting("scheme-specific part", p);
                     checkChars(p, q, L_URIC, H_URIC, "opaque part");
+                    schemeSpecificPart = input.substring(p, q);
                     p = q;
                 }
             } else {
-                ssp = 0;
                 p = parseHierarchical(0, n);
             }
-            schemeSpecificPart = input.substring(ssp, p);
             if (at(p, n, '#')) {
                 checkChars(p + 1, n, L_URIC, H_URIC, "fragment");
                 fragment = input.substring(p + 1, n);