140 141 private static int checkPort(int port) { 142 if (port < 0 || port > 0xFFFF) 143 throw new IllegalArgumentException("port out of range:" + port); 144 return port; 145 } 146 147 private static String checkHost(String hostname) { 148 if (hostname == null) 149 throw new IllegalArgumentException("hostname can't be null"); 150 return hostname; 151 } 152 153 /** 154 * Creates a socket address where the IP address is the wildcard address 155 * and the port number a specified value. 156 * <p> 157 * A valid port value is between 0 and 65535. 158 * A port number of {@code zero} will let the system pick up an 159 * ephemeral port in a {@code bind} operation. 160 * <p> 161 * @param port The port number 162 * @throws IllegalArgumentException if the port parameter is outside the specified 163 * range of valid port values. 164 */ 165 public InetSocketAddress(int port) { 166 this(InetAddress.anyLocalAddress(), port); 167 } 168 169 /** 170 * 171 * Creates a socket address from an IP address and a port number. 172 * <p> 173 * A valid port value is between 0 and 65535. 174 * A port number of {@code zero} will let the system pick up an 175 * ephemeral port in a {@code bind} operation. 176 * <P> 177 * A {@code null} address will assign the <i>wildcard</i> address. 178 * <p> 179 * @param addr The IP address 180 * @param port The port number 181 * @throws IllegalArgumentException if the port parameter is outside the specified 182 * range of valid port values. 183 */ 184 public InetSocketAddress(InetAddress addr, int port) { 185 holder = new InetSocketAddressHolder( 186 null, 187 addr == null ? InetAddress.anyLocalAddress() : addr, 188 checkPort(port)); 189 } 190 191 /** 192 * 193 * Creates a socket address from a hostname and a port number. 194 * <p> 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>. 197 * <p> 198 * If there is a security manager, its {@code checkConnect} method 199 * is called with the host name as its argument to check the permission 200 * to resolve it. This could result in a SecurityException. 201 * <P> 202 * A valid port value is between 0 and 65535. 203 * A port number of {@code zero} will let the system pick up an 204 * ephemeral port in a {@code bind} operation. 205 * <P> 206 * @param hostname the Host name 207 * @param port The port number 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>. 210 * @throws SecurityException if a security manager is present and 211 * permission to resolve the host name is 212 * denied. 213 * @see #isUnresolved() 214 */ 215 public InetSocketAddress(String hostname, int port) { 216 checkHost(hostname); 217 InetAddress addr = null; 218 String host = null; 219 try { 220 addr = InetAddress.getByName(hostname); 221 } catch(UnknownHostException e) { 222 host = hostname; 223 } 224 holder = new InetSocketAddressHolder(host, addr, checkPort(port)); 225 } 226 227 // private constructor for creating unresolved instances 228 private InetSocketAddress(int port, String hostname) { 229 holder = new InetSocketAddressHolder(hostname, null, port); 230 } 231 232 /** 233 * 234 * Creates an unresolved socket address from a hostname and a port number. 235 * <p> 236 * No attempt will be made to resolve the hostname into an InetAddress. 237 * The address will be flagged as <I>unresolved</I>. 238 * <p> 239 * A valid port value is between 0 and 65535. 240 * A port number of {@code zero} will let the system pick up an 241 * ephemeral port in a {@code bind} operation. 242 * <P> 243 * @param host the Host name 244 * @param port The port number 245 * @throws IllegalArgumentException if the port parameter is outside 246 * the range of valid port values, or if the hostname 247 * parameter is <TT>null</TT>. 248 * @see #isUnresolved() 249 * @return a {@code InetSocketAddress} representing the unresolved 250 * socket address 251 * @since 1.5 252 */ 253 public static InetSocketAddress createUnresolved(String host, int port) { 254 return new InetSocketAddress(checkPort(port), checkHost(host)); 255 } 256 257 /** 258 * @serialField hostname String 259 * @serialField addr InetAddress 260 * @serialField port int 261 */ 262 private static final ObjectStreamField[] serialPersistentFields = { | 140 141 private static int checkPort(int port) { 142 if (port < 0 || port > 0xFFFF) 143 throw new IllegalArgumentException("port out of range:" + port); 144 return port; 145 } 146 147 private static String checkHost(String hostname) { 148 if (hostname == null) 149 throw new IllegalArgumentException("hostname can't be null"); 150 return hostname; 151 } 152 153 /** 154 * Creates a socket address where the IP address is the wildcard address 155 * and the port number a specified value. 156 * <p> 157 * A valid port value is between 0 and 65535. 158 * A port number of {@code zero} will let the system pick up an 159 * ephemeral port in a {@code bind} operation. 160 * 161 * @param port The port number 162 * @throws IllegalArgumentException if the port parameter is outside the specified 163 * range of valid port values. 164 */ 165 public InetSocketAddress(int port) { 166 this(InetAddress.anyLocalAddress(), port); 167 } 168 169 /** 170 * 171 * Creates a socket address from an IP address and a port number. 172 * <p> 173 * A valid port value is between 0 and 65535. 174 * A port number of {@code zero} will let the system pick up an 175 * ephemeral port in a {@code bind} operation. 176 * <P> 177 * A {@code null} address will assign the <i>wildcard</i> address. 178 * 179 * @param addr The IP address 180 * @param port The port number 181 * @throws IllegalArgumentException if the port parameter is outside the specified 182 * range of valid port values. 183 */ 184 public InetSocketAddress(InetAddress addr, int port) { 185 holder = new InetSocketAddressHolder( 186 null, 187 addr == null ? InetAddress.anyLocalAddress() : addr, 188 checkPort(port)); 189 } 190 191 /** 192 * 193 * Creates a socket address from a hostname and a port number. 194 * <p> 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>. 197 * <p> 198 * If there is a security manager, its {@code checkConnect} method 199 * is called with the host name as its argument to check the permission 200 * to resolve it. This could result in a SecurityException. 201 * <P> 202 * A valid port value is between 0 and 65535. 203 * A port number of {@code zero} will let the system pick up an 204 * ephemeral port in a {@code bind} operation. 205 * 206 * @param hostname the Host name 207 * @param port The port number 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>. 210 * @throws SecurityException if a security manager is present and 211 * permission to resolve the host name is 212 * denied. 213 * @see #isUnresolved() 214 */ 215 public InetSocketAddress(String hostname, int port) { 216 checkHost(hostname); 217 InetAddress addr = null; 218 String host = null; 219 try { 220 addr = InetAddress.getByName(hostname); 221 } catch(UnknownHostException e) { 222 host = hostname; 223 } 224 holder = new InetSocketAddressHolder(host, addr, checkPort(port)); 225 } 226 227 // private constructor for creating unresolved instances 228 private InetSocketAddress(int port, String hostname) { 229 holder = new InetSocketAddressHolder(hostname, null, port); 230 } 231 232 /** 233 * 234 * Creates an unresolved socket address from a hostname and a port number. 235 * <p> 236 * No attempt will be made to resolve the hostname into an InetAddress. 237 * The address will be flagged as <I>unresolved</I>. 238 * <p> 239 * A valid port value is between 0 and 65535. 240 * A port number of {@code zero} will let the system pick up an 241 * ephemeral port in a {@code bind} operation. 242 * 243 * @param host the Host name 244 * @param port The port number 245 * @throws IllegalArgumentException if the port parameter is outside 246 * the range of valid port values, or if the hostname 247 * parameter is <TT>null</TT>. 248 * @see #isUnresolved() 249 * @return a {@code InetSocketAddress} representing the unresolved 250 * socket address 251 * @since 1.5 252 */ 253 public static InetSocketAddress createUnresolved(String host, int port) { 254 return new InetSocketAddress(checkPort(port), checkHost(host)); 255 } 256 257 /** 258 * @serialField hostname String 259 * @serialField addr InetAddress 260 * @serialField port int 261 */ 262 private static final ObjectStreamField[] serialPersistentFields = { |