1 7 package java.net; 8 9 import java.io.ObjectInputStream ; 10 import java.io.IOException ; 11 import java.io.InvalidObjectException ; 12 13 31 public class InetSocketAddress extends SocketAddress { 32 35 private String hostname = null; 36 39 private InetAddress addr = null; 40 43 private int port; 44 45 private static final long serialVersionUID = 5076001401234631237L; 46 47 private InetSocketAddress() { 48 } 49 50 62 public InetSocketAddress(int port) { 63 this(InetAddress.anyLocalAddress(), port); 64 } 65 66 81 public InetSocketAddress(InetAddress addr, int port) { 82 if (port < 0 || port > 0xFFFF) { 83 throw new IllegalArgumentException ("port out of range:" + port); 84 } 85 this.port = port; 86 if (addr == null) 87 this.addr = InetAddress.anyLocalAddress(); 88 else 89 this.addr = addr; 90 } 91 92 116 public InetSocketAddress(String hostname, int port) { 117 if (port < 0 || port > 0xFFFF) { 118 throw new IllegalArgumentException ("port out of range:" + port); 119 } 120 if (hostname == null) { 121 throw new IllegalArgumentException ("hostname can't be null"); 122 } 123 try { 124 addr = InetAddress.getByName(hostname); 125 } catch(UnknownHostException e) { 126 this.hostname = hostname; 127 addr = null; 128 } 129 this.port = port; 130 } 131 132 153 public static InetSocketAddress createUnresolved(String host, int port) { 154 if (port < 0 || port > 0xFFFF) { 155 throw new IllegalArgumentException ("port out of range:" + port); 156 } 157 if (host == null) { 158 throw new IllegalArgumentException ("hostname can't be null"); 159 } 160 InetSocketAddress s = new InetSocketAddress (); 161 s.port = port; 162 s.hostname = host; 163 s.addr = null; 164 return s; 165 } 166 167 private void readObject(ObjectInputStream s) 168 throws IOException , ClassNotFoundException { 169 s.defaultReadObject(); 170 171 if (port < 0 || port > 0xFFFF) { 173 throw new InvalidObjectException ("port out of range:" + port); 174 } 175 176 if (hostname == null && addr == null) { 177 throw new InvalidObjectException ("hostname and addr " + 178 "can't both be null"); 179 } 180 } 181 182 187 public final int getPort() { 188 return port; 189 } 190 191 197 public final InetAddress getAddress() { 198 return addr; 199 } 200 201 206 public final String getHostName() { 207 if (hostname != null) 208 return hostname; 209 if (addr != null) 210 return addr.getHostName(); 211 return null; 212 } 213 214 222 final String getHostString() { 223 if (hostname != null) 224 return hostname; 225 if (addr != null) { 226 if (addr.hostName != null) 227 return addr.hostName; 228 else 229 return addr.getHostAddress(); 230 } 231 return null; 232 } 233 234 240 public final boolean isUnresolved() { 241 return addr == null; 242 } 243 244 252 public String toString() { 253 if (isUnresolved()) { 254 return hostname + ":" + port; 255 } else { 256 return addr.toString() + ":" + port; 257 } 258 } 259 260 277 public final boolean equals(Object obj) { 278 if (obj == null || !(obj instanceof InetSocketAddress )) 279 return false; 280 InetSocketAddress sockAddr = (InetSocketAddress ) obj; 281 boolean sameIP = false; 282 if (this.addr != null) 283 sameIP = this.addr.equals(sockAddr.addr); 284 else if (this.hostname != null) 285 sameIP = (sockAddr.addr == null) && 286 this.hostname.equals(sockAddr.hostname); 287 else 288 sameIP = (sockAddr.addr == null) && (sockAddr.hostname == null); 289 return sameIP && (this.port == sockAddr.port); 290 } 291 292 297 public final int hashCode() { 298 if (addr != null) 299 return addr.hashCode() + port; 300 if (hostname != null) 301 return hostname.hashCode() + port; 302 return port; 303 } 304 } 305 | Popular Tags |