1 7 8 package java.net; 9 10 import java.security.AccessController ; 11 import java.io.ObjectStreamException ; 12 import sun.security.action.*; 13 14 67 68 public final 69 class Inet4Address extends InetAddress { 70 final static int INADDRSZ = 4; 71 72 75 private static final long serialVersionUID = 3286316764910316507L; 76 77 80 static { 81 init(); 82 } 83 84 Inet4Address() { 85 super(); 86 hostName = null; 87 address = 0; 88 family = IPv4; 89 } 90 91 Inet4Address(String hostName, byte addr[]) { 92 this.hostName = hostName; 93 this.family = IPv4; 94 if (addr != null) { 95 if (addr.length == INADDRSZ) { 96 address = addr[3] & 0xFF; 97 address |= ((addr[2] << 8) & 0xFF00); 98 address |= ((addr[1] << 16) & 0xFF0000); 99 address |= ((addr[0] << 24) & 0xFF000000); 100 } 101 } 102 } 103 Inet4Address(String hostName, int address) { 104 this.hostName = hostName; 105 this.family = IPv4; 106 this.address = address; 107 } 108 109 117 private Object writeReplace() throws ObjectStreamException { 118 InetAddress inet = new InetAddress (); 120 inet.hostName = this.hostName; 121 inet.address = this.address; 122 123 129 inet.family = 2; 130 131 return inet; 132 } 133 134 142 public boolean isMulticastAddress() { 143 return ((address & 0xf0000000) == 0xe0000000); 144 } 145 146 152 public boolean isAnyLocalAddress() { 153 return address == 0; 154 } 155 156 163 private static final int loopback = 2130706433; 164 public boolean isLoopbackAddress() { 165 166 byte[] byteAddr = getAddress(); 167 return byteAddr[0] == 127; 168 } 169 170 177 public boolean isLinkLocalAddress() { 178 return (((address >>> 24) & 0xFF) == 169) 183 && (((address >>> 16) & 0xFF) == 254); 184 } 185 186 193 public boolean isSiteLocalAddress() { 194 return (((address >>> 24) & 0xFF) == 10) 199 || ((((address >>> 24) & 0xFF) == 172) 200 && (((address >>> 16) & 0xF0) == 16)) 201 || ((((address >>> 24) & 0xFF) == 192) 202 && (((address >>> 16) & 0xFF) == 168)); 203 } 204 205 213 public boolean isMCGlobal() { 214 byte[] byteAddr = getAddress(); 216 return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) && 217 !((byteAddr[0] & 0xff) == 224 && byteAddr[1] == 0 && 218 byteAddr[2] == 0); 219 } 220 221 229 public boolean isMCNodeLocal() { 230 return false; 232 } 233 234 242 public boolean isMCLinkLocal() { 243 return (((address >>> 24) & 0xFF) == 224) 245 && (((address >>> 16) & 0xFF) == 0) 246 && (((address >>> 8) & 0xFF) == 0); 247 } 248 249 257 public boolean isMCSiteLocal() { 258 return (((address >>> 24) & 0xFF) == 239) 260 && (((address >>> 16) & 0xFF) == 255); 261 } 262 263 272 public boolean isMCOrgLocal() { 273 return (((address >>> 24) & 0xFF) == 239) 275 && (((address >>> 16) & 0xFF) >= 192) 276 && (((address >>> 16) & 0xFF) <= 195); 277 } 278 279 286 public byte[] getAddress() { 287 byte[] addr = new byte[INADDRSZ]; 288 289 addr[0] = (byte) ((address >>> 24) & 0xFF); 290 addr[1] = (byte) ((address >>> 16) & 0xFF); 291 addr[2] = (byte) ((address >>> 8) & 0xFF); 292 addr[3] = (byte) (address & 0xFF); 293 return addr; 294 } 295 296 302 public String getHostAddress() { 303 return numericToTextFormat(getAddress()); 304 } 305 306 311 public int hashCode() { 312 return address; 313 } 314 315 331 public boolean equals(Object obj) { 332 return (obj != null) && (obj instanceof Inet4Address ) && 333 (((InetAddress )obj).address == address); 334 } 335 336 345 346 static String numericToTextFormat(byte[] src) 347 { 348 return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff); 349 } 350 351 354 private static native void init(); 355 } 356 | Popular Tags |