static String rewriteIPv4MappedNotation ( String string ) { if ( ! string . contains ( "." ) ) { return string ; } else { int lastColon = string . lastIndexOf ( ":" ) ; String firstPart = string . substring ( 0 , lastColon + 1 ) ; String mappedIPv4Part = string . substring ( lastColon + 1 ) ; if ( mappedIPv4Part . contains ( "." ) ) { String [ ] dotSplits = DOT_DELIM . split ( mappedIPv4Part ) ; if ( dotSplits . length != 4 ) throw new IllegalArgumentException ( String . format ( "can not parse [%s]" , string ) ) ; StringBuilder rewrittenString = new StringBuilder ( ) ; rewrittenString . append ( firstPart ) ; int byteZero = Integer . parseInt ( dotSplits [ 0 ] ) ; int byteOne = Integer . parseInt ( dotSplits [ 1 ] ) ; int byteTwo = Integer . parseInt ( dotSplits [ 2 ] ) ; int byteThree = Integer . parseInt ( dotSplits [ 3 ] ) ; rewrittenString . append ( String . format ( "%02x" , byteZero ) ) ; rewrittenString . append ( String . format ( "%02x" , byteOne ) ) ; rewrittenString . append ( ":" ) ; rewrittenString . append ( String . format ( "%02x" , byteTwo ) ) ; rewrittenString . append ( String . format ( "%02x" , byteThree ) ) ; return rewrittenString . toString ( ) ; } else { throw new IllegalArgumentException ( String . format ( "can not parse [%s]" , string ) ) ; } } }
Replaces a w . x . y . z substring at the end of the given string with corresponding hexadecimal notation . This is useful in case the string was using IPv4 - Mapped address notation .