📄 url.java
字号:
/** * This method parses a String representation of a URL within the * context of an existing URL. Principally this means that any * fields not present the URL are inheritied from the context URL. * This allows relative URL's to be easily constructed. If the * context argument is null, then a complete URL must be specified * in the URL string. If the protocol parsed out of the URL is * different from the context URL's protocol, then then URL String * is also expected to be a complete URL. * * @param context The context on which to parse the specification * @param spec The string to parse an URL * * @exception MalformedURLException If a protocol handler cannot be found * for the URL cannot be parsed */ public URL(URL context, String spec) throws MalformedURLException { this(context, spec, (URLStreamHandler) null); } /** * Creates an URL from given arguments * This method parses a String representation of a URL within the * context of an existing URL. Principally this means that any fields * not present the URL are inheritied from the context URL. This allows * relative URL's to be easily constructed. If the context argument is * null, then a complete URL must be specified in the URL string. * If the protocol parsed out of the URL is different * from the context URL's protocol, then then URL String is also * expected to be a complete URL. * <p> * Additionally, this method allows the caller to specify a protocol handler * to use instead of the default. If this handler is specified, the caller * must have the "specifyStreamHandler" permission * (see <code>NetPermission</code>) or a <code>SecurityException</code> * will be thrown. * * @param context The context in which to parse the specification * @param spec The string to parse as an URL * @param ph The stream handler for the URL * * @exception MalformedURLException If a protocol handler cannot be found * or the URL cannot be parsed * @exception SecurityException If the <code>SecurityManager</code> exists * and does not allow the caller to specify its own protocol handler. * * @since 1.2 */ public URL(URL context, String spec, URLStreamHandler ph) throws MalformedURLException { /* A protocol is defined by the doc as the substring before a ':' * as long as the ':' occurs before any '/'. * * If context is null, then spec must be an absolute URL. * * The relative URL need not specify all the components of a URL. * If the protocol, host name, or port number is missing, the value * is inherited from the context. A bare file component is appended * to the context's file. The optional anchor is not inherited. */ // If this is an absolute URL, then ignore context completely. // An absolute URL must have chars prior to "://" but cannot have a colon // right after the "://". The second colon is for an optional port value // and implies that the host from the context is used if available. int colon; int slash = spec.indexOf('/'); if ((colon = spec.indexOf("://", 1)) > 0 && ((colon < slash || slash < 0)) && ! spec.regionMatches(colon, "://:", 0, 4)) context = null; if ((colon = spec.indexOf(':')) > 0 && (colon < slash || slash < 0)) { // Protocol specified in spec string. protocol = spec.substring(0, colon).toLowerCase(); if (context != null && context.protocol.equals(protocol)) { // The 1.2 doc specifically says these are copied to the new URL. host = context.host; port = context.port; userInfo = context.userInfo; authority = context.authority; } } else if (context != null) { // Protocol NOT specified in spec string. // Use context fields (except ref) as a foundation for relative URLs. colon = -1; protocol = context.protocol; host = context.host; port = context.port; userInfo = context.userInfo; if (spec.indexOf(":/", 1) < 0) { file = context.file; if (file == null || file.length() == 0) file = "/"; } authority = context.authority; } else // Protocol NOT specified in spec. and no context available. throw new MalformedURLException("Absolute URL required with null" + " context: " + spec); protocol = protocol.trim(); if (ph != null) { SecurityManager s = System.getSecurityManager(); if (s != null) s.checkPermission(new NetPermission("specifyStreamHandler")); this.ph = ph; } else this.ph = getURLStreamHandler(protocol); if (this.ph == null) throw new MalformedURLException("Protocol handler not found: " + protocol); // JDK 1.2 doc for parseURL specifically states that any '#' ref // is to be excluded by passing the 'limit' as the indexOf the '#' // if one exists, otherwise pass the end of the string. int hashAt = spec.indexOf('#', colon + 1); try { this.ph.parseURL(this, spec, colon + 1, hashAt < 0 ? spec.length() : hashAt); } catch (URLParseError e) { 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 obj 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 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 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, or an empty string if empty. */ 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, or null if empty. * * @since 1.3 */ public String getPath() { // The spec says we need to return an empty string, but some // applications depends on receiving null when the path is empty. if (file == null) return null; int quest = 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() { if (userInfo != null) return userInfo; 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. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -