Java Socket Address Get getInetSocketAddress(String addressPortStr, int defaultPort)

Here you can find the source of getInetSocketAddress(String addressPortStr, int defaultPort)

Description

Get an InetSocketAddress from the String format "
:", or "
".

License

Open Source License

Parameter

Parameter Description
addressPortStr String for Address and Port. Enclose with "[" and "]" for IPv6 addresses.
defaultPort defaultPort when port is not specified in addressPortStr

Exception

Parameter Description
UnknownHostException an exception

Return

InetSocketAddress

Declaration

public static InetSocketAddress getInetSocketAddress(String addressPortStr, int defaultPort)
        throws UnknownHostException 

Method Source Code

//package com.java2s;
/*/*www. j av  a  2  s .  c  om*/
 * Copyright Ericsson AB 2011-2014. All Rights Reserved.
 *
 * The contents of this file are subject to the Lesser GNU Public License,
 *  (the "License"), either version 2.1 of the License, or
 * (at your option) any later version.; you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * License along with this software. If not, it can be
 * retrieved online at https://www.gnu.org/licenses/lgpl.html. Moreover
 * it could also be requested from Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
 * WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
 * EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
 * OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND,
    
 * EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
 * LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE,
 * YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
 *
 * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
 * WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
 * REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
 * DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL
 * DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY
 * (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
 * INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE
 * OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH
 * HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 */

import java.net.InetSocketAddress;

import java.net.UnknownHostException;

public class Main {
    /**
     * Get an InetSocketAddress from the String format "<Address>:<Port>", or
     * "<Address>".
     *
     * @param addressPortStr String for Address and Port. Enclose with "[" and
     * "]" for IPv6 addresses.
     * @param defaultPort defaultPort when port is not specified in
     * addressPortStr
     * @return InetSocketAddress
     * @throws UnknownHostException
     */
    public static InetSocketAddress getInetSocketAddress(String addressPortStr, int defaultPort)
            throws UnknownHostException {
        if (addressPortStr == null || addressPortStr.isEmpty()) {
            return null;
        }
        String addressStr;
        int port;
        if (addressPortStr.charAt(0) == '[') {
            // [2001:1::1]:1234 or [2001:1::1]
            int i = addressPortStr.indexOf(']');
            if (i <= 0) {
                return null;
            }
            addressStr = addressPortStr.substring(1, i);
            if (addressPortStr.length() > i + 2 && addressPortStr.charAt(i + 1) == ':') {
                // [2001:1::1]:1234
                port = Integer.parseInt(addressPortStr.substring(i + 2));
            } else if (addressPortStr.length() == i + 1) {
                // [2001:1::1] (is it legitimate??)
                port = defaultPort;
            } else {
                return null;
            }
        } else if (addressPortStr.contains(":")) {
            int i = addressPortStr.lastIndexOf(':');
            if (addressPortStr.indexOf(':') == i) {
                // www.fqdn.com:1234 or 192.168.1.1:1234
                addressStr = addressPortStr.substring(0, i);
                port = Integer.parseInt(addressPortStr.substring(i + 1));
            } else {
                // IPv6 Address 2001:1::1 (at least including two ':')
                addressStr = addressPortStr;
                port = defaultPort;
            }
        } else {
            // www.fqdn.com or 192.168.1.1
            addressStr = addressPortStr;
            port = defaultPort;
        }
        if (addressStr == null || port < 0) {
            return null;
        }
        return new InetSocketAddress(addressStr, port);
    }
}

Related

  1. getHostString(InetSocketAddress socketAddress)
  2. getHostString(InetSocketAddress socketAddress)
  3. getHostStringFromInetSocketAddress(InetSocketAddress addr)
  4. getHostStringWithoutNameLookup( InetSocketAddress inetSocketAddress)
  5. getInetSocketAddress(long addrAsInt32, int port)
  6. getInetSocketAddress(String arg)
  7. getInetSocketAddress(String endpoint)
  8. getInetSocketAddress(String endpointURI, int defaultPort)
  9. getInetSocketAddress(String s, String mainHost)