< prev index next >

src/java.base/share/classes/java/net/Inet6Address.java

Print this page
rev 51919 : 8215281: Use String.isEmpty() when applicable in java.base
Reviewed-by: dfuchs, alanb


 416      * type specified in {@code addr}. The call will fail with an
 417      * UnknownHostException if the given interface does not have a numeric
 418      * scope_id assigned for the given address type (eg. link-local or site-local).
 419      * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
 420      * scoped addresses.
 421      *
 422      * @param host the specified host
 423      * @param addr the raw IP address in network byte order
 424      * @param nif an interface this address must be associated with.
 425      * @return  an Inet6Address object created from the raw IP address.
 426      * @throws  UnknownHostException
 427      *          if IP address is of illegal length, or if the interface does not
 428      *          have a numeric scope_id assigned for the given address type.
 429      *
 430      * @since 1.5
 431      */
 432     public static Inet6Address getByAddress(String host, byte[] addr,
 433                                             NetworkInterface nif)
 434         throws UnknownHostException
 435     {
 436         if (host != null && host.length() > 0 && host.charAt(0) == '[') {
 437             if (host.charAt(host.length()-1) == ']') {
 438                 host = host.substring(1, host.length() -1);
 439             }
 440         }
 441         if (addr != null) {
 442             if (addr.length == Inet6Address.INADDRSZ) {
 443                 return new Inet6Address(host, addr, nif);
 444             }
 445         }
 446         throw new UnknownHostException("addr is of illegal length");
 447     }
 448 
 449     /**
 450      * Create an Inet6Address in the exact manner of {@link
 451      * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is
 452      * set to the given numeric value. The scope_id is not checked to determine
 453      * if it corresponds to any interface on the system.
 454      * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
 455      * scoped addresses.
 456      *
 457      * @param host the specified host
 458      * @param addr the raw IP address in network byte order
 459      * @param scope_id the numeric scope_id for the address.
 460      * @return  an Inet6Address object created from the raw IP address.
 461      * @throws  UnknownHostException  if IP address is of illegal length.
 462      *
 463      * @since 1.5
 464      */
 465     public static Inet6Address getByAddress(String host, byte[] addr,
 466                                             int scope_id)
 467         throws UnknownHostException
 468     {
 469         if (host != null && host.length() > 0 && host.charAt(0) == '[') {
 470             if (host.charAt(host.length()-1) == ']') {
 471                 host = host.substring(1, host.length() -1);
 472             }
 473         }
 474         if (addr != null) {
 475             if (addr.length == Inet6Address.INADDRSZ) {
 476                 return new Inet6Address(host, addr, scope_id);
 477             }
 478         }
 479         throw new UnknownHostException("addr is of illegal length");
 480     }
 481 
 482     private void initstr(String hostName, byte addr[], String ifname)
 483         throws UnknownHostException
 484     {
 485         try {
 486             NetworkInterface nif = NetworkInterface.getByName (ifname);
 487             if (nif == null) {
 488                 throw new UnknownHostException ("no such interface " + ifname);
 489             }


 584     /**
 585      * restore the state of this object from stream
 586      * including the scope information, only if the
 587      * scoped interface name is valid on this system
 588      */
 589     private void readObject(ObjectInputStream s)
 590         throws IOException, ClassNotFoundException {
 591         NetworkInterface scope_ifname = null;
 592 
 593         if (getClass().getClassLoader() != null) {
 594             throw new SecurityException ("invalid address type");
 595         }
 596 
 597         ObjectInputStream.GetField gf = s.readFields();
 598         byte[] ipaddress = (byte[])gf.get("ipaddress", new byte[0]);
 599         int scope_id = gf.get("scope_id", -1);
 600         boolean scope_id_set = gf.get("scope_id_set", false);
 601         boolean scope_ifname_set = gf.get("scope_ifname_set", false);
 602         String ifname = (String)gf.get("ifname", null);
 603 
 604         if (ifname != null && !"".equals (ifname)) {
 605             try {
 606                 scope_ifname = NetworkInterface.getByName(ifname);
 607                 if (scope_ifname == null) {
 608                     /* the interface does not exist on this system, so we clear
 609                      * the scope information completely */
 610                     scope_id_set = false;
 611                     scope_ifname_set = false;
 612                     scope_id = 0;
 613                 } else {
 614                     scope_ifname_set = true;
 615                     try {
 616                         scope_id = deriveNumericScope (ipaddress, scope_ifname);
 617                     } catch (UnknownHostException e) {
 618                         // typically should not happen, but it may be that
 619                         // the machine being used for deserialization has
 620                         // the same interface name but without IPv6 configured.
 621                     }
 622                 }
 623             } catch (SocketException e) {}
 624         }




 416      * type specified in {@code addr}. The call will fail with an
 417      * UnknownHostException if the given interface does not have a numeric
 418      * scope_id assigned for the given address type (eg. link-local or site-local).
 419      * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
 420      * scoped addresses.
 421      *
 422      * @param host the specified host
 423      * @param addr the raw IP address in network byte order
 424      * @param nif an interface this address must be associated with.
 425      * @return  an Inet6Address object created from the raw IP address.
 426      * @throws  UnknownHostException
 427      *          if IP address is of illegal length, or if the interface does not
 428      *          have a numeric scope_id assigned for the given address type.
 429      *
 430      * @since 1.5
 431      */
 432     public static Inet6Address getByAddress(String host, byte[] addr,
 433                                             NetworkInterface nif)
 434         throws UnknownHostException
 435     {
 436         if (host != null && !host.isEmpty() && host.charAt(0) == '[') {
 437             if (host.charAt(host.length()-1) == ']') {
 438                 host = host.substring(1, host.length() -1);
 439             }
 440         }
 441         if (addr != null) {
 442             if (addr.length == Inet6Address.INADDRSZ) {
 443                 return new Inet6Address(host, addr, nif);
 444             }
 445         }
 446         throw new UnknownHostException("addr is of illegal length");
 447     }
 448 
 449     /**
 450      * Create an Inet6Address in the exact manner of {@link
 451      * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is
 452      * set to the given numeric value. The scope_id is not checked to determine
 453      * if it corresponds to any interface on the system.
 454      * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
 455      * scoped addresses.
 456      *
 457      * @param host the specified host
 458      * @param addr the raw IP address in network byte order
 459      * @param scope_id the numeric scope_id for the address.
 460      * @return  an Inet6Address object created from the raw IP address.
 461      * @throws  UnknownHostException  if IP address is of illegal length.
 462      *
 463      * @since 1.5
 464      */
 465     public static Inet6Address getByAddress(String host, byte[] addr,
 466                                             int scope_id)
 467         throws UnknownHostException
 468     {
 469         if (host != null && !host.isEmpty() && host.charAt(0) == '[') {
 470             if (host.charAt(host.length()-1) == ']') {
 471                 host = host.substring(1, host.length() -1);
 472             }
 473         }
 474         if (addr != null) {
 475             if (addr.length == Inet6Address.INADDRSZ) {
 476                 return new Inet6Address(host, addr, scope_id);
 477             }
 478         }
 479         throw new UnknownHostException("addr is of illegal length");
 480     }
 481 
 482     private void initstr(String hostName, byte addr[], String ifname)
 483         throws UnknownHostException
 484     {
 485         try {
 486             NetworkInterface nif = NetworkInterface.getByName (ifname);
 487             if (nif == null) {
 488                 throw new UnknownHostException ("no such interface " + ifname);
 489             }


 584     /**
 585      * restore the state of this object from stream
 586      * including the scope information, only if the
 587      * scoped interface name is valid on this system
 588      */
 589     private void readObject(ObjectInputStream s)
 590         throws IOException, ClassNotFoundException {
 591         NetworkInterface scope_ifname = null;
 592 
 593         if (getClass().getClassLoader() != null) {
 594             throw new SecurityException ("invalid address type");
 595         }
 596 
 597         ObjectInputStream.GetField gf = s.readFields();
 598         byte[] ipaddress = (byte[])gf.get("ipaddress", new byte[0]);
 599         int scope_id = gf.get("scope_id", -1);
 600         boolean scope_id_set = gf.get("scope_id_set", false);
 601         boolean scope_ifname_set = gf.get("scope_ifname_set", false);
 602         String ifname = (String)gf.get("ifname", null);
 603 
 604         if (ifname != null && !ifname.isEmpty()) {
 605             try {
 606                 scope_ifname = NetworkInterface.getByName(ifname);
 607                 if (scope_ifname == null) {
 608                     /* the interface does not exist on this system, so we clear
 609                      * the scope information completely */
 610                     scope_id_set = false;
 611                     scope_ifname_set = false;
 612                     scope_id = 0;
 613                 } else {
 614                     scope_ifname_set = true;
 615                     try {
 616                         scope_id = deriveNumericScope (ipaddress, scope_ifname);
 617                     } catch (UnknownHostException e) {
 618                         // typically should not happen, but it may be that
 619                         // the machine being used for deserialization has
 620                         // the same interface name but without IPv6 configured.
 621                     }
 622                 }
 623             } catch (SocketException e) {}
 624         }


< prev index next >