diff -r 0a582b5a2737 -r 7579a0ee92fb emul/src/main/java/java/net/URL.java --- a/emul/src/main/java/java/net/URL.java Tue Oct 30 09:24:41 2012 +0100 +++ b/emul/src/main/java/java/net/URL.java Mon Dec 17 09:55:07 2012 +0100 @@ -196,6 +196,17 @@ */ private String ref; + /** + * The host's IP address, used in equals and hashCode. + * Computed on demand. An uninitialized or unknown hostAddress is null. + */ + transient Object hostAddress; + + /** + * The URLStreamHandler for this URL. + */ + transient URLStreamHandler handler; + /* Our hash code. * @serial */ @@ -308,8 +319,47 @@ this(protocol, host, -1, file); } - private URL(String protocol, String host, int port, String file, - Object handler) throws MalformedURLException { + /** + * Creates a URL object from the specified + * protocol, host, port + * number, file, and handler. Specifying + * a port number of -1 indicates that + * the URL should use the default port for the protocol. Specifying + * a handler of null indicates that the URL + * should use a default stream handler for the protocol, as outlined + * for: + * java.net.URL#URL(java.lang.String, java.lang.String, int, + * java.lang.String) + * + *

If the handler is not null and there is a security manager, + * the security manager's checkPermission + * method is called with a + * NetPermission("specifyStreamHandler") permission. + * This may result in a SecurityException. + * + * No validation of the inputs is performed by this constructor. + * + * @param protocol the name of the protocol to use. + * @param host the name of the host. + * @param port the port number on the host. + * @param file the file on the host + * @param handler the stream handler for the URL. + * @exception MalformedURLException if an unknown protocol is specified. + * @exception SecurityException + * if a security manager exists and its + * checkPermission method doesn't allow + * specifying a stream handler explicitly. + * @see java.lang.System#getProperty(java.lang.String) + * @see java.net.URL#setURLStreamHandlerFactory( + * java.net.URLStreamHandlerFactory) + * @see java.net.URLStreamHandler + * @see java.net.URLStreamHandlerFactory#createURLStreamHandler( + * java.lang.String) + * @see SecurityManager#checkPermission + * @see java.net.NetPermission + */ + public URL(String protocol, String host, int port, String file, + URLStreamHandler handler) throws MalformedURLException { if (handler != null) { throw new SecurityException(); } @@ -348,10 +398,11 @@ // Note: we don't do validation of the URL here. Too risky to change // right now, but worth considering for future reference. -br -// if (handler == null && -// (handler = getURLStreamHandler(protocol)) == null) { -// throw new MalformedURLException("unknown protocol: " + protocol); -// } + if (handler == null && + (handler = getURLStreamHandler(protocol)) == null) { + throw new MalformedURLException("unknown protocol: " + protocol); + } + this.handler = handler; } /** @@ -441,7 +492,7 @@ * @see java.net.URLStreamHandler#parseURL(java.net.URL, * java.lang.String, int, int) */ - private URL(URL context, String spec, Object handler) + public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException { String original = spec; @@ -494,9 +545,9 @@ newProtocol.equalsIgnoreCase(context.protocol))) { // inherit the protocol handler from the context // if not specified to the constructor -// if (handler == null) { -// handler = context.handler; -// } + if (handler == null) { + handler = context.handler; + } // If the context is a hierarchical URL scheme and the spec // contains a matching scheme then maintain backwards @@ -523,15 +574,15 @@ // Get the protocol handler if not specified or the protocol // of the context could not be used -// if (handler == null && -// (handler = getURLStreamHandler(protocol)) == null) { -// throw new MalformedURLException("unknown protocol: "+protocol); -// } - -// this.handler = handler; + if (handler == null && + (handler = getURLStreamHandler(protocol)) == null) { + throw new MalformedURLException("unknown protocol: "+protocol); + } + this.handler = handler; i = spec.indexOf('#', start); if (i >= 0) { +//thrw(protocol + " hnd: " + handler.getClass().getName() + " i: " + i); ref = spec.substring(i + 1, limit); limit = i; } @@ -547,7 +598,7 @@ } } -// handler.parseURL(this, spec, start, limit); + handler.parseURL(this, spec, start, limit); } catch(MalformedURLException e) { throw e; @@ -557,7 +608,7 @@ throw exception; } } - + /* * Returns true if specified string is a valid protocol name. */ @@ -601,6 +652,7 @@ /* This is very important. We must recompute this after the * URL has been changed. */ hashCode = -1; + hostAddress = null; int q = file.lastIndexOf('?'); if (q != -1) { query = file.substring(q+1); @@ -639,6 +691,7 @@ /* This is very important. We must recompute this after the * URL has been changed. */ hashCode = -1; + hostAddress = null; this.query = query; this.authority = authority; } @@ -697,6 +750,19 @@ } /** + * Gets the default port number of the protocol associated + * with this URL. If the URL scheme or the URLStreamHandler + * for the URL do not define a default port number, + * then -1 is returned. + * + * @return the port number + * @since 1.4 + */ + public int getDefaultPort() { + return handler.getDefaultPort(); + } + + /** * Gets the protocol name of this URL. * * @return the protocol of this URL. @@ -773,8 +839,7 @@ return false; URL u2 = (URL)obj; - // return handler.equals(this, u2); - return u2 == this; + return handler.equals(this, u2); } /** @@ -789,7 +854,7 @@ if (hashCode != -1) return hashCode; - // hashCode = handler.hashCode(this); + hashCode = handler.hashCode(this); return hashCode; } @@ -805,8 +870,7 @@ * false otherwise. */ public boolean sameFile(URL other) { -// return handler.sameFile(this, other); - throw new UnsupportedOperationException(); + return handler.sameFile(this, other); } /** @@ -834,8 +898,7 @@ * @see java.net.URLStreamHandler#toExternalForm(java.net.URL) */ public String toExternalForm() { - throw new UnsupportedOperationException(); -// return handler.toExternalForm(this); + return handler.toExternalForm(this); } /** @@ -925,6 +988,11 @@ // return openConnection().getContent(classes); } + static URLStreamHandler getURLStreamHandler(String protocol) { + Class c = URLStreamHandler.class; // XXX only here to pre-initialize URLStreamHandler + URLStreamHandler universal = new URLStreamHandler() {}; + return universal; + } }