集册 Java实例教程 创建给定字符串数组的InetSocketAddress数组

创建给定字符串数组的InetSocketAddress数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

673
创建给定字符串数组的InetSocketAddress数组

/*

 * Copyright 2014, The Sporting Exchange Limited

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

//package com.nowjava;


import java.net.InetSocketAddress;
/* 
*来 自
 nowjava
*/


public class Main {

    public static void main(String[] argv) throws Exception {

        String[] addresses = new String[] { "1", "abc", "level", null,

                "nowjava.com", "asdf 123" };

        System.out.println(java.util.Arrays

                .toString(parseAddressList(addresses)));

    }


    /**

     * <p>Creates an array of InetSocketAddress given an array of String</p>

     *

     * @param addresses the addresses, which may be a mixture of any of the formats supported by

     *                  createInetSocketAddress(String address)

     */

    public static InetSocketAddress[] parseAddressList(String[] addresses) {

        InetSocketAddress[] isa = new InetSocketAddress[addresses.length];

        for (int x = 0; x < addresses.length; x++) {

            isa[x] = createInetSocketAddress(addresses[x]);

        }/*时代Java公众号 提 供*/

        return isa;

    }


    /**

     * <p>Creates an InetSocketAddress given a host and optional port in a single String</p>

     * <p/>

     * <p>This allows either IP4 or IP6 addresses (including port) to be provided as Strings as per rfc2732</p>

     *

     * @param address the address in one of the following formats (the braces '[]'and colon ':' are literal here):

     *                host<br>

     *                [host]<br>

     *                [host]:port<br>

     *                [host]port<br>

     *                ip4host:port<br>

     *                <p>Assumes port 0 if non is specified.</p>

     *                <p/>

     *                <p> As per java.net.InetSocketAddress

     *                A port number of zero will let the system pick up an ephemeral port in a bind operation.</p>

     */

    public static InetSocketAddress createInetSocketAddress(String address) {

        return createInetSocketAddress(address, 0);

    }


    /**

     * <p>Creates an InetSocketAddress given a host and optional port in a single String

     * <p/>

     * <p>This allows either IP4 or IP6 addresses (including port) to be provided as Strings as per rfc2732</p>

     *

     * @param address     the address in one of the following formats (the braces '[]'and colon ':' are literal here):

     *                    host<br>

     *                    [host]<br>

     *                    [host]:port<br>

     *                    [host]port<br>

     *                    ip4host:port<br>

     * @param defaultPort The default port to be used ONLY IF the string does not specify a port

     * @see java.net.InetSocketAddress

     */

    public static InetSocketAddress createInetSocketAddress(String address,

            int defaultPort) {

        String original = address.trim();

        String host = original;

        int port = defaultPort;


        if (host.startsWith("[")) {

            // it is an address in [host] or [host]port format

            String[] s = original.split("\\]");

            if (s.length > 1) {

                if (s[1].startsWith(":")) {

                    s[1] = s[1].substring(1);

                }

                port = computePort(s[1], 0);

            }

            host = s[0].substring(1);

        }


        if (host.indexOf(":") == host.lastIndexOf(":")

                && (host.indexOf(
展开阅读全文