⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 url.java

📁 kaffe Java 解释器语言,源码,Java的子集系统,开放源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	throw new MalformedURLException(e.getMessage());      }        if (hashAt >= 0)      ref = spec.substring(hashAt + 1);    hashCode = hashCode();			// Used for serialization.  }  /**   * Test another URL for equality with this one.  This will be true only if   * the argument is non-null and all of the fields in the URL's match    * exactly (ie, protocol, host, port, file, and ref).  Overrides   * Object.equals(), implemented by calling the equals method of the handler.   *   * @param url The URL to compare with   *   * @return true if the URL is equal, false otherwise   */  public boolean equals (Object obj)  {    if (! (obj instanceof URL))      return false;    return ph.equals (this, (URL) obj);  }  /**   * Returns the contents of this URL as an object by first opening a   * connection, then calling the getContent() method against the connection   *   * @return A content object for this URL   * @exception IOException If opening the connection or getting the   * content fails.   *   * @since 1.3   */  public final Object getContent() throws IOException  {    return openConnection().getContent();  }  /**   * Gets the contents of this URL   *   * @param classes The allow classes for the content object.   *   * @return a context object for this URL.   *   * @exception IOException If an error occurs   */  public final Object getContent (Class[] classes) throws IOException  {    // FIXME: implement this    return getContent();  }  /**   * Returns the file portion of the URL.   * Defined as <code>path[?query]</code>.   * Returns the empty string if there is no file portion.   *   * @return The filename specified in this URL.   */  public String getFile()  {    return file == null ? "" : file;  }  /**   * Returns the path of the URL. This is the part of the file before any '?'   * character.   *   * @return The path specified in this URL.   *    * @since 1.3   */  public String getPath()  {    int quest = (file == null) ? -1 : file.indexOf('?');    return quest < 0 ? getFile() : file.substring(0, quest);  }  /**   * Returns the authority of the URL   *   * @return The authority specified in this URL.   *    * @since 1.3   */  public String getAuthority()  {    return authority;  }  /**   * Returns the host of the URL   *   * @return The host specified in this URL.   */  public String getHost()  {    int at = (host == null) ? -1 : host.indexOf('@');    return at < 0 ? host : host.substring(at + 1, host.length());  }  /**   * Returns the port number of this URL or -1 if the default port number is   * being used.   *   * @return The port number   *   * @see #getDefaultPort()   */  public int getPort()  {    return port;  }  /**   * Returns the default port of the URL. If the StreamHandler for the URL   * protocol does not define a default port it returns -1.   *   * @return The default port of the current protocol.   */  public int getDefaultPort()  {    return ph.getDefaultPort();  }  /**   * Returns the protocol of the URL   *   * @return The specified protocol.   */  public String getProtocol()  {    return protocol;  }  /**   * Returns the ref (sometimes called the "# reference" or "anchor") portion   * of the URL.   *   * @return The ref   */  public String getRef()  {    return ref;  }  /**   * Returns the user information of the URL. This is the part of the host   * name before the '@'.   *   * @return the user at a particular host or null when no user defined.   */  public String getUserInfo ()  {    int at = (host == null) ? -1 : host.indexOf('@');    return at < 0 ? null : host.substring(0, at);  }  /**   * Returns the query of the URL. This is the part of the file before the   * '?'.   *   * @return the query part of the file, or null when there is no query part.   */  public String getQuery ()  {    int quest = (file == null) ? -1 : file.indexOf('?');    return quest < 0 ? null : file.substring(quest + 1, file.length());  }  /**   * Returns a hashcode computed by the URLStreamHandler of this URL   *   * @return The hashcode for this URL.   */  public int hashCode()  {    if (hashCode != 0)      return hashCode;		// Use cached value if available.    else      return ph.hashCode (this);  }  /**   * Returns a URLConnection object that represents a connection to the remote   * object referred to by the URL. The URLConnection is created by calling the   * openConnection() method of the protocol handler   *   * @return A URLConnection for this URL   *   * @exception IOException If an error occurs   */  public URLConnection openConnection() throws IOException  {    return ph.openConnection(this);  }  /**   * Opens a connection to this URL and returns an InputStream for reading   * from that connection   *   * @return An <code>InputStream</code> for this URL.   *    * @exception IOException If an error occurs   */  public final InputStream openStream() throws IOException  {    return openConnection().getInputStream();  }  /**   * Tests whether or not another URL refers to the same "file" as this one.   * This will be true if and only if the passed object is not null, is a   * URL, and matches all fields but the ref (ie, protocol, host, port,   * and file);   *   * @param url The URL object to test with   *   * @return true if URL matches this URL's file, false otherwise   */  public boolean sameFile(URL other)  {    return ph.sameFile(this, other);  }  /**   * Sets the specified fields of the URL. This is not a public method so   * that only URLStreamHandlers can modify URL fields. This might be called   * by the <code>parseURL()</code> method in that class. URLs are otherwise   * constant.   *   * @param protocol The protocol name for this URL   * @param host The hostname or IP address for this URL   * @param port The port number of this URL   * @param file The "file" portion of this URL.   * @param ref The anchor portion of this URL.   */  protected void set(String protocol, String host, int port, String file,		     String ref)  {    // TBD: Theoretically, a poorly written StreamHandler could pass an    // invalid protocol.  It will cause the handler to be set to null    // thus overriding a valid handler.  Callers of this method should    // be aware of this.    this.ph = getURLStreamHandler(protocol);    this.protocol = protocol.toLowerCase();    this.authority = "";    this.port = port;    this.host = host;    this.file = file;    if (host != null)      this.authority += host;    if (port >= 0)      this.authority += ":" + port;    this.ref = ref;    hashCode = hashCode();			// Used for serialization.  }    /**   * Sets the specified fields of the URL. This is not a public method so   * that only URLStreamHandlers can modify URL fields. URLs are otherwise   * constant.   *   * @param protocol The protocol name for this URL.   * @param host The hostname or IP address for this URL.   * @param port The port number of this URL.   * @param authority The authority of this URL.   * @param userInfo The user and password (if needed) of this URL.   * @param path The "path" portion of this URL.   * @param query The query of this URL.   * @param ref The anchor portion of this URL.   *   * @since 1.3   */  protected void set(String protocol, String host, int port,		     String authority, String userInfo,		     String path, String query, String ref)  {    // TBD: Theoretically, a poorly written StreamHandler could pass an    // invalid protocol.  It will cause the handler to be set to null    // thus overriding a valid handler.  Callers of this method should    // be aware of this.    this.ph = getURLStreamHandler(protocol);    this.protocol = protocol.toLowerCase();    this.host = host;    this.userInfo = userInfo;    this.port = port;    this.file = path;    this.authority = authority;    if (query == null)      this.file = file;    else      this.file = file + "?" + query;    this.ref = ref;    hashCode = hashCode();			// Used for serialization.  }  /**   * Sets the URLStreamHandlerFactory for this class.  This factory is   * responsible for returning the appropriate protocol handler for   * a given URL.   *   * @param fac The URLStreamHandlerFactory class to use   *   * @exception Error If the factory is alread set.   * @exception SecurityException If a security manager exists and its   * checkSetFactory method doesn't allow the operation   */  public static synchronized void	setURLStreamHandlerFactory(URLStreamHandlerFactory fac)  {    if (factory != null)      throw new Error("URLStreamHandlerFactory already set");    // Throw an exception if an extant security mgr precludes    // setting the factory.    SecurityManager s = System.getSecurityManager();    if (s != null)      s.checkSetFactory();    factory = fac;  }  /**   * Returns a String representing this URL.  The String returned is   * created by calling the protocol handler's toExternalForm() method.   *   * @return A string for this URL   */  public String toExternalForm()  {    // Identical to toString().    return ph.toExternalForm(this);  }  /**   * Returns a String representing this URL.  Identical to toExternalForm().   * The value returned is created by the protocol handler's    * toExternalForm method.  Overrides Object.toString()   *   * @return A string for this URL   */  public String toString()  {    // Identical to toExternalForm().    return ph.toExternalForm(this);  }  /**   * This internal method is used in two different constructors to load   * a protocol handler for this URL.   *   * @param protocol The protocol to load a handler for   *   * @return A URLStreamHandler for this protocol, or null when not found.   */  private static synchronized URLStreamHandler    getURLStreamHandler (String protocol)  {    URLStreamHandler ph = null;    // First, see if a protocol handler is in our cache.    if (cache_handlers)      {        if ((ph = (URLStreamHandler) ph_cache.get (protocol)) != null)          return ph;      }    // If a non-default factory has been set, use it to find the protocol.    if (factory != null)      {	ph = factory.createURLStreamHandler (protocol);      }    // Non-default factory may have returned null or a factory wasn't set.    // Use the default search algorithm to find a handler for this protocol.    if (ph == null)      {	// Get the list of packages to check and append our default handler	// to it, along with the JDK specified default as a last resort.	// Except in very unusual environments the JDK specified one shouldn't	// ever be needed (or available).	String ph_search_path =	  System.getProperty("java.protocol.handler.pkgs");	// Tack our default package on at the ends.	if (ph_search_path != null)          ph_search_path += "|" + DEFAULT_SEARCH_PATH;	else          ph_search_path = DEFAULT_SEARCH_PATH;	// Finally loop through our search path looking for a match.	StringTokenizer pkgPrefix = new StringTokenizer (ph_search_path, "|");        	do          {            String clsName = (pkgPrefix.nextToken() + "."			      + protocol + ".Handler");                     try              {                Object obj = Class.forName (clsName,                                            true,                                            Thread.currentThread().getContextClassLoader()).newInstance();	                    if (!(obj instanceof URLStreamHandler))                  continue;                else                  ph = (URLStreamHandler) obj;              }            catch (Exception e)              {                // Can't instantiate; handler still null,		// go on to next element.              }          }	while ((! (ph instanceof URLStreamHandler))               && pkgPrefix.hasMoreTokens());      }    // Update the hashtable with the new protocol handler.    if (ph != null        && cache_handlers)      if (ph instanceof URLStreamHandler)	ph_cache.put (protocol, ph);      else	ph = null;    return ph;  }  private void readObject(ObjectInputStream ois)    throws IOException, ClassNotFoundException  {    ois.defaultReadObject();    this.ph = getURLStreamHandler(protocol);    if (this.ph == null)      throw new IOException("Handler for protocol " + protocol + " not found");  }  private void writeObject(ObjectOutputStream oos) throws IOException  {    oos.defaultWriteObject();  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -