128 * IPv4-mapped address as input, both in byte array and text |
129 * IPv4-mapped address as input, both in byte array and text |
129 * representation. However, it will be converted into an IPv4 |
130 * representation. However, it will be converted into an IPv4 |
130 * address.</td></tr> |
131 * address.</td></tr> |
131 * </table></blockquote> |
132 * </table></blockquote> |
132 * <p> |
133 * <p> |
133 * <h4> <A NAME="scoped">Textual representation of IPv6 scoped addresses</a> </h4> |
134 * <h4><A NAME="scoped">Textual representation of IPv6 scoped addresses</a></h4> |
134 * <p> |
135 * |
135 * The textual representation of IPv6 addresses as described above can be extended |
136 * <p> The textual representation of IPv6 addresses as described above can be |
136 * to specify IPv6 scoped addresses. This extension to the basic addressing architecture |
137 * extended to specify IPv6 scoped addresses. This extension to the basic |
137 * is described in [draft-ietf-ipngwg-scoping-arch-04.txt]. |
138 * addressing architecture is described in [draft-ietf-ipngwg-scoping-arch-04.txt]. |
138 * <p> |
139 * |
139 * Because link-local and site-local addresses are non-global, it is possible that different hosts |
140 * <p> Because link-local and site-local addresses are non-global, it is possible |
140 * may have the same destination address and may be reachable through different interfaces on the |
141 * that different hosts may have the same destination address and may be |
141 * same originating system. In this case, the originating system is said to be connected |
142 * reachable through different interfaces on the same originating system. In |
142 * to multiple zones of the same scope. In order to disambiguate which is the intended destination |
143 * this case, the originating system is said to be connected to multiple zones |
143 * zone, it is possible to append a zone identifier (or <i>scope_id</i>) to an IPv6 address. |
144 * of the same scope. In order to disambiguate which is the intended destination |
144 * <p> |
145 * zone, it is possible to append a zone identifier (or <i>scope_id</i>) to an |
145 * The general format for specifying the <i>scope_id</i> is the following: |
146 * IPv6 address. |
|
147 * |
|
148 * <p> The general format for specifying the <i>scope_id</i> is the following: |
|
149 * |
146 * <p><blockquote><i>IPv6-address</i>%<i>scope_id</i></blockquote> |
150 * <p><blockquote><i>IPv6-address</i>%<i>scope_id</i></blockquote> |
147 * <p> The IPv6-address is a literal IPv6 address as described above. |
151 * <p> The IPv6-address is a literal IPv6 address as described above. |
148 * The <i>scope_id</i> refers to an interface on the local system, and it can be specified |
152 * The <i>scope_id</i> refers to an interface on the local system, and it can be |
149 * in two ways. |
153 * specified in two ways. |
150 * <p><ol><li><i>As a numeric identifier.</i> This must be a positive integer that identifies the |
154 * <p><ol><li><i>As a numeric identifier.</i> This must be a positive integer |
151 * particular interface and scope as understood by the system. Usually, the numeric |
155 * that identifies the particular interface and scope as understood by the |
152 * values can be determined through administration tools on the system. Each interface may |
156 * system. Usually, the numeric values can be determined through administration |
153 * have multiple values, one for each scope. If the scope is unspecified, then the default value |
157 * tools on the system. Each interface may have multiple values, one for each |
154 * used is zero.</li><p> |
158 * scope. If the scope is unspecified, then the default value used is zero.</li> |
155 * <li><i>As a string.</i> This must be the exact string that is returned by |
159 * <p><li><i>As a string.</i> This must be the exact string that is returned by |
156 * {@link java.net.NetworkInterface#getName()} for the particular interface in question. |
160 * {@link java.net.NetworkInterface#getName()} for the particular interface in |
157 * When an Inet6Address is created in this way, the numeric scope-id is determined at the time |
161 * question. When an Inet6Address is created in this way, the numeric scope-id |
158 * the object is created by querying the relevant NetworkInterface.</li> |
162 * is determined at the time the object is created by querying the relevant |
159 * </ol><p> |
163 * NetworkInterface.</li></ol> |
160 * Note also, that the numeric <i>scope_id</i> can be retrieved from Inet6Address instances returned from the |
164 * |
161 * NetworkInterface class. This can be used to find out the current scope ids configured on the system. |
165 * <p> Note also, that the numeric <i>scope_id</i> can be retrieved from |
|
166 * Inet6Address instances returned from the NetworkInterface class. This can be |
|
167 * used to find out the current scope ids configured on the system. |
162 * @since 1.4 |
168 * @since 1.4 |
163 */ |
169 */ |
164 |
170 |
165 public final |
171 public final |
166 class Inet6Address extends InetAddress { |
172 class Inet6Address extends InetAddress { |
167 final static int INADDRSZ = 16; |
173 final static int INADDRSZ = 16; |
168 |
174 |
169 /* |
175 /* |
170 * cached scope_id - for link-local address use only. |
176 * cached scope_id - for link-local address use only. |
171 */ |
177 */ |
172 private transient int cached_scope_id = 0; |
178 private transient int cached_scope_id; // 0 |
173 |
179 |
174 /** |
180 /** |
175 * Holds a 128-bit (16 bytes) IPv6 address. |
181 * Holds a 128-bit (16 bytes) IPv6 address. |
176 * |
182 * |
177 * @serial |
183 * @serial |
178 */ |
184 */ |
179 byte[] ipaddress; |
185 byte[] ipaddress; |
180 |
186 |
181 /** |
187 /** |
182 * scope_id. The scope specified when the object is created. If the object is created |
188 * scope_id. The scope specified when the object is created. If the object |
183 * with an interface name, then the scope_id is not determined until the time it is needed. |
189 * is created with an interface name, then the scope_id is not determined |
184 */ |
190 * until the time it is needed. |
185 private int scope_id = 0; |
191 */ |
|
192 private int scope_id; // 0 |
186 |
193 |
187 /** |
194 /** |
188 * This will be set to true when the scope_id field contains a valid |
195 * This will be set to true when the scope_id field contains a valid |
189 * integer scope_id. |
196 * integer scope_id. |
190 */ |
197 */ |
191 private boolean scope_id_set = false; |
198 private boolean scope_id_set; // false |
192 |
199 |
193 /** |
200 /** |
194 * scoped interface. scope_id is derived from this as the scope_id of the first |
201 * scoped interface. scope_id is derived from this as the scope_id of the first |
195 * address whose scope is the same as this address for the named interface. |
202 * address whose scope is the same as this address for the named interface. |
196 */ |
203 */ |
197 private transient NetworkInterface scope_ifname = null; |
204 private transient NetworkInterface scope_ifname; // null |
198 |
|
199 /** |
|
200 * set if the object is constructed with a scoped interface instead of a |
|
201 * numeric scope id. |
|
202 */ |
|
203 private boolean scope_ifname_set = false; |
|
204 |
205 |
205 private static final long serialVersionUID = 6880410070516793377L; |
206 private static final long serialVersionUID = 6880410070516793377L; |
206 |
207 |
207 /* |
208 // Perform native initialization |
208 * Perform initializations. |
209 static { init(); } |
209 */ |
|
210 static { |
|
211 init(); |
|
212 } |
|
213 |
210 |
214 Inet6Address() { |
211 Inet6Address() { |
215 super(); |
212 super(); |
216 hostName = null; |
213 hostName = null; |
217 ipaddress = new byte[INADDRSZ]; |
214 ipaddress = new byte[INADDRSZ]; |
237 try { |
234 try { |
238 initif (hostName, addr, null); |
235 initif (hostName, addr, null); |
239 } catch (UnknownHostException e) {} /* cant happen if ifname is null */ |
236 } catch (UnknownHostException e) {} /* cant happen if ifname is null */ |
240 } |
237 } |
241 |
238 |
242 Inet6Address (String hostName, byte addr[], NetworkInterface nif) throws UnknownHostException { |
239 Inet6Address (String hostName, byte addr[], NetworkInterface nif) |
|
240 throws UnknownHostException |
|
241 { |
243 initif (hostName, addr, nif); |
242 initif (hostName, addr, nif); |
244 } |
243 } |
245 |
244 |
246 Inet6Address (String hostName, byte addr[], String ifname) throws UnknownHostException { |
245 Inet6Address (String hostName, byte addr[], String ifname) |
|
246 throws UnknownHostException |
|
247 { |
247 initstr (hostName, addr, ifname); |
248 initstr (hostName, addr, ifname); |
248 } |
249 } |
249 |
250 |
250 /** |
251 /** |
251 * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])} |
252 * Create an Inet6Address in the exact manner of {@link |
252 * except that the IPv6 scope_id is set to the value corresponding to the given interface |
253 * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is |
253 * for the address type specified in <code>addr</code>. |
254 * set to the value corresponding to the given interface for the address |
254 * The call will fail with an UnknownHostException if the given interface does not have a numeric |
255 * type specified in <code>addr</code>. The call will fail with an |
|
256 * UnknownHostException if the given interface does not have a numeric |
255 * scope_id assigned for the given address type (eg. link-local or site-local). |
257 * scope_id assigned for the given address type (eg. link-local or site-local). |
256 * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 |
258 * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 |
257 * scoped addresses. |
259 * scoped addresses. |
258 * |
260 * |
259 * @param host the specified host |
261 * @param host the specified host |
260 * @param addr the raw IP address in network byte order |
262 * @param addr the raw IP address in network byte order |
261 * @param nif an interface this address must be associated with. |
263 * @param nif an interface this address must be associated with. |
262 * @return an Inet6Address object created from the raw IP address. |
264 * @return an Inet6Address object created from the raw IP address. |
263 * @exception UnknownHostException if IP address is of illegal length, or if the interface |
265 * @throws UnknownHostException |
264 * does not have a numeric scope_id assigned for the given address type. |
266 * if IP address is of illegal length, or if the interface does not |
|
267 * have a numeric scope_id assigned for the given address type. |
265 * |
268 * |
266 * @since 1.5 |
269 * @since 1.5 |
267 */ |
270 */ |
268 |
271 public static Inet6Address getByAddress(String host, byte[] addr, |
269 public static Inet6Address getByAddress(String host, byte[] addr, NetworkInterface nif) |
272 NetworkInterface nif) |
270 throws UnknownHostException { |
273 throws UnknownHostException |
|
274 { |
271 if (host != null && host.length() > 0 && host.charAt(0) == '[') { |
275 if (host != null && host.length() > 0 && host.charAt(0) == '[') { |
272 if (host.charAt(host.length()-1) == ']') { |
276 if (host.charAt(host.length()-1) == ']') { |
273 host = host.substring(1, host.length() -1); |
277 host = host.substring(1, host.length() -1); |
274 } |
278 } |
275 } |
279 } |
280 } |
284 } |
281 throw new UnknownHostException("addr is of illegal length"); |
285 throw new UnknownHostException("addr is of illegal length"); |
282 } |
286 } |
283 |
287 |
284 /** |
288 /** |
285 * Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])} |
289 * Create an Inet6Address in the exact manner of {@link |
286 * except that the IPv6 scope_id is set to the given numeric value. |
290 * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is |
287 * The scope_id is not checked to determine if it corresponds to any interface on the system. |
291 * set to the given numeric value. The scope_id is not checked to determine |
|
292 * if it corresponds to any interface on the system. |
288 * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 |
293 * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 |
289 * scoped addresses. |
294 * scoped addresses. |
290 * |
295 * |
291 * @param host the specified host |
296 * @param host the specified host |
292 * @param addr the raw IP address in network byte order |
297 * @param addr the raw IP address in network byte order |
293 * @param scope_id the numeric scope_id for the address. |
298 * @param scope_id the numeric scope_id for the address. |
294 * @return an Inet6Address object created from the raw IP address. |
299 * @return an Inet6Address object created from the raw IP address. |
295 * @exception UnknownHostException if IP address is of illegal length. |
300 * @throws UnknownHostException if IP address is of illegal length. |
296 * |
301 * |
297 * @since 1.5 |
302 * @since 1.5 |
298 */ |
303 */ |
299 |
304 public static Inet6Address getByAddress(String host, byte[] addr, |
300 public static Inet6Address getByAddress(String host, byte[] addr, int scope_id) |
305 int scope_id) |
301 throws UnknownHostException { |
306 throws UnknownHostException |
|
307 { |
302 if (host != null && host.length() > 0 && host.charAt(0) == '[') { |
308 if (host != null && host.length() > 0 && host.charAt(0) == '[') { |
303 if (host.charAt(host.length()-1) == ']') { |
309 if (host.charAt(host.length()-1) == ']') { |
304 host = host.substring(1, host.length() -1); |
310 host = host.substring(1, host.length() -1); |
305 } |
311 } |
306 } |
312 } |
322 } catch (SocketException e) { |
330 } catch (SocketException e) { |
323 throw new UnknownHostException ("SocketException thrown" + ifname); |
331 throw new UnknownHostException ("SocketException thrown" + ifname); |
324 } |
332 } |
325 } |
333 } |
326 |
334 |
327 private void initif(String hostName, byte addr[],NetworkInterface nif) throws UnknownHostException { |
335 private void initif(String hostName, byte addr[],NetworkInterface nif) |
|
336 throws UnknownHostException |
|
337 { |
328 this.hostName = hostName; |
338 this.hostName = hostName; |
329 if (addr.length == INADDRSZ) { // normal IPv6 address |
339 if (addr.length == INADDRSZ) { // normal IPv6 address |
330 family = IPv6; |
340 family = IPv6; |
331 ipaddress = addr.clone(); |
341 ipaddress = addr.clone(); |
332 } |
342 } |
333 if (nif != null) { |
343 if (nif != null) { |
334 this.scope_ifname = nif; |
344 scope_ifname = nif; |
335 scope_ifname_set = true; |
345 scope_id = deriveNumericScope(nif); |
336 scope_id = deriveNumericScope (nif); |
|
337 scope_id_set = true; |
346 scope_id_set = true; |
338 } |
347 } |
339 } |
348 } |
340 |
349 |
341 /* check the two Ipv6 addresses and return false if they are both |
350 /* check the two Ipv6 addresses and return false if they are both |
342 * non global address types, but not the same. |
351 * non global address types, but not the same. |
343 * (ie. one is sitelocal and the other linklocal) |
352 * (ie. one is sitelocal and the other linklocal) |
344 * return true otherwise. |
353 * return true otherwise. |
345 */ |
354 */ |
346 private boolean differentLocalAddressTypes(Inet6Address other) { |
355 private boolean differentLocalAddressTypes(Inet6Address other) { |
347 |
356 if (isLinkLocalAddress() && !other.isLinkLocalAddress()) |
348 if (isLinkLocalAddress() && !other.isLinkLocalAddress()) { |
|
349 return false; |
357 return false; |
350 } |
358 if (isSiteLocalAddress() && !other.isSiteLocalAddress()) |
351 if (isSiteLocalAddress() && !other.isSiteLocalAddress()) { |
|
352 return false; |
359 return false; |
353 } |
|
354 return true; |
360 return true; |
355 } |
361 } |
356 |
362 |
357 private int deriveNumericScope (NetworkInterface ifc) throws UnknownHostException { |
363 private int deriveNumericScope(NetworkInterface ifc) |
|
364 throws UnknownHostException |
|
365 { |
358 Enumeration<InetAddress> addresses = ifc.getInetAddresses(); |
366 Enumeration<InetAddress> addresses = ifc.getInetAddresses(); |
359 while (addresses.hasMoreElements()) { |
367 while (addresses.hasMoreElements()) { |
360 InetAddress addr = addresses.nextElement(); |
368 InetAddress addr = addresses.nextElement(); |
361 if (!(addr instanceof Inet6Address)) { |
369 if (!(addr instanceof Inet6Address)) { |
362 continue; |
370 continue; |
371 return ia6_addr.scope_id; |
379 return ia6_addr.scope_id; |
372 } |
380 } |
373 throw new UnknownHostException ("no scope_id found"); |
381 throw new UnknownHostException ("no scope_id found"); |
374 } |
382 } |
375 |
383 |
376 private int deriveNumericScope (String ifname) throws UnknownHostException { |
384 private int deriveNumericScope(String ifname) throws UnknownHostException { |
377 Enumeration<NetworkInterface> en; |
385 Enumeration<NetworkInterface> en; |
378 try { |
386 try { |
379 en = NetworkInterface.getNetworkInterfaces(); |
387 en = NetworkInterface.getNetworkInterfaces(); |
380 } catch (SocketException e) { |
388 } catch (SocketException e) { |
381 throw new UnknownHostException ("could not enumerate local network interfaces"); |
389 throw new UnknownHostException( |
|
390 "could not enumerate local network interfaces"); |
382 } |
391 } |
383 while (en.hasMoreElements()) { |
392 while (en.hasMoreElements()) { |
384 NetworkInterface ifc = en.nextElement(); |
393 NetworkInterface ifc = en.nextElement(); |
385 if (ifc.getName().equals (ifname)) { |
394 if (ifc.getName().equals(ifname)) { |
386 Enumeration<InetAddress> addresses = ifc.getInetAddresses(); |
395 Enumeration<InetAddress> addresses = ifc.getInetAddresses(); |
387 while (addresses.hasMoreElements()) { |
396 while (addresses.hasMoreElements()) { |
388 InetAddress addr = addresses.nextElement(); |
397 InetAddress addr = addresses.nextElement(); |
389 if (!(addr instanceof Inet6Address)) { |
398 if (!(addr instanceof Inet6Address)) { |
390 continue; |
399 continue; |
398 /* found a matching address - return its scope_id */ |
407 /* found a matching address - return its scope_id */ |
399 return ia6_addr.scope_id; |
408 return ia6_addr.scope_id; |
400 } |
409 } |
401 } |
410 } |
402 } |
411 } |
403 throw new UnknownHostException ("No matching address found for interface : " +ifname); |
412 throw new UnknownHostException( |
|
413 "No matching address found for interface : " +ifname); |
404 } |
414 } |
405 |
415 |
406 /** |
416 /** |
407 * restore the state of this object from stream |
417 * restore the state of this object from stream |
408 * including the scope information, only if the |
418 * including the scope information, only if the |
409 * scoped interface name is valid on this system |
419 * scoped interface name is valid on this system |
410 */ |
420 */ |
411 private void readObject(ObjectInputStream s) |
421 private void readObject(ObjectInputStream s) |
412 throws IOException, ClassNotFoundException { |
422 throws IOException, ClassNotFoundException { |
413 scope_ifname = null; |
|
414 scope_ifname_set = false; |
|
415 s.defaultReadObject(); |
423 s.defaultReadObject(); |
416 |
424 |
417 if (ifname != null && !"".equals (ifname)) { |
425 if (ifname != null && !ifname.equals("")) { |
418 try { |
426 try { |
419 scope_ifname = NetworkInterface.getByName(ifname); |
427 scope_ifname = NetworkInterface.getByName(ifname); |
420 if (scope_ifname == null) { |
428 if (scope_ifname != null) { |
421 /* the interface does not exist on this system, so we clear |
|
422 * the scope information completely */ |
|
423 scope_id_set = false; |
|
424 scope_ifname_set = false; |
|
425 scope_id = 0; |
|
426 } else { |
|
427 try { |
429 try { |
428 scope_id = deriveNumericScope (scope_ifname); |
430 scope_id = deriveNumericScope(scope_ifname); |
429 } catch (UnknownHostException e) { |
431 } catch (UnknownHostException e) { |
430 // typically should not happen, but it may be that |
432 // typically should not happen, but it may be that |
431 // the machine being used for deserialization has |
433 // the machine being used for deserialization has |
432 // the same interface name but without IPv6 configured. |
434 // the same interface name but without IPv6 configured. |
433 } |
435 } |
578 } |
590 } |
579 |
591 |
580 /** |
592 /** |
581 * Utility routine to check if the multicast address has organization scope. |
593 * Utility routine to check if the multicast address has organization scope. |
582 * |
594 * |
583 * @return a <code>boolean</code> indicating if the address has |
595 * @return a {@code boolean} indicating if the address has is a multicast |
584 * is a multicast address of organization-local scope, |
596 * address of organization-local scope, false if it is not of |
585 * false if it is not of organization-local scope |
597 * organization-local scope or it is not a multicast address |
586 * or it is not a multicast address |
598 * |
587 * @since 1.4 |
599 * @since 1.4 |
588 */ |
600 */ |
589 @Override |
601 @Override |
590 public boolean isMCOrgLocal() { |
602 public boolean isMCOrgLocal() { |
591 return ((ipaddress[0] & 0xff) == 0xff |
603 return ((ipaddress[0] & 0xff) == 0xff |
592 && (ipaddress[1] & 0x0f) == 0x08); |
604 && (ipaddress[1] & 0x0f) == 0x08); |
593 } |
605 } |
594 |
606 |
595 /** |
607 /** |
596 * Returns the raw IP address of this <code>InetAddress</code> |
608 * Returns the raw IP address of this {@code InetAddress} object. The result |
597 * object. The result is in network byte order: the highest order |
609 * is in network byte order: the highest order byte of the address is in |
598 * byte of the address is in <code>getAddress()[0]</code>. |
610 * {@code getAddress()[0]}. |
599 * |
611 * |
600 * @return the raw IP address of this object. |
612 * @return the raw IP address of this object. |
601 */ |
613 */ |
602 @Override |
614 @Override |
603 public byte[] getAddress() { |
615 public byte[] getAddress() { |
607 /** |
619 /** |
608 * Returns the numeric scopeId, if this instance is associated with |
620 * Returns the numeric scopeId, if this instance is associated with |
609 * an interface. If no scoped_id is set, the returned value is zero. |
621 * an interface. If no scoped_id is set, the returned value is zero. |
610 * |
622 * |
611 * @return the scopeId, or zero if not set. |
623 * @return the scopeId, or zero if not set. |
|
624 * |
612 * @since 1.5 |
625 * @since 1.5 |
613 */ |
626 */ |
614 public int getScopeId () { |
627 public int getScopeId() { |
615 return scope_id; |
628 return scope_id; |
616 } |
629 } |
617 |
630 |
618 /** |
631 /** |
619 * Returns the scoped interface, if this instance was created with |
632 * Returns the scoped interface, if this instance was created with |
620 * with a scoped interface. |
633 * with a scoped interface. |
621 * |
634 * |
622 * @return the scoped interface, or null if not set. |
635 * @return the scoped interface, or null if not set. |
623 * @since 1.5 |
636 * @since 1.5 |
624 */ |
637 */ |
625 public NetworkInterface getScopedInterface () { |
638 public NetworkInterface getScopedInterface() { |
626 return scope_ifname; |
639 return scope_ifname; |
627 } |
640 } |
628 |
641 |
629 /** |
642 /** |
630 * Returns the IP address string in textual presentation. If the instance was created |
643 * Returns the IP address string in textual presentation. If the instance |
631 * specifying a scope identifier then the scope id is appended to the IP address preceded by |
644 * was created specifying a scope identifier then the scope id is appended |
632 * a "%" (per-cent) character. This can be either a numeric value or a string, depending on which |
645 * to the IP address preceded by a "%" (per-cent) character. This can be |
633 * was used to createthe instance. |
646 * either a numeric value or a string, depending on which was used to create |
|
647 * the instance. |
634 * |
648 * |
635 * @return the raw IP address in a string format. |
649 * @return the raw IP address in a string format. |
636 */ |
650 */ |
637 @Override |
651 @Override |
638 public String getHostAddress() { |
652 public String getHostAddress() { |
639 String s = numericToTextFormat(ipaddress); |
653 String s = numericToTextFormat(ipaddress); |
640 if (scope_ifname_set) { /* must check this first */ |
654 if (scope_ifname != null) { /* must check this first */ |
641 s = s + "%" + scope_ifname.getName(); |
655 s = s + "%" + scope_ifname.getName(); |
642 } else if (scope_id_set) { |
656 } else if (scope_id_set) { |
643 s = s + "%" + scope_id; |
657 s = s + "%" + scope_id; |
644 } |
658 } |
645 return s; |
659 return s; |
672 return 0; |
686 return 0; |
673 } |
687 } |
674 } |
688 } |
675 |
689 |
676 /** |
690 /** |
677 * Compares this object against the specified object. |
691 * Compares this object against the specified object. The result is {@code |
678 * The result is <code>true</code> if and only if the argument is |
692 * true} if and only if the argument is not {@code null} and it represents |
679 * not <code>null</code> and it represents the same IP address as |
693 * the same IP address as this object. |
680 * this object. |
694 * |
681 * <p> |
695 * <p> Two instances of {@code InetAddress} represent the same IP address |
682 * Two instances of <code>InetAddress</code> represent the same IP |
696 * if the length of the byte arrays returned by {@code getAddress} is the |
683 * address if the length of the byte arrays returned by |
697 * same for both, and each of the array components is the same for the byte |
684 * <code>getAddress</code> is the same for both, and each of the |
698 * arrays. |
685 * array components is the same for the byte arrays. |
|
686 * |
699 * |
687 * @param obj the object to compare against. |
700 * @param obj the object to compare against. |
688 * @return <code>true</code> if the objects are the same; |
701 * |
689 * <code>false</code> otherwise. |
702 * @return {@code true} if the objects are the same; {@code false} otherwise. |
|
703 * |
690 * @see java.net.InetAddress#getAddress() |
704 * @see java.net.InetAddress#getAddress() |
691 */ |
705 */ |
692 @Override |
706 @Override |
693 public boolean equals(Object obj) { |
707 public boolean equals(Object obj) { |
694 if (obj == null || |
708 if (obj == null || !(obj instanceof Inet6Address)) |
695 !(obj instanceof Inet6Address)) |
|
696 return false; |
709 return false; |
697 |
710 |
698 Inet6Address inetAddr = (Inet6Address)obj; |
711 Inet6Address inetAddr = (Inet6Address)obj; |
699 |
|
700 for (int i = 0; i < INADDRSZ; i++) { |
712 for (int i = 0; i < INADDRSZ; i++) { |
701 if (ipaddress[i] != inetAddr.ipaddress[i]) |
713 if (ipaddress[i] != inetAddr.ipaddress[i]) |
702 return false; |
714 return false; |
703 } |
715 } |
704 |
716 |