jdk/src/share/classes/java/net/InetSocketAddress.java
changeset 19069 1d9cb0d080e3
parent 18156 edb590d448c5
child 21278 ef8a3a2a72f2
equal deleted inserted replaced
19068:f2358d18923a 19069:1d9cb0d080e3
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    41  * <p>
    41  * <p>
    42  * It provides an immutable object used by sockets for binding, connecting, or
    42  * It provides an immutable object used by sockets for binding, connecting, or
    43  * as returned values.
    43  * as returned values.
    44  * <p>
    44  * <p>
    45  * The <i>wildcard</i> is a special local IP address. It usually means "any"
    45  * The <i>wildcard</i> is a special local IP address. It usually means "any"
    46  * and can only be used for <code>bind</code> operations.
    46  * and can only be used for {@code bind} operations.
    47  *
    47  *
    48  * @see java.net.Socket
    48  * @see java.net.Socket
    49  * @see java.net.ServerSocket
    49  * @see java.net.ServerSocket
    50  * @since 1.4
    50  * @since 1.4
    51  */
    51  */
   153     /**
   153     /**
   154      * Creates a socket address where the IP address is the wildcard address
   154      * Creates a socket address where the IP address is the wildcard address
   155      * and the port number a specified value.
   155      * and the port number a specified value.
   156      * <p>
   156      * <p>
   157      * A valid port value is between 0 and 65535.
   157      * A valid port value is between 0 and 65535.
   158      * A port number of <code>zero</code> will let the system pick up an
   158      * A port number of {@code zero} will let the system pick up an
   159      * ephemeral port in a <code>bind</code> operation.
   159      * ephemeral port in a {@code bind} operation.
   160      * <p>
   160      * <p>
   161      * @param   port    The port number
   161      * @param   port    The port number
   162      * @throws IllegalArgumentException if the port parameter is outside the specified
   162      * @throws IllegalArgumentException if the port parameter is outside the specified
   163      * range of valid port values.
   163      * range of valid port values.
   164      */
   164      */
   169     /**
   169     /**
   170      *
   170      *
   171      * Creates a socket address from an IP address and a port number.
   171      * Creates a socket address from an IP address and a port number.
   172      * <p>
   172      * <p>
   173      * A valid port value is between 0 and 65535.
   173      * A valid port value is between 0 and 65535.
   174      * A port number of <code>zero</code> will let the system pick up an
   174      * A port number of {@code zero} will let the system pick up an
   175      * ephemeral port in a <code>bind</code> operation.
   175      * ephemeral port in a {@code bind} operation.
   176      * <P>
   176      * <P>
   177      * A <code>null</code> address will assign the <i>wildcard</i> address.
   177      * A {@code null} address will assign the <i>wildcard</i> address.
   178      * <p>
   178      * <p>
   179      * @param   addr    The IP address
   179      * @param   addr    The IP address
   180      * @param   port    The port number
   180      * @param   port    The port number
   181      * @throws IllegalArgumentException if the port parameter is outside the specified
   181      * @throws IllegalArgumentException if the port parameter is outside the specified
   182      * range of valid port values.
   182      * range of valid port values.
   193      * Creates a socket address from a hostname and a port number.
   193      * Creates a socket address from a hostname and a port number.
   194      * <p>
   194      * <p>
   195      * An attempt will be made to resolve the hostname into an InetAddress.
   195      * An attempt will be made to resolve the hostname into an InetAddress.
   196      * If that attempt fails, the address will be flagged as <I>unresolved</I>.
   196      * If that attempt fails, the address will be flagged as <I>unresolved</I>.
   197      * <p>
   197      * <p>
   198      * If there is a security manager, its <code>checkConnect</code> method
   198      * If there is a security manager, its {@code checkConnect} method
   199      * is called with the host name as its argument to check the permissiom
   199      * is called with the host name as its argument to check the permissiom
   200      * to resolve it. This could result in a SecurityException.
   200      * to resolve it. This could result in a SecurityException.
   201      * <P>
   201      * <P>
   202      * A valid port value is between 0 and 65535.
   202      * A valid port value is between 0 and 65535.
   203      * A port number of <code>zero</code> will let the system pick up an
   203      * A port number of {@code zero} will let the system pick up an
   204      * ephemeral port in a <code>bind</code> operation.
   204      * ephemeral port in a {@code bind} operation.
   205      * <P>
   205      * <P>
   206      * @param   hostname the Host name
   206      * @param   hostname the Host name
   207      * @param   port    The port number
   207      * @param   port    The port number
   208      * @throws IllegalArgumentException if the port parameter is outside the range
   208      * @throws IllegalArgumentException if the port parameter is outside the range
   209      * of valid port values, or if the hostname parameter is <TT>null</TT>.
   209      * of valid port values, or if the hostname parameter is <TT>null</TT>.
   235      * <p>
   235      * <p>
   236      * No attempt will be made to resolve the hostname into an InetAddress.
   236      * No attempt will be made to resolve the hostname into an InetAddress.
   237      * The address will be flagged as <I>unresolved</I>.
   237      * The address will be flagged as <I>unresolved</I>.
   238      * <p>
   238      * <p>
   239      * A valid port value is between 0 and 65535.
   239      * A valid port value is between 0 and 65535.
   240      * A port number of <code>zero</code> will let the system pick up an
   240      * A port number of {@code zero} will let the system pick up an
   241      * ephemeral port in a <code>bind</code> operation.
   241      * ephemeral port in a {@code bind} operation.
   242      * <P>
   242      * <P>
   243      * @param   host    the Host name
   243      * @param   host    the Host name
   244      * @param   port    The port number
   244      * @param   port    The port number
   245      * @throws IllegalArgumentException if the port parameter is outside
   245      * @throws IllegalArgumentException if the port parameter is outside
   246      *                  the range of valid port values, or if the hostname
   246      *                  the range of valid port values, or if the hostname
   247      *                  parameter is <TT>null</TT>.
   247      *                  parameter is <TT>null</TT>.
   248      * @see     #isUnresolved()
   248      * @see     #isUnresolved()
   249      * @return  a <code>InetSocketAddress</code> representing the unresolved
   249      * @return  a {@code InetSocketAddress} representing the unresolved
   250      *          socket address
   250      *          socket address
   251      * @since 1.5
   251      * @since 1.5
   252      */
   252      */
   253     public static InetSocketAddress createUnresolved(String host, int port) {
   253     public static InetSocketAddress createUnresolved(String host, int port) {
   254         return new InetSocketAddress(checkPort(port), checkHost(host));
   254         return new InetSocketAddress(checkPort(port), checkHost(host));
   324         return holder.getPort();
   324         return holder.getPort();
   325     }
   325     }
   326 
   326 
   327     /**
   327     /**
   328      *
   328      *
   329      * Gets the <code>InetAddress</code>.
   329      * Gets the {@code InetAddress}.
   330      *
   330      *
   331      * @return the InetAdress or <code>null</code> if it is unresolved.
   331      * @return the InetAdress or {@code null} if it is unresolved.
   332      */
   332      */
   333     public final InetAddress getAddress() {
   333     public final InetAddress getAddress() {
   334         return holder.getAddress();
   334         return holder.getAddress();
   335     }
   335     }
   336 
   336 
   337     /**
   337     /**
   338      * Gets the <code>hostname</code>.
   338      * Gets the {@code hostname}.
   339      * Note: This method may trigger a name service reverse lookup if the
   339      * Note: This method may trigger a name service reverse lookup if the
   340      * address was created with a literal IP address.
   340      * address was created with a literal IP address.
   341      *
   341      *
   342      * @return  the hostname part of the address.
   342      * @return  the hostname part of the address.
   343      */
   343      */
   358     }
   358     }
   359 
   359 
   360     /**
   360     /**
   361      * Checks whether the address has been resolved or not.
   361      * Checks whether the address has been resolved or not.
   362      *
   362      *
   363      * @return <code>true</code> if the hostname couldn't be resolved into
   363      * @return {@code true} if the hostname couldn't be resolved into
   364      *          an <code>InetAddress</code>.
   364      *          an {@code InetAddress}.
   365      */
   365      */
   366     public final boolean isUnresolved() {
   366     public final boolean isUnresolved() {
   367         return holder.isUnresolved();
   367         return holder.isUnresolved();
   368     }
   368     }
   369 
   369 
   380         return holder.toString();
   380         return holder.toString();
   381     }
   381     }
   382 
   382 
   383     /**
   383     /**
   384      * Compares this object against the specified object.
   384      * Compares this object against the specified object.
   385      * The result is <code>true</code> if and only if the argument is
   385      * The result is {@code true} if and only if the argument is
   386      * not <code>null</code> and it represents the same address as
   386      * not {@code null} and it represents the same address as
   387      * this object.
   387      * this object.
   388      * <p>
   388      * <p>
   389      * Two instances of <code>InetSocketAddress</code> represent the same
   389      * Two instances of {@code InetSocketAddress} represent the same
   390      * address if both the InetAddresses (or hostnames if it is unresolved) and port
   390      * address if both the InetAddresses (or hostnames if it is unresolved) and port
   391      * numbers are equal.
   391      * numbers are equal.
   392      * If both addresses are unresolved, then the hostname and the port number
   392      * If both addresses are unresolved, then the hostname and the port number
   393      * are compared.
   393      * are compared.
   394      *
   394      *
   395      * Note: Hostnames are case insensitive. e.g. "FooBar" and "foobar" are
   395      * Note: Hostnames are case insensitive. e.g. "FooBar" and "foobar" are
   396      * considered equal.
   396      * considered equal.
   397      *
   397      *
   398      * @param   obj   the object to compare against.
   398      * @param   obj   the object to compare against.
   399      * @return  <code>true</code> if the objects are the same;
   399      * @return  {@code true} if the objects are the same;
   400      *          <code>false</code> otherwise.
   400      *          {@code false} otherwise.
   401      * @see java.net.InetAddress#equals(java.lang.Object)
   401      * @see java.net.InetAddress#equals(java.lang.Object)
   402      */
   402      */
   403     @Override
   403     @Override
   404     public final boolean equals(Object obj) {
   404     public final boolean equals(Object obj) {
   405         if (obj == null || !(obj instanceof InetSocketAddress))
   405         if (obj == null || !(obj instanceof InetSocketAddress))