Here you can find the source of getSocketAddressList(String list, int defaultPort)
InetSocketAddress
.
Parameter | Description |
---|---|
list | space delimited addresss[:port] string. |
defaultPort | port take if not specified. |
InetSocketAddress
.
public static InetSocketAddress[] getSocketAddressList(String list, int defaultPort)
//package com.java2s; //License from project: Open Source License import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.StringTokenizer; public class Main { /**//from ww w.j a v a 2 s. co m * Parse space delimited addresss[:port] string and return array of <code>InetSocketAddress</code>. * @param list space delimited addresss[:port] string. * @param defaultPort port take if not specified. * @return array of <code>InetSocketAddress</code>. */ public static InetSocketAddress[] getSocketAddressList(String list, int defaultPort) { return getSocketAddressList(list, defaultPort, null); } /** * Parse space delimited address[:port] string and return array of <code>InetSocketAddress</code>. * @param list space delimited address[:port] string. * @param defaultPort port take if not specified. * @param appendList list to be appended. * @return array of <code>InetSocketAddress</code>. */ public static InetSocketAddress[] getSocketAddressList(String list, int defaultPort, InetSocketAddress[] appendList) { ArrayList<InetSocketAddress> al = new ArrayList<InetSocketAddress>(); // parse string StringTokenizer st = new StringTokenizer(list); while (st.hasMoreTokens()) { int port = defaultPort; String address = st.nextToken(); // check port int pos = address.indexOf(':'); if (pos >= 0) { try { port = Integer.parseInt(address.substring(pos + 1)); } catch (NumberFormatException nfe) { /* noop */ } address = address.substring(0, pos); } // add parsed address al.add(new InetSocketAddress(address, port)); } // copy to array int appendSize = (appendList == null) ? 0 : appendList.length; InetSocketAddress[] isar = new InetSocketAddress[al.size() + appendSize]; al.toArray(isar); if (appendSize > 0) System.arraycopy(appendList, 0, isar, al.size(), appendSize); return isar; } }