< prev index next >

src/java.base/share/classes/java/net/InetSocketAddress.java

Print this page




  84         }
  85 
  86         private String getHostString() {
  87             if (hostname != null)
  88                 return hostname;
  89             if (addr != null) {
  90                 if (addr.holder().getHostName() != null)
  91                     return addr.holder().getHostName();
  92                 else
  93                     return addr.getHostAddress();
  94             }
  95             return null;
  96         }
  97 
  98         private boolean isUnresolved() {
  99             return addr == null;
 100         }
 101 
 102         @Override
 103         public String toString() {



 104             if (isUnresolved()) {
 105                 return hostname + ":" + port;
 106             } else {
 107                 return addr.toString() + ":" + port;





 108             }

 109         }
 110 
 111         @Override
 112         public final boolean equals(Object obj) {
 113             if (obj == null || !(obj instanceof InetSocketAddressHolder))
 114                 return false;
 115             InetSocketAddressHolder that = (InetSocketAddressHolder)obj;
 116             boolean sameIP;
 117             if (addr != null)
 118                 sameIP = addr.equals(that.addr);
 119             else if (hostname != null)
 120                 sameIP = (that.addr == null) &&
 121                     hostname.equalsIgnoreCase(that.hostname);
 122             else
 123                 sameIP = (that.addr == null) && (that.hostname == null);
 124             return sameIP && (port == that.port);
 125         }
 126 
 127         @Override
 128         public final int hashCode() {


 350      * @since 1.7
 351      */
 352     public final String getHostString() {
 353         return holder.getHostString();
 354     }
 355 
 356     /**
 357      * Checks whether the address has been resolved or not.
 358      *
 359      * @return {@code true} if the hostname couldn't be resolved into
 360      *          an {@code InetAddress}.
 361      */
 362     public final boolean isUnresolved() {
 363         return holder.isUnresolved();
 364     }
 365 
 366     /**
 367      * Constructs a string representation of this InetSocketAddress.
 368      * This String is constructed by calling toString() on the InetAddress
 369      * and concatenating the port number (with a colon). If the address
 370      * is unresolved then the part before the colon will only contain the hostname.


 371      *
 372      * @return  a string representation of this object.
 373      */
 374     @Override
 375     public String toString() {
 376         return holder.toString();
 377     }
 378 
 379     /**
 380      * Compares this object against the specified object.
 381      * The result is {@code true} if and only if the argument is
 382      * not {@code null} and it represents the same address as
 383      * this object.
 384      * <p>
 385      * Two instances of {@code InetSocketAddress} represent the same
 386      * address if both the InetAddresses (or hostnames if it is unresolved) and port
 387      * numbers are equal.
 388      * If both addresses are unresolved, then the hostname and the port number
 389      * are compared.
 390      *




  84         }
  85 
  86         private String getHostString() {
  87             if (hostname != null)
  88                 return hostname;
  89             if (addr != null) {
  90                 if (addr.holder().getHostName() != null)
  91                     return addr.holder().getHostName();
  92                 else
  93                     return addr.getHostAddress();
  94             }
  95             return null;
  96         }
  97 
  98         private boolean isUnresolved() {
  99             return addr == null;
 100         }
 101 
 102         @Override
 103         public String toString() {
 104 
 105             String formatted;
 106 
 107             if (isUnresolved()) {
 108                 formatted = hostname + "/<unresolved>";
 109             } else {
 110                 formatted = addr.toString();
 111                 if (addr instanceof Inet6Address) {
 112                     int i = formatted.lastIndexOf("/");
 113                     formatted = formatted.substring(0, i + 1)
 114                             + "[" + formatted.substring(i + 1) + "]";
 115                 }
 116             }
 117             return formatted + ":" + port;
 118         }
 119 
 120         @Override
 121         public final boolean equals(Object obj) {
 122             if (obj == null || !(obj instanceof InetSocketAddressHolder))
 123                 return false;
 124             InetSocketAddressHolder that = (InetSocketAddressHolder)obj;
 125             boolean sameIP;
 126             if (addr != null)
 127                 sameIP = addr.equals(that.addr);
 128             else if (hostname != null)
 129                 sameIP = (that.addr == null) &&
 130                     hostname.equalsIgnoreCase(that.hostname);
 131             else
 132                 sameIP = (that.addr == null) && (that.hostname == null);
 133             return sameIP && (port == that.port);
 134         }
 135 
 136         @Override
 137         public final int hashCode() {


 359      * @since 1.7
 360      */
 361     public final String getHostString() {
 362         return holder.getHostString();
 363     }
 364 
 365     /**
 366      * Checks whether the address has been resolved or not.
 367      *
 368      * @return {@code true} if the hostname couldn't be resolved into
 369      *          an {@code InetAddress}.
 370      */
 371     public final boolean isUnresolved() {
 372         return holder.isUnresolved();
 373     }
 374 
 375     /**
 376      * Constructs a string representation of this InetSocketAddress.
 377      * This String is constructed by calling toString() on the InetAddress
 378      * and concatenating the port number (with a colon). If the address
 379      * is an IPv6 address, the IPv6 literal is enclosed in square brackets.
 380      * If the address is {@linkplain #isUnresolved() unresolved},
 381      * {@code <unresolved>} is displayed in place of the address literal.
 382      *
 383      * @return  a string representation of this object.
 384      */
 385     @Override
 386     public String toString() {
 387         return holder.toString();
 388     }
 389 
 390     /**
 391      * Compares this object against the specified object.
 392      * The result is {@code true} if and only if the argument is
 393      * not {@code null} and it represents the same address as
 394      * this object.
 395      * <p>
 396      * Two instances of {@code InetSocketAddress} represent the same
 397      * address if both the InetAddresses (or hostnames if it is unresolved) and port
 398      * numbers are equal.
 399      * If both addresses are unresolved, then the hostname and the port number
 400      * are compared.
 401      *


< prev index next >