Java Socket Address Get getSocketAddressList(String list, int defaultPort)

Here you can find the source of getSocketAddressList(String list, int defaultPort)

Description

Parse space delimited addresss[:port] string and return array of InetSocketAddress.

License

Open Source License

Parameter

Parameter Description
list space delimited addresss[:port] string.
defaultPort port take if not specified.

Return

array of InetSocketAddress.

Declaration

public static InetSocketAddress[] getSocketAddressList(String list, int defaultPort) 

Method Source Code

//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;
    }
}

Related

  1. getRawAddress(InetSocketAddress inetSocketAddress)
  2. getSimpleIpport(SocketAddress remoteAddr)
  3. getSocketAddress(InetSocketAddress socket)
  4. getSocketAddress(SocketAddress socketAddress)
  5. getSocketAddress(String hostHeader)
  6. getSocketConnectString( InetSocketAddress socketAddress)
  7. inetAddrToHoman(final InetSocketAddress sockAddr)
  8. inetSocketAddress2IpPort(InetSocketAddress addr)
  9. IpToString(InetSocketAddress address)