jdk/src/java.base/share/classes/java/net/Inet4Address.java
changeset 42474 6b0beb616950
parent 40118 f63c761ec2d2
child 44844 b2b4d98404ba
equal deleted inserted replaced
42473:b5671137e3fb 42474:6b0beb616950
   155      * Utility routine to check if the InetAddress is an
   155      * Utility routine to check if the InetAddress is an
   156      * IP multicast address. IP multicast address is a Class D
   156      * IP multicast address. IP multicast address is a Class D
   157      * address i.e first four bits of the address are 1110.
   157      * address i.e first four bits of the address are 1110.
   158      * @return a {@code boolean} indicating if the InetAddress is
   158      * @return a {@code boolean} indicating if the InetAddress is
   159      * an IP multicast address
   159      * an IP multicast address
   160      * @since   1.1
       
   161      */
   160      */
   162     public boolean isMulticastAddress() {
   161     public boolean isMulticastAddress() {
   163         return ((holder().getAddress() & 0xf0000000) == 0xe0000000);
   162         return ((holder().getAddress() & 0xf0000000) == 0xe0000000);
   164     }
   163     }
   165 
   164 
   166     /**
   165     /**
   167      * Utility routine to check if the InetAddress is a wildcard address.
   166      * Utility routine to check if the InetAddress is a wildcard address.
   168      * @return a {@code boolean} indicating if the Inetaddress is
   167      * @return a {@code boolean} indicating if the Inetaddress is
   169      *         a wildcard address.
   168      *         a wildcard address.
   170      * @since 1.4
       
   171      */
   169      */
   172     public boolean isAnyLocalAddress() {
   170     public boolean isAnyLocalAddress() {
   173         return holder().getAddress() == 0;
   171         return holder().getAddress() == 0;
   174     }
   172     }
   175 
   173 
   176     /**
   174     /**
   177      * Utility routine to check if the InetAddress is a loopback address.
   175      * Utility routine to check if the InetAddress is a loopback address.
   178      *
   176      *
   179      * @return a {@code boolean} indicating if the InetAddress is
   177      * @return a {@code boolean} indicating if the InetAddress is
   180      * a loopback address; or false otherwise.
   178      * a loopback address; or false otherwise.
   181      * @since 1.4
       
   182      */
   179      */
   183     public boolean isLoopbackAddress() {
   180     public boolean isLoopbackAddress() {
   184         /* 127.x.x.x */
   181         /* 127.x.x.x */
   185         byte[] byteAddr = getAddress();
   182         byte[] byteAddr = getAddress();
   186         return byteAddr[0] == 127;
   183         return byteAddr[0] == 127;
   189     /**
   186     /**
   190      * Utility routine to check if the InetAddress is an link local address.
   187      * Utility routine to check if the InetAddress is an link local address.
   191      *
   188      *
   192      * @return a {@code boolean} indicating if the InetAddress is
   189      * @return a {@code boolean} indicating if the InetAddress is
   193      * a link local address; or false if address is not a link local unicast address.
   190      * a link local address; or false if address is not a link local unicast address.
   194      * @since 1.4
       
   195      */
   191      */
   196     public boolean isLinkLocalAddress() {
   192     public boolean isLinkLocalAddress() {
   197         // link-local unicast in IPv4 (169.254.0.0/16)
   193         // link-local unicast in IPv4 (169.254.0.0/16)
   198         // defined in "Documenting Special Use IPv4 Address Blocks
   194         // defined in "Documenting Special Use IPv4 Address Blocks
   199         // that have been Registered with IANA" by Bill Manning
   195         // that have been Registered with IANA" by Bill Manning
   206     /**
   202     /**
   207      * Utility routine to check if the InetAddress is a site local address.
   203      * Utility routine to check if the InetAddress is a site local address.
   208      *
   204      *
   209      * @return a {@code boolean} indicating if the InetAddress is
   205      * @return a {@code boolean} indicating if the InetAddress is
   210      * a site local address; or false if address is not a site local unicast address.
   206      * a site local address; or false if address is not a site local unicast address.
   211      * @since 1.4
       
   212      */
   207      */
   213     public boolean isSiteLocalAddress() {
   208     public boolean isSiteLocalAddress() {
   214         // refer to RFC 1918
   209         // refer to RFC 1918
   215         // 10/8 prefix
   210         // 10/8 prefix
   216         // 172.16/12 prefix
   211         // 172.16/12 prefix
   227      * Utility routine to check if the multicast address has global scope.
   222      * Utility routine to check if the multicast address has global scope.
   228      *
   223      *
   229      * @return a {@code boolean} indicating if the address has
   224      * @return a {@code boolean} indicating if the address has
   230      *         is a multicast address of global scope, false if it is not
   225      *         is a multicast address of global scope, false if it is not
   231      *         of global scope or it is not a multicast address
   226      *         of global scope or it is not a multicast address
   232      * @since 1.4
       
   233      */
   227      */
   234     public boolean isMCGlobal() {
   228     public boolean isMCGlobal() {
   235         // 224.0.1.0 to 238.255.255.255
   229         // 224.0.1.0 to 238.255.255.255
   236         byte[] byteAddr = getAddress();
   230         byte[] byteAddr = getAddress();
   237         return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) &&
   231         return ((byteAddr[0] & 0xff) >= 224 && (byteAddr[0] & 0xff) <= 238 ) &&
   243      * Utility routine to check if the multicast address has node scope.
   237      * Utility routine to check if the multicast address has node scope.
   244      *
   238      *
   245      * @return a {@code boolean} indicating if the address has
   239      * @return a {@code boolean} indicating if the address has
   246      *         is a multicast address of node-local scope, false if it is not
   240      *         is a multicast address of node-local scope, false if it is not
   247      *         of node-local scope or it is not a multicast address
   241      *         of node-local scope or it is not a multicast address
   248      * @since 1.4
       
   249      */
   242      */
   250     public boolean isMCNodeLocal() {
   243     public boolean isMCNodeLocal() {
   251         // unless ttl == 0
   244         // unless ttl == 0
   252         return false;
   245         return false;
   253     }
   246     }
   256      * Utility routine to check if the multicast address has link scope.
   249      * Utility routine to check if the multicast address has link scope.
   257      *
   250      *
   258      * @return a {@code boolean} indicating if the address has
   251      * @return a {@code boolean} indicating if the address has
   259      *         is a multicast address of link-local scope, false if it is not
   252      *         is a multicast address of link-local scope, false if it is not
   260      *         of link-local scope or it is not a multicast address
   253      *         of link-local scope or it is not a multicast address
   261      * @since 1.4
       
   262      */
   254      */
   263     public boolean isMCLinkLocal() {
   255     public boolean isMCLinkLocal() {
   264         // 224.0.0/24 prefix and ttl == 1
   256         // 224.0.0/24 prefix and ttl == 1
   265         int address = holder().getAddress();
   257         int address = holder().getAddress();
   266         return (((address >>> 24) & 0xFF) == 224)
   258         return (((address >>> 24) & 0xFF) == 224)
   272      * Utility routine to check if the multicast address has site scope.
   264      * Utility routine to check if the multicast address has site scope.
   273      *
   265      *
   274      * @return a {@code boolean} indicating if the address has
   266      * @return a {@code boolean} indicating if the address has
   275      *         is a multicast address of site-local scope, false if it is not
   267      *         is a multicast address of site-local scope, false if it is not
   276      *         of site-local scope or it is not a multicast address
   268      *         of site-local scope or it is not a multicast address
   277      * @since 1.4
       
   278      */
   269      */
   279     public boolean isMCSiteLocal() {
   270     public boolean isMCSiteLocal() {
   280         // 239.255/16 prefix or ttl < 32
   271         // 239.255/16 prefix or ttl < 32
   281         int address = holder().getAddress();
   272         int address = holder().getAddress();
   282         return (((address >>> 24) & 0xFF) == 239)
   273         return (((address >>> 24) & 0xFF) == 239)
   288      *
   279      *
   289      * @return a {@code boolean} indicating if the address has
   280      * @return a {@code boolean} indicating if the address has
   290      *         is a multicast address of organization-local scope,
   281      *         is a multicast address of organization-local scope,
   291      *         false if it is not of organization-local scope
   282      *         false if it is not of organization-local scope
   292      *         or it is not a multicast address
   283      *         or it is not a multicast address
   293      * @since 1.4
       
   294      */
   284      */
   295     public boolean isMCOrgLocal() {
   285     public boolean isMCOrgLocal() {
   296         // 239.192 - 239.195
   286         // 239.192 - 239.195
   297         int address = holder().getAddress();
   287         int address = holder().getAddress();
   298         return (((address >>> 24) & 0xFF) == 239)
   288         return (((address >>> 24) & 0xFF) == 239)
   320 
   310 
   321     /**
   311     /**
   322      * Returns the IP address string in textual presentation form.
   312      * Returns the IP address string in textual presentation form.
   323      *
   313      *
   324      * @return  the raw IP address in a string format.
   314      * @return  the raw IP address in a string format.
   325      * @since   1.0.2
       
   326      */
   315      */
   327     public String getHostAddress() {
   316     public String getHostAddress() {
   328         return numericToTextFormat(getAddress());
   317         return numericToTextFormat(getAddress());
   329     }
   318     }
   330 
   319 
   357         return (obj != null) && (obj instanceof Inet4Address) &&
   346         return (obj != null) && (obj instanceof Inet4Address) &&
   358             (((InetAddress)obj).holder().getAddress() == holder().getAddress());
   347             (((InetAddress)obj).holder().getAddress() == holder().getAddress());
   359     }
   348     }
   360 
   349 
   361     // Utilities
   350     // Utilities
   362     /*
   351 
       
   352     /**
   363      * Converts IPv4 binary address into a string suitable for presentation.
   353      * Converts IPv4 binary address into a string suitable for presentation.
   364      *
   354      *
   365      * @param src a byte array representing an IPv4 numeric address
   355      * @param src a byte array representing an IPv4 numeric address
   366      * @return a String representing the IPv4 address in
   356      * @return a String representing the IPv4 address in
   367      *         textual representation format
   357      *         textual representation format
   368      * @since 1.4
   358      */
   369      */
       
   370 
       
   371     static String numericToTextFormat(byte[] src)
   359     static String numericToTextFormat(byte[] src)
   372     {
   360     {
   373         return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
   361         return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
   374     }
   362     }
   375 
   363