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

📄 urlname.java

📁 java Email you can use it to send email to others
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * Compares two URLNames. The result is true if and only if the     * argument is not null and is a URLName object that represents the     * same URLName as this object. Two URLName objects are equal if     * they have the same protocol and the same host,     * the same port number on the host, the same username,     * and the same file on the host. The fields (host, username,     * file) are also considered the same if they are both     * null.  <p>     *     * Hosts are considered equal if the names are equal (case independent)     * or if host name lookups for them both succeed and they both reference     * the same IP address. <p>     *     * Note that URLName has no knowledge of default port numbers for     * particular protocols, so "imap://host" and "imap://host:143"     * would not compare as equal. <p>     *     * Note also that the password field is not included in the comparison,     * nor is any reference field appended to the filename.     */    public boolean equals(Object obj) {        if (!(obj instanceof URLName))	    return false;	URLName u2 = (URLName)obj;	// compare protocols	if (u2.protocol == null || !u2.protocol.equals(protocol))	    return false;	// compare hosts	InetAddress a1 = getHostAddress(), a2 = u2.getHostAddress();	// if we have internet address for both, and they're not the same, fail	if (a1 != null && a2 != null) {	    if (!a1.equals(a2))		return false;	// else, if we have host names for both, and they're not the same, fail	} else if (host != null && u2.host != null) {	    if (!host.equalsIgnoreCase(u2.host))		return false;	// else, if not both null	} else if (host != u2.host) {	    return false;	}	// at this point, hosts match	// compare usernames	if (!(username == u2.username ||		(username != null && username.equals(u2.username))))	    return false;	// Forget about password since it doesn't	// really denote a different store.	// compare files	String f1 = file == null ? "" : file;	String f2 = u2.file == null ? "" : u2.file;	if (!f1.equals(f2))	    return false;	// compare ports	if (port != u2.port)	    return false;	// all comparisons succeeded, they're equal        return true;    }    /**     * Compute the hash code for this URLName.     */    public int hashCode() {	if (hashCode != 0)	    return hashCode;	if (protocol != null)	    hashCode += protocol.hashCode();	InetAddress addr = getHostAddress();	if (addr != null)	    hashCode += addr.hashCode();	else if (host != null)	    hashCode += host.toLowerCase(Locale.ENGLISH).hashCode();	if (username != null)	    hashCode += username.hashCode();	if (file != null)	    hashCode += file.hashCode();	hashCode += port;	return hashCode;    }    /**     * Get the IP address of our host.  Look up the     * name the first time and remember that we've done     * so, whether the lookup fails or not.     */    private synchronized InetAddress getHostAddress() {	if (hostAddressKnown)	    return hostAddress;	if (host == null)	    return null;	try {	    hostAddress = InetAddress.getByName(host);	} catch (UnknownHostException ex) {	    hostAddress = null;	}	hostAddressKnown = true;	return hostAddress;    }    /**     * The class contains a utility method for converting a     * <code>String</code> into a MIME format called     * "<code>x-www-form-urlencoded</code>" format.     * <p>     * To convert a <code>String</code>, each character is examined in turn:     * <ul>     * <li>The ASCII characters '<code>a</code>' through '<code>z</code>',     *     '<code>A</code>' through '<code>Z</code>', '<code>0</code>'     *     through '<code>9</code>', and &quot;.&quot;, &quot;-&quot;,      * &quot;*&quot;, &quot;_&quot; remain the same.     * <li>The space character '<code>&nbsp;</code>' is converted into a     *     plus sign '<code>+</code>'.     * <li>All other characters are converted into the 3-character string     *     "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit     *     hexadecimal representation of the lower 8-bits of the character.     * </ul>     *     * @author  Herb Jellinek     * @version 1.16, 10/23/99     * @since   JDK1.0     */    static BitSet dontNeedEncoding;    static final int caseDiff = ('a' - 'A');    /* The list of characters that are not encoded have been determined by       referencing O'Reilly's "HTML: The Definitive Guide" (page 164). */    static {	dontNeedEncoding = new BitSet(256);	int i;	for (i = 'a'; i <= 'z'; i++) {	    dontNeedEncoding.set(i);	}	for (i = 'A'; i <= 'Z'; i++) {	    dontNeedEncoding.set(i);	}	for (i = '0'; i <= '9'; i++) {	    dontNeedEncoding.set(i);	}	/* encoding a space to a + is done in the encode() method */	dontNeedEncoding.set(' ');	dontNeedEncoding.set('-');	dontNeedEncoding.set('_');	dontNeedEncoding.set('.');	dontNeedEncoding.set('*');    }    /**     * Translates a string into <code>x-www-form-urlencoded</code> format.     *     * @param   s   <code>String</code> to be translated.     * @return  the translated <code>String</code>.     */    static String encode(String s) {	if (s == null)	    return null;	// the common case is no encoding is needed	for (int i = 0; i < s.length(); i++) {	    int c = (int)s.charAt(i);	    if (c == ' ' || !dontNeedEncoding.get(c))		return _encode(s);	}	return s;    }    private static String _encode(String s) {	int maxBytesPerChar = 10;        StringBuffer out = new StringBuffer(s.length());	ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);	OutputStreamWriter writer = new OutputStreamWriter(buf);	for (int i = 0; i < s.length(); i++) {	    int c = (int)s.charAt(i);	    if (dontNeedEncoding.get(c)) {		if (c == ' ') {		    c = '+';		}		out.append((char)c);	    } else {		// convert to external encoding before hex conversion		try {		    writer.write(c);		    writer.flush();		} catch(IOException e) {		    buf.reset();		    continue;		}		byte[] ba = buf.toByteArray();		for (int j = 0; j < ba.length; j++) {		    out.append('%');		    char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);		    // converting to use uppercase letter as part of		    // the hex value if ch is a letter.		    if (Character.isLetter(ch)) {			ch -= caseDiff;		    }		    out.append(ch);		    ch = Character.forDigit(ba[j] & 0xF, 16);		    if (Character.isLetter(ch)) {			ch -= caseDiff;		    }		    out.append(ch);		}		buf.reset();	    }	}	return out.toString();    }    /**     * The class contains a utility method for converting from     * a MIME format called "<code>x-www-form-urlencoded</code>"     * to a <code>String</code>     * <p>     * To convert to a <code>String</code>, each character is examined in turn:     * <ul>     * <li>The ASCII characters '<code>a</code>' through '<code>z</code>',     * '<code>A</code>' through '<code>Z</code>', and '<code>0</code>'     * through '<code>9</code>' remain the same.     * <li>The plus sign '<code>+</code>'is converted into a     * space character '<code>&nbsp;</code>'.     * <li>The remaining characters are represented by 3-character     * strings which begin with the percent sign,     * "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit     * hexadecimal representation of the lower 8-bits of the character.     * </ul>     *     * @author  Mark Chamness     * @author  Michael McCloskey     * @version 1.7, 10/22/99     * @since   1.2     */    /**     * Decodes a &quot;x-www-form-urlencoded&quot;      * to a <tt>String</tt>.     * @param s the <code>String</code> to decode     * @return the newly decoded <code>String</code>     */    static String decode(String s) {	if (s == null)	    return null;	if (indexOfAny(s, "+%") == -1)	    return s;		// the common case        StringBuffer sb = new StringBuffer();        for (int i = 0; i < s.length(); i++) {            char c = s.charAt(i);            switch (c) {                case '+':                    sb.append(' ');                    break;                case '%':                    try {                        sb.append((char)Integer.parseInt(                                        s.substring(i+1,i+3),16));                    } catch (NumberFormatException e) {                        throw new IllegalArgumentException();                    }                    i += 2;                    break;                default:                    sb.append(c);                    break;            }        }        // Undo conversion to external encoding        String result = sb.toString();        try {            byte[] inputBytes = result.getBytes("8859_1");            result = new String(inputBytes);        } catch (UnsupportedEncodingException e) {            // The system should always have 8859_1        }        return result;    }    /**     * Return the first index of any of the characters in "any" in "s",     * or -1 if none are found.     *     * This should be a method on String.     */    private static int indexOfAny(String s, String any) {	return indexOfAny(s, any, 0);    }    private static int indexOfAny(String s, String any, int start) {	try {	    int len = s.length();	    for (int i = start; i < len; i++) {		if (any.indexOf(s.charAt(i)) >= 0)		    return i;	    }	    return -1;	} catch (StringIndexOutOfBoundsException e) {	    return -1;	}    }    /*    // Do not remove, this is needed when testing new URL cases    public static void main(String[] argv) {	String [] testURLNames = {	    "protocol://userid:password@host:119/file",	    "http://funny/folder/file.html",	    "http://funny/folder/file.html#ref",	    "http://funny/folder/file.html#",	    "http://funny/#ref",	    "imap://jmr:secret@labyrinth//var/mail/jmr",	    "nntp://fred@labyrinth:143/save/it/now.mbox",	    "imap://jmr@labyrinth/INBOX",	    "imap://labryrinth",	    "imap://labryrinth/",	    "file:",	    "file:INBOX",	    "file:/home/shannon/mail/foo",	    "/tmp/foo",	    "//host/tmp/foo",	    ":/tmp/foo",	    "/really/weird:/tmp/foo#bar",	    ""	};	URLName url =	    new URLName("protocol", "host", 119, "file", "userid", "password");	System.out.println("Test URL: " + url.toString());	if (argv.length == 0) {	    for (int i = 0; i < testURLNames.length; i++) {		print(testURLNames[i]);		System.out.println();	    }	} else {	    for (int i = 0; i < argv.length; i++) {		print(argv[i]);		System.out.println();	    }	    if (argv.length == 2) {		URLName u1 = new URLName(argv[0]);		URLName u2 = new URLName(argv[1]);		System.out.println("URL1 hash code: " + u1.hashCode());		System.out.println("URL2 hash code: " + u2.hashCode());		if (u1.equals(u2))		    System.out.println("success, equal");		else		    System.out.println("fail, not equal");		if (u2.equals(u1))		    System.out.println("success, equal");		else		    System.out.println("fail, not equal");		if (u1.hashCode() == u2.hashCode())		    System.out.println("success, hashCodes equal");		else		    System.out.println("fail, hashCodes not equal");	    }	}    }    private static void print(String name) {	URLName url = new URLName(name);	System.out.println("Original URL: " + name);	System.out.println("The fullUrl : " + url.toString());	if (!name.equals(url.toString()))	    System.out.println("            : NOT EQUAL!");	System.out.println("The protocol is: " + url.getProtocol());	System.out.println("The host is: " + url.getHost());	System.out.println("The port is: " + url.getPort());	System.out.println("The user is: " + url.getUsername());	System.out.println("The password is: " + url.getPassword());	System.out.println("The file is: " + url.getFile());	System.out.println("The ref is: " + url.getRef());    }    */}

⌨️ 快捷键说明

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