Java Socket Address Get toInetSocketAddressList(String addresses, int defaultPort)

Here you can find the source of toInetSocketAddressList(String addresses, int defaultPort)

Description

Parses a comma separated list of ip/host:port pairs.

License

Apache License

Parameter

Parameter Description
list a parameter
defaultPort a parameter

Declaration

public static List<InetSocketAddress> toInetSocketAddressList(String addresses, int defaultPort) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.InetSocketAddress;

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//from   w w w. jav a  2s  . co m
     * Parses a comma separated list of ip/host:port pairs. If a port isn't
     * specified, the default port parameter is used instead.
     * 
     * @param list
     * @param defaultPort
     * @return
     */
    public static List<InetSocketAddress> toInetSocketAddressList(String addresses, int defaultPort) {
        String[] split = addresses.split(",");
        return toInetSocketAddressList(split, defaultPort);
    }

    public static List<InetSocketAddress> toInetSocketAddressList(String[] hostPortPairs, int defaultPort) {
        List<InetSocketAddress> addressList = new ArrayList<InetSocketAddress>();

        for (String hostPortPair : hostPortPairs) {
            addressList.add(toInetSocketAddress(hostPortPair.trim(), defaultPort));
        }

        return addressList;

    }

    public static InetSocketAddress toInetSocketAddress(String hostPortPair, int defaultPort) {
        String[] split = hostPortPair.split(":");

        String hostname = split[0];
        int port;
        if (split.length > 1) {
            port = Integer.parseInt(split[1]);
        } else {
            port = defaultPort;
        }

        InetSocketAddress inetSocketAddress = new InetSocketAddress(hostname, port);
        return inetSocketAddress;
    }
}

Related

  1. toFileName(InetSocketAddress address)
  2. toHttpUrl(InetSocketAddress addr)
  3. toInetSocketAddress(String address)
  4. toInetSocketAddress(String hostPortPair, int defaultPort)
  5. toInetSocketAddressLIst(List addressList)
  6. toSocketAddress(URL url)
  7. toString(InetSocketAddress addr)
  8. toString(InetSocketAddress entryServer)
  9. toString(InetSocketAddress socketAddress)