code: public static String formatIPAddressForURI(InetAddress inet){
          if(inet == null){
              throw new IllegalArgumentException();
          }
          if(inet instanceof Inet4Address){
              return inet.getHostAddress();
          } else if (inet instanceof Inet6Address){
              return '[' + formatAddress(inet) + ']';
          } else {
              return inet.getHostAddress();
          }
      }
docs: Formats input address. For IPV4 returns simply host address, for IPV6 formats address according to <a
  href="http://tools.ietf.org/html/rfc5952">RFC5952</a> rules. It embeds IPV6 address in '[', ']'.
  
  @param inet
  @return