public static InetAddress forUriString ( String hostAddr ) { Preconditions . checkNotNull ( hostAddr ) ; // Decide if this should be an IPv6 or IPv4 address. String ipString ; int expectBytes ; if ( hostAddr . startsWith ( "[" ) && hostAddr . endsWith ( "]" ) ) { ipString = hostAddr . substring ( 1 , hostAddr . length ( ) - 1 ) ; expectBytes = 16 ; } else { ipString = hostAddr ; expectBytes = 4 ; } // Parse the address, and make sure the length/version is correct. byte [ ] addr = ipStringToBytes ( ipString ) ; if ( addr == null || addr . length != expectBytes ) { throw new IllegalArgumentException ( String . format ( "Not a valid URI IP literal: '%s'" , hostAddr ) ) ; } return bytesToInetAddress ( addr ) ; }
Returns an InetAddress representing the literal IPv4 or IPv6 host portion of a URL encoded in the format specified by RFC 3986 section 3 . 2 . 2 .