Here you can find the source of format(final SocketAddress s, final int defaultPort)
public static String format(final SocketAddress s, final int defaultPort)
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.net.InetSocketAddress; import java.net.SocketAddress; public class Main { /** Format an address string into {@code host:port} or {@code *:port} syntax. */ public static String format(final SocketAddress s, final int defaultPort) { if (s instanceof InetSocketAddress) { final InetSocketAddress addr = (InetSocketAddress) s; if (addr.getPort() == defaultPort) { return safeHostname(hostname(addr)); }//from w w w . j a va 2 s . c om return format(hostname(addr), addr.getPort()); } return s.toString(); } /** Format an address string into {@code host:port} or {@code *:port} syntax. */ public static String format(String hostname, int port) { return safeHostname(hostname) + ":" + port; } private static String safeHostname(String hostname) { if (0 <= hostname.indexOf(':')) { hostname = "[" + hostname + "]"; } return hostname; } /** Get the name or IP address, or {@code *} if this address is a wildcard IP. */ public static String hostname(final InetSocketAddress addr) { if (addr.getAddress() != null) { if (addr.getAddress().isAnyLocalAddress()) { return "*"; } return addr.getAddress().getHostName(); } return addr.getHostName(); } }