jdk/src/share/classes/java/net/InetSocketAddress.java
author xdono
Thu, 02 Oct 2008 19:58:32 -0700
changeset 1247 b4c26443dee5
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6754988: Update copyright year Summary: Update for files that have been modified starting July 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class implements an IP Socket Address (IP address + port number)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * It can also be a pair (hostname + port number), in which case an attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * will be made to resolve the hostname. If resolution fails then the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * is said to be <I>unresolved</I> but can still be used on some circumstances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * like connecting through a proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * It provides an immutable object used by sockets for binding, connecting, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * as returned values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The <i>wildcard</i> is a special local IP address. It usually means "any"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * and can only be used for <code>bind</code> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see java.net.Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see java.net.ServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class InetSocketAddress extends SocketAddress {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /* The hostname of the Socket Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private String hostname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /* The IP address of the Socket Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private InetAddress addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /* The port number of the Socket Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final long serialVersionUID = 5076001401234631237L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private InetSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Creates a socket address where the IP address is the wildcard address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * and the port number a specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * A valid port value is between 0 and 65535.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * A port number of <code>zero</code> will let the system pick up an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * ephemeral port in a <code>bind</code> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param   port    The port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @throws IllegalArgumentException if the port parameter is outside the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * range of valid port values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public InetSocketAddress(int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this(InetAddress.anyLocalAddress(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Creates a socket address from an IP address and a port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * A valid port value is between 0 and 65535.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * A port number of <code>zero</code> will let the system pick up an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * ephemeral port in a <code>bind</code> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * A <code>null</code> address will assign the <i>wildcard</i> address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param   addr    The IP address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param   port    The port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @throws IllegalArgumentException if the port parameter is outside the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * range of valid port values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public InetSocketAddress(InetAddress addr, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (port < 0 || port > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new IllegalArgumentException("port out of range:" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (addr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            this.addr = InetAddress.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            this.addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Creates a socket address from a hostname and a port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * An attempt will be made to resolve the hostname into an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * If that attempt fails, the address will be flagged as <I>unresolved</I>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * If there is a security manager, its <code>checkConnect</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * is called with the host name as its argument to check the permissiom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * to resolve it. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * A valid port value is between 0 and 65535.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * A port number of <code>zero</code> will let the system pick up an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * ephemeral port in a <code>bind</code> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param   hostname the Host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param   port    The port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @throws IllegalArgumentException if the port parameter is outside the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * of valid port values, or if the hostname parameter is <TT>null</TT>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws SecurityException if a security manager is present and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *                           permission to resolve the host name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *                           denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @see     #isUnresolved()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public InetSocketAddress(String hostname, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (port < 0 || port > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new IllegalArgumentException("port out of range:" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (hostname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new IllegalArgumentException("hostname can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            addr = InetAddress.getByName(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } catch(UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            this.hostname = hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Creates an unresolved socket address from a hostname and a port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * No attempt will be made to resolve the hostname into an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * The address will be flagged as <I>unresolved</I>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * A valid port value is between 0 and 65535.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * A port number of <code>zero</code> will let the system pick up an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * ephemeral port in a <code>bind</code> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param   host    the Host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param   port    The port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @throws IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *                  the range of valid port values, or if the hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *                  parameter is <TT>null</TT>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see     #isUnresolved()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return  a <code>InetSocketAddress</code> representing the unresolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *          socket address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public static InetSocketAddress createUnresolved(String host, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (port < 0 || port > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new IllegalArgumentException("port out of range:" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new IllegalArgumentException("hostname can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        InetSocketAddress s = new InetSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        s.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        s.hostname = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        s.addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // Check that our invariants are satisfied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (port < 0 || port > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new InvalidObjectException("port out of range:" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (hostname == null && addr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new InvalidObjectException("hostname and addr " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                             "can't both be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Gets the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @return the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public final int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Gets the <code>InetAddress</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return the InetAdress or <code>null</code> if it is unresolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public final InetAddress getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Gets the <code>hostname</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Note: This method may trigger a name service reverse lookup if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * address was created with a literal IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @return  the hostname part of the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public final String getHostName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (hostname != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (addr != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return addr.getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Returns the hostname, or the String form of the address if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * doesn't have a hostname (it was created using a literal).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * This has the benefit of <b>not</b> attemptimg a reverse lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return the hostname, or String representation of the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public final String getHostString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (hostname != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (addr.hostName != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                return addr.hostName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                return addr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Checks whether the address has been resolved or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @return <code>true</code> if the hostname couldn't be resolved into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *          an <code>InetAddress</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public final boolean isUnresolved() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return addr == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Constructs a string representation of this InetSocketAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * This String is constructed by calling toString() on the InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * and concatenating the port number (with a colon). If the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * is unresolved then the part before the colon will only contain the hostname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @return  a string representation of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (isUnresolved()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return hostname + ":" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return addr.toString() + ":" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Compares this object against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * The result is <code>true</code> if and only if the argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * not <code>null</code> and it represents the same address as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Two instances of <code>InetSocketAddress</code> represent the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * address if both the InetAddresses (or hostnames if it is unresolved) and port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * numbers are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * If both addresses are unresolved, then the hostname & the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * are compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Note: Hostnames are case insensitive. e.g. "FooBar" and "foobar" are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * considered equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param   obj   the object to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return  <code>true</code> if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @see java.net.InetAddress#equals(java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public final boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (obj == null || !(obj instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        InetSocketAddress sockAddr = (InetSocketAddress) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        boolean sameIP = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (this.addr != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            sameIP = this.addr.equals(sockAddr.addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        else if (this.hostname != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            sameIP = (sockAddr.addr == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                this.hostname.equalsIgnoreCase(sockAddr.hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            sameIP = (sockAddr.addr == null) && (sockAddr.hostname == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return sameIP && (this.port == sockAddr.port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns a hashcode for this socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return  a hash code value for this socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public final int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (addr != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return addr.hashCode() + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (hostname != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return hostname.toLowerCase().hashCode() + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
}