@ Override public IPAddress toAddress ( ) throws UnknownHostException , HostNameException { IPAddress addr = resolvedAddress ; if ( addr == null && ! resolvedIsNull ) { //note that validation handles empty address resolution validate ( ) ; synchronized ( this ) { addr = resolvedAddress ; if ( addr == null && ! resolvedIsNull ) { if ( parsedHost . isAddressString ( ) ) { addr = parsedHost . asAddress ( ) ; resolvedIsNull = ( addr == null ) ; //note there is no need to apply prefix or mask here, it would have been applied to the address already } else { String strHost = parsedHost . getHost ( ) ; if ( strHost . length ( ) == 0 && ! validationOptions . emptyIsLoopback ) { addr = null ; resolvedIsNull = true ; } else { //Note we do not set resolvedIsNull, so we will attempt to resolve again if the previous attempt threw an exception InetAddress inetAddress = InetAddress . getByName ( strHost ) ; byte bytes [ ] = inetAddress . getAddress ( ) ; Integer networkPrefixLength = parsedHost . getNetworkPrefixLength ( ) ; if ( networkPrefixLength == null ) { IPAddress mask = parsedHost . getMask ( ) ; if ( mask != null ) { byte maskBytes [ ] = mask . getBytes ( ) ; if ( maskBytes . length != bytes . length ) { throw new HostNameException ( host , "ipaddress.error.ipMismatch" ) ; } for ( int i = 0 ; i < bytes . length ; i ++ ) { bytes [ i ] &= maskBytes [ i ] ; } networkPrefixLength = mask . getBlockMaskPrefixLength ( true ) ; } } IPAddressStringParameters addressParams = validationOptions . addressOptions ; if ( bytes . length == IPv6Address . BYTE_COUNT ) { IPv6AddressCreator creator = addressParams . getIPv6Parameters ( ) . getNetwork ( ) . getAddressCreator ( ) ; addr = creator . createAddressInternal ( bytes , networkPrefixLength , null , this ) ; /* address creation */ } else { IPv4AddressCreator creator = addressParams . getIPv4Parameters ( ) . getNetwork ( ) . getAddressCreator ( ) ; addr = creator . createAddressInternal ( bytes , networkPrefixLength , this ) ; /* address creation */ } } } resolvedAddress = addr ; } } } return addr ; }
If this represents an ip address returns that address . If this represents a host returns the resolved ip address of that host . Otherwise returns null but only for strings that are considered valid address strings but cannot be converted to address objects .