Source for java.net.InetSocketAddress

   1: /* InetSocketAddress.java --
   2:    Copyright (C) 2002, 2006  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package java.net;
  39: 
  40: 
  41: /**
  42:  * InetSocketAddress instances represent socket addresses
  43:  * in the java.nio package. They encapsulate a InetAddress and
  44:  * a port number.
  45:  *
  46:  * @since 1.4
  47:  */
  48: public class InetSocketAddress extends SocketAddress
  49: {
  50:   /**
  51:    * Compatible with JDK 1.4+
  52:    */
  53:   private static final long serialVersionUID = 5076001401234631237L;
  54: 
  55:   /**
  56:    * Name of host.
  57:    */
  58:   private String hostname;
  59: 
  60:   /**
  61:    * Address of host.
  62:    */
  63:   private InetAddress addr;
  64: 
  65:   /**
  66:    * Port of host.
  67:    */
  68:   private int port;
  69: 
  70:   /**
  71:    * Constructs an InetSocketAddress instance.
  72:    *
  73:    * @param addr Address of the socket
  74:    * @param port Port if the socket
  75:    *
  76:    * @exception IllegalArgumentException If the port number is illegel
  77:    */
  78:   public InetSocketAddress(InetAddress addr, int port)
  79:     throws IllegalArgumentException
  80:   {
  81:     if (port < 0 || port > 65535)
  82:       throw new IllegalArgumentException("Bad port number: " + port);
  83: 
  84:     if (addr == null)
  85:       addr = InetAddress.ANY_IF;
  86: 
  87:     this.addr = addr;
  88:     this.port = port;
  89:     this.hostname = addr.getHostName();
  90:   }
  91: 
  92:   /**
  93:    * Constructs an InetSocketAddress instance.
  94:    *
  95:    * @param port Port if the socket
  96:    *
  97:    * @exception IllegalArgumentException If the port number is illegal
  98:    */
  99:   public InetSocketAddress(int port) throws IllegalArgumentException
 100:   {
 101:     this((InetAddress) null, port);
 102:   }
 103: 
 104:   /**
 105:    * Constructs an InetSocketAddress instance.
 106:    *
 107:    * @param hostname The hostname for the socket address
 108:    * @param port The port for the socket address
 109:    *
 110:    * @exception IllegalArgumentException If the port number is illegal or
 111:    * the hostname argument is null
 112:    */
 113:   public InetSocketAddress(String hostname, int port)
 114:   {
 115:     this(hostname, port, true);
 116:   }
 117: 
 118:   /**
 119:    * Constructs an InetSocketAddress instance.
 120:    *
 121:    * @param hostname The hostname for the socket address
 122:    * @param port The port for the socket address
 123:    * @param resolve <code>true</code> if the address has to be resolved,
 124:    * <code>false</code> otherwise
 125:    *
 126:    * @exception IllegalArgumentException If the port number is illegal or
 127:    * the hostname argument is null
 128:    */
 129:   private InetSocketAddress(String hostname, int port, boolean resolve)
 130:   {
 131:     if (hostname == null)
 132:       throw new IllegalArgumentException("Null host name value");
 133: 
 134:     if (port < 0 || port > 65535)
 135:       throw new IllegalArgumentException("Bad port number: " + port);
 136: 
 137:     this.port = port;
 138:     this.hostname = hostname;
 139:     this.addr = null;
 140: 
 141:     if (resolve)
 142:     {
 143:       try
 144:         {
 145:           this.addr = InetAddress.getByName(hostname);
 146:         }
 147:       catch (Exception e) // UnknownHostException, SecurityException
 148:         {
 149:           // Do nothing here. this.addr is already set to null.
 150:         }
 151:     }
 152: 
 153:   }
 154: 
 155:   /**
 156:    * Creates an unresolved <code>InetSocketAddress</code> object.
 157:    *
 158:    * @param hostname The hostname for the socket address
 159:    * @param port The port for the socket address
 160:    *
 161:    * @exception IllegalArgumentException If the port number is illegal or
 162:    * the hostname argument is null
 163:    *
 164:    * @since 1.5
 165:    */
 166:   public static InetSocketAddress createUnresolved(String hostname, int port)
 167:   {
 168:     return new InetSocketAddress(hostname, port, false);
 169:   }
 170: 
 171:   /**
 172:    * Test if obj is a <code>InetSocketAddress</code> and
 173:    * has the same address and port
 174:    *
 175:    * @param obj The obj to compare this address with.
 176:    *
 177:    * @return True if obj is equal.
 178:    */
 179:   public final boolean equals(Object obj)
 180:   {
 181:     // InetSocketAddress objects are equal when addr and port are equal.
 182:     // The hostname may differ.
 183:     if (obj instanceof InetSocketAddress)
 184:       {
 185:     InetSocketAddress sa = (InetSocketAddress) obj;
 186: 
 187:     if (addr == null && sa.addr != null)
 188:       return false;
 189:     else if (addr == null && sa.addr == null)
 190:       return hostname.equals(sa.hostname) && sa.port == port;
 191:     else
 192:       return addr.equals(sa.addr) && sa.port == port;
 193:       }
 194: 
 195:     return false;
 196:   }
 197: 
 198:   /**
 199:    * Returns the <code>InetAddress</code> or
 200:    * <code>null</code> if its unresolved
 201:    *
 202:    * @return The IP address of this address.
 203:    */
 204:   public final InetAddress getAddress()
 205:   {
 206:     return addr;
 207:   }
 208: 
 209:   /**
 210:    * Returns <code>hostname</code>
 211:    *
 212:    * @return The hostname of this address.
 213:    */
 214:   public final String getHostName()
 215:   {
 216:     return hostname;
 217:   }
 218: 
 219:   /**
 220:    * Returns the <code>port</code>
 221:    *
 222:    * @return The port of this address.
 223:    */
 224:   public final int getPort()
 225:   {
 226:     return port;
 227:   }
 228: 
 229:   /**
 230:    * Returns the hashcode of the <code>InetSocketAddress</code>
 231:    *
 232:    * @return The hashcode for this address.
 233:    */
 234:   public final int hashCode()
 235:   {
 236:     return port + addr.hashCode();
 237:   }
 238: 
 239:   /**
 240:    * Checks wether the address has been resolved or not
 241:    *
 242:    * @return True if address is unresolved.
 243:    */
 244:   public final boolean isUnresolved()
 245:   {
 246:     return addr == null;
 247:   }
 248: 
 249:   /**
 250:    * Returns the <code>InetSocketAddress</code> as string
 251:    *
 252:    * @return A string represenation of this address.
 253:    */
 254:   public String toString()
 255:   {
 256:     return (addr == null ? hostname : addr.toString()) + ":" + port;
 257:   }
 258: }