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

📄 urlname.java

📁 java Email you can use it to send email to others
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License").  You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code.  If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license."  If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above.  However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. *//* * @(#)URLName.java	1.19 07/05/04 */package javax.mail;import java.net.*;import java.io.ByteArrayOutputStream;import java.io.OutputStreamWriter;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.BitSet;import java.util.Locale;/** * The name of a URL. This class represents a URL name and also  * provides the basic parsing functionality to parse most internet  * standard URL schemes. <p> * * Note that this class differs from <code>java.net.URL</code>  * in that this class just represents the name of a URL, it does  * not model the connection to a URL. * * @version	1.19, 07/05/04 * @author	Christopher Cotton * @author	Bill Shannon */public class URLName {    /**     * The full version of the URL     */    protected String fullURL;    /**      * The protocol to use (ftp, http, nntp, imap, pop3 ... etc.) .      */    private String protocol;    /**      * The username to use when connecting     */    private String username;    /**      * The password to use when connecting.     */    private String password;    /**      * The host name to which to connect.      */    private String host;    /**     * The host's IP address, used in equals and hashCode.     * Computed on demand.     */    private InetAddress hostAddress;    private boolean hostAddressKnown = false;    /**      * The protocol port to connect to.      */    private int port = -1;    /**      * The specified file name on that host.      */    private String file;    /**      * # reference.      */    private String ref;    /**     * Our hash code.     */    private int hashCode = 0;    /**     * A way to turn off encoding, just in case...     */    private static boolean doEncode = true;    static {	try {	    doEncode = !Boolean.getBoolean("mail.URLName.dontencode");	} catch (Exception ex) {	    // ignore any errors	}    }    /**     * Creates a URLName object from the specified protocol,     * host, port number, file, username, and password. Specifying a port     * number of -1 indicates that the URL should use the default port for     * the protocol.     */    public URLName(	String protocol,	String host,	int port,	String file,	String username,	String password	)    {	this.protocol = protocol;	this.host = host;	this.port = port;	int refStart;	if (file != null && (refStart = file.indexOf('#')) != -1) {	    this.file = file.substring(0, refStart);	    this.ref = file.substring(refStart + 1);	} else {	    this.file = file;	    this.ref = null;	}	this.username = doEncode ? encode(username) : username;	this.password = doEncode ? encode(password) : password;    }    /**     * Construct a URLName from a java.net.URL object.     */    public URLName(URL url) {	this(url.toString());    }    /**     * Construct a URLName from the string.  Parses out all the possible     * information (protocol, host, port, file, username, password).     */    public URLName(String url) {	parseString(url);    }    /**     * Constructs a string representation of this URLName.     */    public String toString() {	if (fullURL == null) {	    // add the "protocol:"	    StringBuffer tempURL = new StringBuffer();	    if (protocol != null) {		tempURL.append(protocol);		tempURL.append(":");	    }	    if (username != null || host != null) {		// add the "//"		tempURL.append("//");				// add the user:password@		// XXX - can you just have a password? without a username?		if (username != null) {		    tempURL.append(username);				    if (password != null){			tempURL.append(":");			tempURL.append(password);		    }				    tempURL.append("@");		}	    		// add host		if (host != null) {		    tempURL.append(host);		}	    		// add port (if needed)		if (port != -1) {		    tempURL.append(":");		    tempURL.append(Integer.toString(port));		}		if (file != null)		    tempURL.append("/");	    }	    	    // add the file	    if (file != null) {		tempURL.append(file);	    }	    	    // add the ref	    if (ref != null) {		tempURL.append("#");		tempURL.append(ref);	    }	    // create the fullURL now	    fullURL = tempURL.toString();	}	return fullURL;    }    /**     * Method which does all of the work of parsing the string.     */    protected void parseString(String url) {	// initialize everything in case called from subclass	// (URLName really should be a final class)	protocol = file = ref = host = username = password = null;	port = -1;	int len = url.length();	// find the protocol	// XXX - should check for only legal characters before the colon	// (legal: a-z, A-Z, 0-9, "+", ".", "-")	int protocolEnd = url.indexOf(':');        if (protocolEnd != -1)	    protocol = url.substring(0, protocolEnd);	// is this an Internet standard URL that contains a host name?	if (url.regionMatches(protocolEnd + 1, "//", 0, 2)) {	    // find where the file starts	    String fullhost = null;	    int fileStart = url.indexOf('/', protocolEnd + 3);	    if (fileStart != -1) {		fullhost = url.substring(protocolEnd + 3, fileStart);		if (fileStart + 1 < len)		    file = url.substring(fileStart + 1);		else		    file = "";	    } else		fullhost = url.substring(protocolEnd + 3);	    // examine the fullhost, for username password etc.	    int i = fullhost.indexOf('@');	    if (i != -1) {		String fulluserpass = fullhost.substring(0, i);		fullhost = fullhost.substring(i + 1);		// get user and password		int passindex = fulluserpass.indexOf(':');		if (passindex != -1) {		    username = fulluserpass.substring(0, passindex);		    password = fulluserpass.substring(passindex + 1);		} else {		    username = fulluserpass;		}	    }	    	    // get the port (if there)	    int portindex;	    if (fullhost.length() > 0 && fullhost.charAt(0) == '[') {		// an IPv6 address?		portindex = fullhost.indexOf(':', fullhost.indexOf(']'));	    } else {		portindex = fullhost.indexOf(':');	    }	    if (portindex != -1) {		String portstring = fullhost.substring(portindex + 1);		if (portstring.length() > 0) {		    try {			port = Integer.parseInt(portstring);		    } catch (NumberFormatException nfex) {			port = -1;		    }		}				host = fullhost.substring(0, portindex);	    } else {		host = fullhost;	    }	} else {	    if (protocolEnd + 1 < len)		file = url.substring(protocolEnd + 1);	}	// extract the reference from the file name, if any	int refStart;	if (file != null && (refStart = file.indexOf('#')) != -1) {	    ref = file.substring(refStart + 1);	    file = file.substring(0, refStart);	}    }        /**     * Returns the port number of this URLName.     * Returns -1 if the port is not set.      */    public int getPort() {	return port;    }    /**     * Returns the protocol of this URLName.     * Returns null if this URLName has no protocol.     */    public String getProtocol() {	return protocol;    }    /**     * Returns the file name of this URLName.     * Returns null if this URLName has no file name.     */    public String getFile() {	return file;    }    /**     * Returns the reference of this URLName.     * Returns null if this URLName has no reference.     */    public String getRef() {	return ref;    }    /**     * Returns the host of this URLName.     * Returns null if this URLName has no host.     */    public String getHost() {	return host;    }    /**     * Returns the user name of this URLName.     * Returns null if this URLName has no user name.     */    public String getUsername() {	return doEncode ? decode(username) : username;    }    /**     * Returns the password of this URLName.     * Returns null if this URLName has no password.     */    public String getPassword() {	return doEncode ? decode(password) : password;    }    /**     * Constructs a URL from the URLName.     */    public URL getURL() throws MalformedURLException {        return new URL(getProtocol(), getHost(), getPort(), getFile());    }    /**

⌨️ 快捷键说明

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