< prev index next >

src/java.base/share/classes/java/net/URLStreamHandler.java

Print this page
rev 51919 : 8215281: Use String.isEmpty() when applicable in java.base
Reviewed-by: dfuchs, alanb


 218                     ind = host.indexOf(':');
 219                     port = -1;
 220                     if (ind >= 0) {
 221                         // port can be null according to RFC2396
 222                         if (host.length() > (ind + 1)) {
 223                             port = Integer.parseInt(host, ind + 1,
 224                                     host.length(), 10);
 225                         }
 226                         host = host.substring(0, ind);
 227                     }
 228                 }
 229             } else {
 230                 host = "";
 231             }
 232             if (port < -1)
 233                 throw new IllegalArgumentException("Invalid port number :" +
 234                                                    port);
 235             start = i;
 236             // If the authority is defined then the path is defined by the
 237             // spec only; See RFC 2396 Section 5.2.4.
 238             if (authority != null && authority.length() > 0)
 239                 path = "";
 240         }
 241 
 242         if (host == null) {
 243             host = "";
 244         }
 245 
 246         // Parse the file path if any
 247         if (start < limit) {
 248             if (spec.charAt(start) == '/') {
 249                 path = spec.substring(start, limit);
 250             } else if (path != null && path.length() > 0) {
 251                 isRelPath = true;
 252                 int ind = path.lastIndexOf('/');
 253                 String seperator = "";
 254                 if (ind == -1 && authority != null)
 255                     seperator = "/";
 256                 path = path.substring(0, ind + 1) + seperator +
 257                          spec.substring(start, limit);
 258 
 259             } else {
 260                 String seperator = (authority != null) ? "/" : "";
 261                 path = seperator + spec.substring(start, limit);
 262             }
 263         } else if (queryOnly && path != null) {
 264             int ind = path.lastIndexOf('/');
 265             if (ind < 0)
 266                 ind = 0;
 267             path = path.substring(0, ind) + "/";
 268         }
 269         if (path == null)
 270             path = "";


 466         if (a1 != null && a2 != null) {
 467             return a1.equals(a2);
 468         // else, if both have host names, compare them
 469         } else if (u1.getHost() != null && u2.getHost() != null)
 470             return u1.getHost().equalsIgnoreCase(u2.getHost());
 471          else
 472             return u1.getHost() == null && u2.getHost() == null;
 473     }
 474 
 475     /**
 476      * Converts a {@code URL} of a specific protocol to a
 477      * {@code String}.
 478      *
 479      * @param   u   the URL.
 480      * @return  a string representation of the {@code URL} argument.
 481      */
 482     protected String toExternalForm(URL u) {
 483         String s;
 484         return u.getProtocol()
 485             + ':'
 486             + (((s = u.getAuthority()) != null && s.length() > 0)
 487                ? "//" + s : "")
 488             + (((s = u.getPath()) != null) ? s : "")
 489             + (((s = u.getQuery()) != null) ? '?' + s : "")
 490             + (((s = u.getRef()) != null) ? '#' + s : "");
 491     }
 492 
 493     /**
 494      * Sets the fields of the {@code URL} argument to the indicated values.
 495      * Only classes derived from URLStreamHandler are able
 496      * to use this method to set the values of the URL fields.
 497      *
 498      * @param   u         the URL to modify.
 499      * @param   protocol  the protocol name.
 500      * @param   host      the remote host value for the URL.
 501      * @param   port      the port on the remote machine.
 502      * @param   authority the authority part for the URL.
 503      * @param   userInfo the userInfo part of the URL.
 504      * @param   path      the path component of the URL.
 505      * @param   query     the query part for the URL.
 506      * @param   ref       the reference.
 507      * @exception       SecurityException       if the protocol handler of the URL is
 508      *                                  different from this one
 509      * @since 1.3
 510      */


 527      * @param   u         the URL to modify.
 528      * @param   protocol  the protocol name. This value is ignored since 1.2.
 529      * @param   host      the remote host value for the URL.
 530      * @param   port      the port on the remote machine.
 531      * @param   file      the file.
 532      * @param   ref       the reference.
 533      * @exception       SecurityException       if the protocol handler of the URL is
 534      *                                  different from this one
 535      * @deprecated Use setURL(URL, String, String, int, String, String, String,
 536      *             String);
 537      */
 538     @Deprecated
 539     protected void setURL(URL u, String protocol, String host, int port,
 540                           String file, String ref) {
 541         /*
 542          * Only old URL handlers call this, so assume that the host
 543          * field might contain "user:passwd@host". Fix as necessary.
 544          */
 545         String authority = null;
 546         String userInfo = null;
 547         if (host != null && host.length() != 0) {
 548             authority = (port == -1) ? host : host + ":" + port;
 549             int at = host.lastIndexOf('@');
 550             if (at != -1) {
 551                 userInfo = host.substring(0, at);
 552                 host = host.substring(at+1);
 553             }
 554         }
 555 
 556         /*
 557          * Assume file might contain query part. Fix as necessary.
 558          */
 559         String path = null;
 560         String query = null;
 561         if (file != null) {
 562             int q = file.lastIndexOf('?');
 563             if (q != -1) {
 564                 query = file.substring(q+1);
 565                 path = file.substring(0, q);
 566             } else
 567                 path = file;


 218                     ind = host.indexOf(':');
 219                     port = -1;
 220                     if (ind >= 0) {
 221                         // port can be null according to RFC2396
 222                         if (host.length() > (ind + 1)) {
 223                             port = Integer.parseInt(host, ind + 1,
 224                                     host.length(), 10);
 225                         }
 226                         host = host.substring(0, ind);
 227                     }
 228                 }
 229             } else {
 230                 host = "";
 231             }
 232             if (port < -1)
 233                 throw new IllegalArgumentException("Invalid port number :" +
 234                                                    port);
 235             start = i;
 236             // If the authority is defined then the path is defined by the
 237             // spec only; See RFC 2396 Section 5.2.4.
 238             if (authority != null && !authority.isEmpty())
 239                 path = "";
 240         }
 241 
 242         if (host == null) {
 243             host = "";
 244         }
 245 
 246         // Parse the file path if any
 247         if (start < limit) {
 248             if (spec.charAt(start) == '/') {
 249                 path = spec.substring(start, limit);
 250             } else if (path != null && !path.isEmpty()) {
 251                 isRelPath = true;
 252                 int ind = path.lastIndexOf('/');
 253                 String seperator = "";
 254                 if (ind == -1 && authority != null)
 255                     seperator = "/";
 256                 path = path.substring(0, ind + 1) + seperator +
 257                          spec.substring(start, limit);
 258 
 259             } else {
 260                 String seperator = (authority != null) ? "/" : "";
 261                 path = seperator + spec.substring(start, limit);
 262             }
 263         } else if (queryOnly && path != null) {
 264             int ind = path.lastIndexOf('/');
 265             if (ind < 0)
 266                 ind = 0;
 267             path = path.substring(0, ind) + "/";
 268         }
 269         if (path == null)
 270             path = "";


 466         if (a1 != null && a2 != null) {
 467             return a1.equals(a2);
 468         // else, if both have host names, compare them
 469         } else if (u1.getHost() != null && u2.getHost() != null)
 470             return u1.getHost().equalsIgnoreCase(u2.getHost());
 471          else
 472             return u1.getHost() == null && u2.getHost() == null;
 473     }
 474 
 475     /**
 476      * Converts a {@code URL} of a specific protocol to a
 477      * {@code String}.
 478      *
 479      * @param   u   the URL.
 480      * @return  a string representation of the {@code URL} argument.
 481      */
 482     protected String toExternalForm(URL u) {
 483         String s;
 484         return u.getProtocol()
 485             + ':'
 486             + ((s = u.getAuthority()) != null && !s.isEmpty()
 487                ? "//" + s : "")
 488             + ((s = u.getPath()) != null ? s : "")
 489             + ((s = u.getQuery()) != null ? '?' + s : "")
 490             + ((s = u.getRef()) != null ? '#' + s : "");
 491     }
 492 
 493     /**
 494      * Sets the fields of the {@code URL} argument to the indicated values.
 495      * Only classes derived from URLStreamHandler are able
 496      * to use this method to set the values of the URL fields.
 497      *
 498      * @param   u         the URL to modify.
 499      * @param   protocol  the protocol name.
 500      * @param   host      the remote host value for the URL.
 501      * @param   port      the port on the remote machine.
 502      * @param   authority the authority part for the URL.
 503      * @param   userInfo the userInfo part of the URL.
 504      * @param   path      the path component of the URL.
 505      * @param   query     the query part for the URL.
 506      * @param   ref       the reference.
 507      * @exception       SecurityException       if the protocol handler of the URL is
 508      *                                  different from this one
 509      * @since 1.3
 510      */


 527      * @param   u         the URL to modify.
 528      * @param   protocol  the protocol name. This value is ignored since 1.2.
 529      * @param   host      the remote host value for the URL.
 530      * @param   port      the port on the remote machine.
 531      * @param   file      the file.
 532      * @param   ref       the reference.
 533      * @exception       SecurityException       if the protocol handler of the URL is
 534      *                                  different from this one
 535      * @deprecated Use setURL(URL, String, String, int, String, String, String,
 536      *             String);
 537      */
 538     @Deprecated
 539     protected void setURL(URL u, String protocol, String host, int port,
 540                           String file, String ref) {
 541         /*
 542          * Only old URL handlers call this, so assume that the host
 543          * field might contain "user:passwd@host". Fix as necessary.
 544          */
 545         String authority = null;
 546         String userInfo = null;
 547         if (host != null && !host.isEmpty()) {
 548             authority = (port == -1) ? host : host + ":" + port;
 549             int at = host.lastIndexOf('@');
 550             if (at != -1) {
 551                 userInfo = host.substring(0, at);
 552                 host = host.substring(at+1);
 553             }
 554         }
 555 
 556         /*
 557          * Assume file might contain query part. Fix as necessary.
 558          */
 559         String path = null;
 560         String query = null;
 561         if (file != null) {
 562             int q = file.lastIndexOf('?');
 563             if (q != -1) {
 564                 query = file.substring(q+1);
 565                 path = file.substring(0, q);
 566             } else
 567                 path = file;
< prev index next >