Exemplo n.º 1
0
  /**
   * Creates a new URL instance using the given arguments. The URL uses the specified port instead
   * of the default port for the given protocol.
   *
   * @param protocol the protocol of the new URL.
   * @param host the host name or IP address of the new URL.
   * @param port the specific port number of the URL. {@code -1} represents the default port of the
   *     protocol.
   * @param file the name of the resource.
   * @param handler the stream handler to be used by this URL.
   * @throws IOException if the combination of all arguments do not represent a valid URL or the
   *     protocol is invalid.
   * @throws SecurityException if {@code handler} is non-{@code null}, and a security manager is
   *     installed that disallows user-defined protocol handlers.
   */
  public URL(String protocol, String host, int port, String file) throws IOException {
    if (port < -1) {
      throw new IOException("Port out of range: " + port);
    }

    if (host != null && host.indexOf(":") != -1 && host.charAt(0) != '[') {
      host = "[" + host + "]";
    }

    if (protocol == null) {
      throw new NullPointerException("Unknown protocol: null");
    }

    this.protocol = protocol;
    this.host = host;
    this.port = port;

    // Set the fields from the arguments. Handle the case where the
    // passed in "file" includes both a file and a reference part.
    int index = -1;
    index = file.indexOf("#", file.lastIndexOf("/"));
    if (index >= 0) {
      this.file = file.substring(0, index);
      ref = file.substring(index + 1);
    } else {
      this.file = file;
    }
    fixURL(false);
  }
Exemplo n.º 2
0
 /**
  * Sets the properties of this URL using the provided arguments. Only a {@code URLStreamHandler}
  * can use this method to set fields of the existing URL instance. A URL is generally constant.
  *
  * @param protocol the protocol to be set.
  * @param host the host name to be set.
  * @param port the port number to be set.
  * @param file the file to be set.
  * @param ref the reference to be set.
  */
 protected void set(String protocol, String host, int port, String file, String ref) {
   if (this.protocol == null) {
     this.protocol = protocol;
   }
   this.host = host;
   this.file = file;
   this.port = port;
   this.ref = ref;
   hashCode = 0;
   fixURL(true);
 }