< prev index next >

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

Print this page
8193034: Optimize URL.toExternalForm
Reviewed-by: chegar, alanb, clanger


 463         InetAddress a1 = getHostAddress(u1);
 464         InetAddress a2 = getHostAddress(u2);
 465         // if we have internet address for both, compare them
 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 
 484         // pre-compute length of StringBuffer
 485         int len = u.getProtocol().length() + 1;
 486         if (u.getAuthority() != null && u.getAuthority().length() > 0)
 487             len += 2 + u.getAuthority().length();
 488         if (u.getPath() != null) {
 489             len += u.getPath().length();
 490         }
 491         if (u.getQuery() != null) {
 492             len += 1 + u.getQuery().length();
 493         }
 494         if (u.getRef() != null)
 495             len += 1 + u.getRef().length();
 496 
 497         StringBuilder result = new StringBuilder(len);
 498         result.append(u.getProtocol());
 499         result.append(":");
 500         if (u.getAuthority() != null && u.getAuthority().length() > 0) {
 501             result.append("//");
 502             result.append(u.getAuthority());
 503         }
 504         if (u.getPath() != null) {
 505             result.append(u.getPath());
 506         }
 507         if (u.getQuery() != null) {
 508             result.append('?');
 509             result.append(u.getQuery());
 510         }
 511         if (u.getRef() != null) {
 512             result.append("#");
 513             result.append(u.getRef());
 514         }
 515         return result.toString();
 516     }
 517 
 518     /**
 519      * Sets the fields of the {@code URL} argument to the indicated values.
 520      * Only classes derived from URLStreamHandler are able
 521      * to use this method to set the values of the URL fields.
 522      *
 523      * @param   u         the URL to modify.
 524      * @param   protocol  the protocol name.
 525      * @param   host      the remote host value for the URL.
 526      * @param   port      the port on the remote machine.
 527      * @param   authority the authority part for the URL.
 528      * @param   userInfo the userInfo part of the URL.
 529      * @param   path      the path component of the URL.
 530      * @param   query     the query part for the URL.
 531      * @param   ref       the reference.
 532      * @exception       SecurityException       if the protocol handler of the URL is
 533      *                                  different from this one
 534      * @since 1.3
 535      */




 463         InetAddress a1 = getHostAddress(u1);
 464         InetAddress a2 = getHostAddress(u2);
 465         // if we have internet address for both, compare them
 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      */


< prev index next >