jdk/src/share/classes/java/net/InetSocketAddress.java
author juh
Tue, 30 Jul 2013 11:04:19 -0700
changeset 19069 1d9cb0d080e3
parent 18156 edb590d448c5
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
8021833: javadoc cleanup in java.net Summary: <code> and <tt> converted to {@code }; package.html to package-info.java Reviewed-by: darcy, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InvalidObjectException;
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    29
import java.io.ObjectInputStream;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    30
import java.io.ObjectOutputStream;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    31
import java.io.ObjectStreamException;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    32
import java.io.ObjectStreamField;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class implements an IP Socket Address (IP address + port number)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * It can also be a pair (hostname + port number), in which case an attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * will be made to resolve the hostname. If resolution fails then the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * is said to be <I>unresolved</I> but can still be used on some circumstances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * like connecting through a proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * It provides an immutable object used by sockets for binding, connecting, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * as returned values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The <i>wildcard</i> is a special local IP address. It usually means "any"
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
    46
 * and can only be used for {@code bind} operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see java.net.Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see java.net.ServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    52
public class InetSocketAddress
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    53
    extends SocketAddress
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    54
{
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    55
    // Private implementation class pointed to by all public methods.
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    56
    private static class InetSocketAddressHolder {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    57
        // The hostname of the Socket Address
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    58
        private String hostname;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    59
        // The IP address of the Socket Address
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    60
        private InetAddress addr;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    61
        // The port number of the Socket Address
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    62
        private int port;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    63
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    64
        private InetSocketAddressHolder(String hostname, InetAddress addr, int port) {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    65
            this.hostname = hostname;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    66
            this.addr = addr;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    67
            this.port = port;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    68
        }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    69
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    70
        private int getPort() {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    71
            return port;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    72
        }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    73
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    74
        private InetAddress getAddress() {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    75
            return addr;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    76
        }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    77
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    78
        private String getHostName() {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    79
            if (hostname != null)
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    80
                return hostname;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    81
            if (addr != null)
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    82
                return addr.getHostName();
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    83
            return null;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    84
        }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    85
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    86
        private String getHostString() {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    87
            if (hostname != null)
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    88
                return hostname;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    89
            if (addr != null) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 16089
diff changeset
    90
                if (addr.holder().getHostName() != null)
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 16089
diff changeset
    91
                    return addr.holder().getHostName();
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    92
                else
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    93
                    return addr.getHostAddress();
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    94
            }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    95
            return null;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    96
        }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    97
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    98
        private boolean isUnresolved() {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
    99
            return addr == null;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   100
        }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   101
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   102
        @Override
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   103
        public String toString() {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   104
            if (isUnresolved()) {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   105
                return hostname + ":" + port;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   106
            } else {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   107
                return addr.toString() + ":" + port;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   108
            }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   109
        }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   110
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   111
        @Override
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   112
        public final boolean equals(Object obj) {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   113
            if (obj == null || !(obj instanceof InetSocketAddressHolder))
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   114
                return false;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   115
            InetSocketAddressHolder that = (InetSocketAddressHolder)obj;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   116
            boolean sameIP;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   117
            if (addr != null)
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   118
                sameIP = addr.equals(that.addr);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   119
            else if (hostname != null)
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   120
                sameIP = (that.addr == null) &&
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   121
                    hostname.equalsIgnoreCase(that.hostname);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   122
            else
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   123
                sameIP = (that.addr == null) && (that.hostname == null);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   124
            return sameIP && (port == that.port);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   125
        }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   126
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   127
        @Override
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   128
        public final int hashCode() {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   129
            if (addr != null)
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   130
                return addr.hashCode() + port;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   131
            if (hostname != null)
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   132
                return hostname.toLowerCase().hashCode() + port;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   133
            return port;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   134
        }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   135
    }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   136
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   137
    private final transient InetSocketAddressHolder holder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private static final long serialVersionUID = 5076001401234631237L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   141
    private static int checkPort(int port) {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   142
        if (port < 0 || port > 0xFFFF)
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   143
            throw new IllegalArgumentException("port out of range:" + port);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   144
        return port;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   145
    }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   146
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   147
    private static String checkHost(String hostname) {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   148
        if (hostname == null)
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   149
            throw new IllegalArgumentException("hostname can't be null");
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   150
        return hostname;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Creates a socket address where the IP address is the wildcard address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * and the port number a specified value.
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.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   158
     * A port number of {@code zero} will let the system pick up an
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   159
     * ephemeral port in a {@code bind} operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param   port    The port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @throws IllegalArgumentException if the port parameter is outside the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * range of valid port values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public InetSocketAddress(int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this(InetAddress.anyLocalAddress(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Creates a socket address from an IP address and a port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * A valid port value is between 0 and 65535.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   174
     * A port number of {@code zero} will let the system pick up an
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   175
     * ephemeral port in a {@code bind} operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <P>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   177
     * A {@code null} address will assign the <i>wildcard</i> address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param   addr    The IP address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param   port    The port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @throws IllegalArgumentException if the port parameter is outside the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * range of valid port values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public InetSocketAddress(InetAddress addr, int port) {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   185
        holder = new InetSocketAddressHolder(
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   186
                        null,
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   187
                        addr == null ? InetAddress.anyLocalAddress() : addr,
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   188
                        checkPort(port));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Creates a socket address from a hostname and a port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * An attempt will be made to resolve the hostname into an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * If that attempt fails, the address will be flagged as <I>unresolved</I>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   198
     * If there is a security manager, its {@code checkConnect} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * is called with the host name as its argument to check the permissiom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * to resolve it. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * A valid port value is between 0 and 65535.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   203
     * A port number of {@code zero} will let the system pick up an
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   204
     * ephemeral port in a {@code bind} operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param   hostname the Host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param   port    The port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @throws IllegalArgumentException if the port parameter is outside the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * of valid port values, or if the hostname parameter is <TT>null</TT>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @throws SecurityException if a security manager is present and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *                           permission to resolve the host name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *                           denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @see     #isUnresolved()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public InetSocketAddress(String hostname, int port) {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   216
        checkHost(hostname);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   217
        InetAddress addr = null;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   218
        String host = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            addr = InetAddress.getByName(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } catch(UnknownHostException e) {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   222
            host = hostname;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   224
        holder = new InetSocketAddressHolder(host, addr, checkPort(port));
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   225
    }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   226
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   227
    // private constructor for creating unresolved instances
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   228
    private InetSocketAddress(int port, String hostname) {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   229
        holder = new InetSocketAddressHolder(hostname, null, port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Creates an unresolved socket address from a hostname and a port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * No attempt will be made to resolve the hostname into an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * The address will be flagged as <I>unresolved</I>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * A valid port value is between 0 and 65535.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   240
     * A port number of {@code zero} will let the system pick up an
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   241
     * ephemeral port in a {@code bind} operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param   host    the Host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param   port    The port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @throws IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *                  the range of valid port values, or if the hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *                  parameter is <TT>null</TT>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @see     #isUnresolved()
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   249
     * @return  a {@code InetSocketAddress} representing the unresolved
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *          socket address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public static InetSocketAddress createUnresolved(String host, int port) {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   254
        return new InetSocketAddress(checkPort(port), checkHost(host));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   257
    /**
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   258
     * @serialField hostname String
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   259
     * @serialField addr InetAddress
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   260
     * @serialField port int
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   261
     */
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   262
    private static final ObjectStreamField[] serialPersistentFields = {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   263
         new ObjectStreamField("hostname", String.class),
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   264
         new ObjectStreamField("addr", InetAddress.class),
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   265
         new ObjectStreamField("port", int.class)};
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   266
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   267
    private void writeObject(ObjectOutputStream out)
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   268
        throws IOException
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   269
    {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   270
        // Don't call defaultWriteObject()
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   271
         ObjectOutputStream.PutField pfields = out.putFields();
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   272
         pfields.put("hostname", holder.hostname);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   273
         pfields.put("addr", holder.addr);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   274
         pfields.put("port", holder.port);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   275
         out.writeFields();
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   276
     }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   277
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   278
    private void readObject(ObjectInputStream in)
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   279
        throws IOException, ClassNotFoundException
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   280
    {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   281
        // Don't call defaultReadObject()
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   282
        ObjectInputStream.GetField oisFields = in.readFields();
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   283
        final String oisHostname = (String)oisFields.get("hostname", null);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   284
        final InetAddress oisAddr = (InetAddress)oisFields.get("addr", null);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   285
        final int oisPort = oisFields.get("port", -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // Check that our invariants are satisfied
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   288
        checkPort(oisPort);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   289
        if (oisHostname == null && oisAddr == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throw new InvalidObjectException("hostname and addr " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                             "can't both be null");
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   292
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   293
        InetSocketAddressHolder h = new InetSocketAddressHolder(oisHostname,
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   294
                                                                oisAddr,
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   295
                                                                oisPort);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   296
        UNSAFE.putObject(this, FIELDS_OFFSET, h);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   297
    }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   298
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   299
    private void readObjectNoData()
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   300
        throws ObjectStreamException
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   301
    {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   302
        throw new InvalidObjectException("Stream data required");
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   303
    }
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   304
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   305
    private static final long FIELDS_OFFSET;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   306
    private static final sun.misc.Unsafe UNSAFE;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   307
    static {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   308
        try {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   309
            sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe();
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   310
            FIELDS_OFFSET = unsafe.objectFieldOffset(
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   311
                    InetSocketAddress.class.getDeclaredField("holder"));
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   312
            UNSAFE = unsafe;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   313
        } catch (ReflectiveOperationException e) {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   314
            throw new Error(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Gets the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @return the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public final int getPort() {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   324
        return holder.getPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   329
     * Gets the {@code InetAddress}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   331
     * @return the InetAdress or {@code null} if it is unresolved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public final InetAddress getAddress() {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   334
        return holder.getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   338
     * Gets the {@code hostname}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Note: This method may trigger a name service reverse lookup if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * address was created with a literal IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @return  the hostname part of the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public final String getHostName() {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   345
        return holder.getHostName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Returns the hostname, or the String form of the address if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * doesn't have a hostname (it was created using a literal).
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   351
     * This has the benefit of <b>not</b> attempting a reverse lookup.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @return the hostname, or String representation of the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public final String getHostString() {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   357
        return holder.getHostString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Checks whether the address has been resolved or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   363
     * @return {@code true} if the hostname couldn't be resolved into
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   364
     *          an {@code InetAddress}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public final boolean isUnresolved() {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   367
        return holder.isUnresolved();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Constructs a string representation of this InetSocketAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * This String is constructed by calling toString() on the InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * and concatenating the port number (with a colon). If the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * is unresolved then the part before the colon will only contain the hostname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @return  a string representation of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   378
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public String toString() {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   380
        return holder.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Compares this object against the specified object.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   385
     * The result is {@code true} if and only if the argument is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   386
     * not {@code null} and it represents the same address as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   389
     * Two instances of {@code InetSocketAddress} represent the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * address if both the InetAddresses (or hostnames if it is unresolved) and port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * numbers are equal.
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 16870
diff changeset
   392
     * If both addresses are unresolved, then the hostname and the port number
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * are compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Note: Hostnames are case insensitive. e.g. "FooBar" and "foobar" are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * considered equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param   obj   the object to compare against.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   399
     * @return  {@code true} if the objects are the same;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   400
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @see java.net.InetAddress#equals(java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   403
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public final boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (obj == null || !(obj instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return false;
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   407
        return holder.equals(((InetSocketAddress) obj).holder);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Returns a hashcode for this socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @return  a hash code value for this socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   415
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public final int hashCode() {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 5506
diff changeset
   417
        return holder.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
}