Java Socket Address Get parseSocketAddress(String addressPort, int defaultPort)

Here you can find the source of parseSocketAddress(String addressPort, int defaultPort)

Description

parse Socket Address

License

Open Source License

Declaration

public static InetSocketAddress parseSocketAddress(String addressPort, int defaultPort)
            throws UnknownHostException 

Method Source Code


//package com.java2s;
/*//from   w w  w  . j a  v a2 s .co m
 * JBoss, Home of Professional Open Source.
 * Copyright 2009, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

public class Main {
    public static InetSocketAddress parseSocketAddress(String addressPort, int defaultPort)
            throws UnknownHostException {
        String address = addressPort;
        int port = defaultPort;

        int lastColon = (addressPort != null) ? addressPort.lastIndexOf(":") : -1;

        if (lastColon >= 0) {
            if (addressPort.indexOf(":") == lastColon) {
                // Handle ipv4 address
                address = addressPort.substring(0, lastColon);
                port = Integer.parseInt(addressPort.substring(lastColon + 1));
            } else { // handle ipv6 address
                // Detect url-style: [ipv6-address]:port
                int openBracket = addressPort.indexOf("[");
                int closeBracket = addressPort.indexOf("]");

                if ((openBracket >= 0) && (closeBracket >= 0)) {
                    address = addressPort.substring(openBracket + 1, closeBracket);

                    if (closeBracket < lastColon) {
                        port = Integer.parseInt(addressPort.substring(lastColon + 1));
                    }
                }
            }
        }

        return new InetSocketAddress((address != null) && (address.length() > 0) ? InetAddress.getByName(address)
                : InetAddress.getLocalHost(), port);
    }
}

Related

  1. parseAddress(SocketAddress address)
  2. parseInetSocketAddress(String address)
  3. parseInetSocketAddress(String endPoint)
  4. parseInetSocketAddress(String text)
  5. parseSocketAddress(String address)
  6. parseSocketAddress(String addrString)
  7. parseSocketAddress(String anAddress)
  8. parseSocketAddress(String socketAddress)
  9. parseSocketAddressAddr(SocketAddress socketAddress)