urlstreamhandler.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 549 行 · 第 1/2 页

JAVA
549
字号
/* * @(#)URLStreamHandler.java	1.55 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * */package java.net;import java.io.IOException;/** * The abstract class <code>URLStreamHandler</code> is the common * superclass for all stream protocol handlers. A stream protocol * handler knows how to make a connection for a particular protocol * type, such as <code>http</code>. * <p> * In most cases, an instance of a <code>URLStreamHandler</code> * subclass is not created directly by an application. Rather, the * first time a protocol name is encountered when constructing a * <code>URL</code>, the appropriate stream protocol handler is * automatically loaded. * * @author  James Gosling * @version 1.48 10/27/00 * @see     java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String) * @since   JDK1.0 */public abstract class URLStreamHandler {    /**     * Opens a connection to the object referenced by the     * <code>URL</code> argument.     * This method should be overridden by a subclass.     *     * <p>If for the handler's protocol (such as HTTP or JAR), there     * exists a public, specialized URLConnection subclass belonging     * to one of the following packages or one of their subpackages:     * java.lang, java.io, java.util, java.net, the connection     * returned will be of that subclass. For example, for HTTP an     * HttpURLConnection will be returned, and for JAR a     * JarURLConnection will be returned.     * NOTE: <B>java.net.HttpURLConnection</B> is found in J2ME CDC profiles      * such as J2ME Foundation Profile.     *     * @param      u   the URL that this connects to.     * @return     a <code>URLConnection</code> object for the <code>URL</code>.     * @exception  IOException  if an I/O error occurs while opening the     *               connection.     */    abstract protected URLConnection openConnection(URL u) throws IOException;    /**     * Parses the string representation of a <code>URL</code> into a     * <code>URL</code> object.     * <p>     * If there is any inherited context, then it has already been     * copied into the <code>URL</code> argument.     * <p>     * The <code>parseURL</code> method of <code>URLStreamHandler</code>     * parses the string representation as if it were an     * <code>http</code> specification. Most URL protocol families have a     * similar parsing. A stream protocol handler for a protocol that has     * a different syntax must override this routine.     *     * @param   u       the <code>URL</code> to receive the result of parsing     *                  the spec.     * @param   spec    the <code>String</code> representing the URL that     *                  must be parsed.     * @param   start   the character index at which to begin parsing. This is     *                  just past the '<code>:</code>' (if there is one) that     *                  specifies the determination of the protocol name.     * @param   limit   the character position to stop parsing at. This is the     *                  end of the string or the position of the     *                  "<code>#</code>" character, if present. All information     *                  after the sharp sign indicates an anchor.     */    protected void parseURL(URL u, String spec, int start, int limit) {        // These fields may receive context content if this was relative URL        String protocol = u.getProtocol();        String authority = u.getAuthority();         String userInfo = u.getUserInfo();        String host = u.getHost();        int port = u.getPort();        String path = u.getPath();	String query = u.getQuery();        // This field has already been parsed        String ref = u.getRef();	boolean isRelPath = false;	boolean queryOnly = false;// FIX: should not assume query if opaque        // Strip off the query part	if (start < limit) {            int queryStart = spec.indexOf('?');            queryOnly = queryStart == start;            if ((queryStart != -1) && (queryStart < limit)) {                query = spec.substring(queryStart+1, limit);                if (limit > queryStart)                    limit = queryStart;                spec = spec.substring(0, queryStart);            }	}	int i = 0;        // Parse the authority part if any	if ((start <= limit - 2) && (spec.charAt(start) == '/') &&	    (spec.charAt(start + 1) == '/')) {	    start += 2;	    i = spec.indexOf('/', start);            if (i < 0) {	        i = spec.indexOf('?', start);		if (i < 0)                    i = limit;	    }            host = authority = spec.substring(start, i);            int ind = authority.indexOf('@');            if (ind != -1) {                userInfo = authority.substring(0, ind);                host = authority.substring(ind+1);	    } else {		userInfo = null;	    }	    if (host != null) {		// If the host is surrounded by [ and ] then its an IPv6 		// literal address as specified in RFC2732		if (host.length()>0 && (host.charAt(0) == '[')) {		    if ((ind = host.indexOf(']')) > 2) {		    			String nhost = host ;			host = nhost.substring(0,ind+1);			if (Inet6Address.			    textToNumericFormat(host.substring(1, ind)) == null) {			    throw new IllegalArgumentException(				"Invalid host: "+ host);			}			port = -1 ;			if (nhost.length() > ind+1) {			    if (nhost.charAt(ind+1) == ':') {				++ind ;				// port can be null according to RFC2396				if (nhost.length() > (ind + 1)) {				    port = Integer.parseInt(nhost.substring(ind+1));                                    if (port<0)                                        port = -2; // -1 is reserved for "no port"				}			    } else {				throw new IllegalArgumentException(				    "Invalid authority field: " + authority);			    }			}		    } else {			throw new IllegalArgumentException(			    "Invalid authority field: " + authority);		    }		} else {		    ind = host.indexOf(':');		    port = -1;		    if (ind >= 0) {			// port can be null according to RFC2396			if (host.length() > (ind + 1)) {			    port = Integer.parseInt(host.substring(ind + 1));                            if (port<0)                                port = -2; // -1 is reserved for "no port"			}			host = host.substring(0, ind);		    }		}	    } else {		host = "";	    }	    if (port < -1 || port > 65535)		throw new IllegalArgumentException("Invalid port number :" +						   port);	    start = i;	    // If the authority is defined then the path is defined by the            // spec only; See RFC 2396 Section 5.2.4.            if (authority != null && authority.length() > 0)                path = "";	}  	if (host == null) { 	    host = ""; 	}        // Parse the file path if any	if (start < limit) {	    if (spec.charAt(start) == '/') {		path = spec.substring(start, limit);	    } else if (path != null && path.length() > 0) {		isRelPath = true;		int ind = path.lastIndexOf('/');		String seperator = "";		if (ind == -1 && authority != null)		    seperator = "/";		path = path.substring(0, ind + 1) + seperator +		         spec.substring(start, limit);						    } else {		String seperator = (authority != null) ? "/" : "";		path = seperator + spec.substring(start, limit);	    }	} else if (queryOnly && path != null) {            int ind = path.lastIndexOf('/');            if (ind < 0)                ind = 0;            path = path.substring(0, ind) + "/";        }	if (path == null)	    path = "";	if (isRelPath) {            // Remove embedded /./            while ((i = path.indexOf("/./")) >= 0) {	        path = path.substring(0, i) + path.substring(i + 2);	    }            // Remove embedded /../ if possible	    i = 0;	    while ((i = path.indexOf("/../", i)) > 0) {	        if ((limit = path.lastIndexOf('/', i - 1)) >= 0) {		    path = path.substring(0, limit) + path.substring(i + 3);		    i = 0;	        } else {		    i = i + 3;		}	    }            // Remove trailing .. if possible            while (path.endsWith("/..")) {                i = path.indexOf("/..");	        if ((limit = path.lastIndexOf('/', i - 1)) >= 0) {		    path = path.substring(0, limit+1);	        } else {		    break;		}	    }	    // Remove starting .            if (path.startsWith("./") && path.length() > 2)                path = path.substring(2);            // Remove trailing .            if (path.endsWith("/."))                path = path.substring(0, path.length() -1);	}	setURL(u, protocol, host, port, authority, userInfo, path, query, ref);    }    /**     * Returns the default port for a URL parsed by this handler. This method     * is meant to be overidden by handlers with default port numbers.     * @return the default port for a <code>URL</code> parsed by this handler.

⌨️ 快捷键说明

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