emul/src/main/java/java/net/URL.java
changeset 339 7579a0ee92fb
parent 122 0a582b5a2737
child 340 785b53689e29
child 342 60f9aad12731
     1.1 --- a/emul/src/main/java/java/net/URL.java	Tue Oct 30 09:24:41 2012 +0100
     1.2 +++ b/emul/src/main/java/java/net/URL.java	Mon Dec 17 09:55:07 2012 +0100
     1.3 @@ -196,6 +196,17 @@
     1.4       */
     1.5      private String ref;
     1.6  
     1.7 +    /**
     1.8 +     * The host's IP address, used in equals and hashCode.
     1.9 +     * Computed on demand. An uninitialized or unknown hostAddress is null.
    1.10 +     */
    1.11 +    transient Object hostAddress;
    1.12 +
    1.13 +    /**
    1.14 +     * The URLStreamHandler for this URL.
    1.15 +     */
    1.16 +    transient URLStreamHandler handler;
    1.17 +
    1.18      /* Our hash code.
    1.19       * @serial
    1.20       */
    1.21 @@ -308,8 +319,47 @@
    1.22          this(protocol, host, -1, file);
    1.23      }
    1.24  
    1.25 -    private URL(String protocol, String host, int port, String file,
    1.26 -               Object handler) throws MalformedURLException {
    1.27 +    /**
    1.28 +     * Creates a <code>URL</code> object from the specified
    1.29 +     * <code>protocol</code>, <code>host</code>, <code>port</code>
    1.30 +     * number, <code>file</code>, and <code>handler</code>. Specifying
    1.31 +     * a <code>port</code> number of <code>-1</code> indicates that
    1.32 +     * the URL should use the default port for the protocol. Specifying
    1.33 +     * a <code>handler</code> of <code>null</code> indicates that the URL
    1.34 +     * should use a default stream handler for the protocol, as outlined
    1.35 +     * for:
    1.36 +     *     java.net.URL#URL(java.lang.String, java.lang.String, int,
    1.37 +     *                      java.lang.String)
    1.38 +     *
    1.39 +     * <p>If the handler is not null and there is a security manager,
    1.40 +     * the security manager's <code>checkPermission</code>
    1.41 +     * method is called with a
    1.42 +     * <code>NetPermission("specifyStreamHandler")</code> permission.
    1.43 +     * This may result in a SecurityException.
    1.44 +     *
    1.45 +     * No validation of the inputs is performed by this constructor.
    1.46 +     *
    1.47 +     * @param      protocol   the name of the protocol to use.
    1.48 +     * @param      host       the name of the host.
    1.49 +     * @param      port       the port number on the host.
    1.50 +     * @param      file       the file on the host
    1.51 +     * @param      handler    the stream handler for the URL.
    1.52 +     * @exception  MalformedURLException  if an unknown protocol is specified.
    1.53 +     * @exception  SecurityException
    1.54 +     *        if a security manager exists and its
    1.55 +     *        <code>checkPermission</code> method doesn't allow
    1.56 +     *        specifying a stream handler explicitly.
    1.57 +     * @see        java.lang.System#getProperty(java.lang.String)
    1.58 +     * @see        java.net.URL#setURLStreamHandlerFactory(
    1.59 +     *                  java.net.URLStreamHandlerFactory)
    1.60 +     * @see        java.net.URLStreamHandler
    1.61 +     * @see        java.net.URLStreamHandlerFactory#createURLStreamHandler(
    1.62 +     *                  java.lang.String)
    1.63 +     * @see        SecurityManager#checkPermission
    1.64 +     * @see        java.net.NetPermission
    1.65 +     */
    1.66 +    public URL(String protocol, String host, int port, String file,
    1.67 +               URLStreamHandler handler) throws MalformedURLException {
    1.68          if (handler != null) {
    1.69              throw new SecurityException();
    1.70          }
    1.71 @@ -348,10 +398,11 @@
    1.72  
    1.73          // Note: we don't do validation of the URL here. Too risky to change
    1.74          // right now, but worth considering for future reference. -br
    1.75 -//        if (handler == null &&
    1.76 -//            (handler = getURLStreamHandler(protocol)) == null) {
    1.77 -//            throw new MalformedURLException("unknown protocol: " + protocol);
    1.78 -//        }
    1.79 +        if (handler == null &&
    1.80 +            (handler = getURLStreamHandler(protocol)) == null) {
    1.81 +            throw new MalformedURLException("unknown protocol: " + protocol);
    1.82 +        }
    1.83 +        this.handler = handler;
    1.84      }
    1.85  
    1.86      /**
    1.87 @@ -441,7 +492,7 @@
    1.88       * @see        java.net.URLStreamHandler#parseURL(java.net.URL,
    1.89       *                  java.lang.String, int, int)
    1.90       */
    1.91 -    private URL(URL context, String spec, Object handler)
    1.92 +    public URL(URL context, String spec, URLStreamHandler handler)
    1.93          throws MalformedURLException
    1.94      {
    1.95          String original = spec;
    1.96 @@ -494,9 +545,9 @@
    1.97                              newProtocol.equalsIgnoreCase(context.protocol))) {
    1.98                  // inherit the protocol handler from the context
    1.99                  // if not specified to the constructor
   1.100 -//                if (handler == null) {
   1.101 -//                    handler = context.handler;
   1.102 -//                }
   1.103 +                if (handler == null) {
   1.104 +                    handler = context.handler;
   1.105 +                }
   1.106  
   1.107                  // If the context is a hierarchical URL scheme and the spec
   1.108                  // contains a matching scheme then maintain backwards
   1.109 @@ -523,15 +574,15 @@
   1.110  
   1.111              // Get the protocol handler if not specified or the protocol
   1.112              // of the context could not be used
   1.113 -//            if (handler == null &&
   1.114 -//                (handler = getURLStreamHandler(protocol)) == null) {
   1.115 -//                throw new MalformedURLException("unknown protocol: "+protocol);
   1.116 -//            }
   1.117 -
   1.118 -//            this.handler = handler;
   1.119 +            if (handler == null &&
   1.120 +                (handler = getURLStreamHandler(protocol)) == null) {
   1.121 +                throw new MalformedURLException("unknown protocol: "+protocol);
   1.122 +            }
   1.123 +            this.handler = handler;
   1.124  
   1.125              i = spec.indexOf('#', start);
   1.126              if (i >= 0) {
   1.127 +//thrw(protocol + " hnd: " + handler.getClass().getName() + " i: " + i);
   1.128                  ref = spec.substring(i + 1, limit);
   1.129                  limit = i;
   1.130              }
   1.131 @@ -547,7 +598,7 @@
   1.132                  }
   1.133              }
   1.134  
   1.135 -//            handler.parseURL(this, spec, start, limit);
   1.136 +            handler.parseURL(this, spec, start, limit);
   1.137  
   1.138          } catch(MalformedURLException e) {
   1.139              throw e;
   1.140 @@ -557,7 +608,7 @@
   1.141              throw exception;
   1.142          }
   1.143      }
   1.144 -
   1.145 +    
   1.146      /*
   1.147       * Returns true if specified string is a valid protocol name.
   1.148       */
   1.149 @@ -601,6 +652,7 @@
   1.150              /* This is very important. We must recompute this after the
   1.151               * URL has been changed. */
   1.152              hashCode = -1;
   1.153 +            hostAddress = null;
   1.154              int q = file.lastIndexOf('?');
   1.155              if (q != -1) {
   1.156                  query = file.substring(q+1);
   1.157 @@ -639,6 +691,7 @@
   1.158              /* This is very important. We must recompute this after the
   1.159               * URL has been changed. */
   1.160              hashCode = -1;
   1.161 +            hostAddress = null;
   1.162              this.query = query;
   1.163              this.authority = authority;
   1.164          }
   1.165 @@ -697,6 +750,19 @@
   1.166      }
   1.167  
   1.168      /**
   1.169 +     * Gets the default port number of the protocol associated
   1.170 +     * with this <code>URL</code>. If the URL scheme or the URLStreamHandler
   1.171 +     * for the URL do not define a default port number,
   1.172 +     * then -1 is returned.
   1.173 +     *
   1.174 +     * @return  the port number
   1.175 +     * @since 1.4
   1.176 +     */
   1.177 +    public int getDefaultPort() {
   1.178 +        return handler.getDefaultPort();
   1.179 +    }
   1.180 +
   1.181 +    /**
   1.182       * Gets the protocol name of this <code>URL</code>.
   1.183       *
   1.184       * @return  the protocol of this <code>URL</code>.
   1.185 @@ -773,8 +839,7 @@
   1.186              return false;
   1.187          URL u2 = (URL)obj;
   1.188  
   1.189 -     //   return handler.equals(this, u2);
   1.190 -        return u2 == this;
   1.191 +        return handler.equals(this, u2);
   1.192      }
   1.193  
   1.194      /**
   1.195 @@ -789,7 +854,7 @@
   1.196          if (hashCode != -1)
   1.197              return hashCode;
   1.198  
   1.199 -     //   hashCode = handler.hashCode(this);
   1.200 +        hashCode = handler.hashCode(this);
   1.201          return hashCode;
   1.202      }
   1.203  
   1.204 @@ -805,8 +870,7 @@
   1.205       *          <code>false</code> otherwise.
   1.206       */
   1.207      public boolean sameFile(URL other) {
   1.208 -//        return handler.sameFile(this, other);
   1.209 -        throw new UnsupportedOperationException();
   1.210 +        return handler.sameFile(this, other);
   1.211      }
   1.212  
   1.213      /**
   1.214 @@ -834,8 +898,7 @@
   1.215       * @see     java.net.URLStreamHandler#toExternalForm(java.net.URL)
   1.216       */
   1.217      public String toExternalForm() {
   1.218 -        throw new UnsupportedOperationException();
   1.219 -//        return handler.toExternalForm(this);
   1.220 +        return handler.toExternalForm(this);
   1.221      }
   1.222  
   1.223      /**
   1.224 @@ -925,6 +988,11 @@
   1.225  //        return openConnection().getContent(classes);
   1.226      }
   1.227  
   1.228 +    static URLStreamHandler getURLStreamHandler(String protocol) {
   1.229 +        Class<URLStreamHandler> c = URLStreamHandler.class; // XXX only here to pre-initialize URLStreamHandler
   1.230 +        URLStreamHandler universal = new URLStreamHandler() {};
   1.231 +        return universal;
   1.232 +    }
   1.233  
   1.234  }
   1.235