Java Socket Address Get parseInetSocketAddress(String text)

Here you can find the source of parseInetSocketAddress(String text)

Description

parse Inet Socket Address

License

Apache License

Declaration

public static InetSocketAddress parseInetSocketAddress(String text) 

Method Source Code

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

import java.net.InetSocketAddress;

public class Main {
    public static InetSocketAddress parseInetSocketAddress(String text) {
        if (text == null || text.isEmpty()) {
            throw new IllegalArgumentException("Address is empty");
        }//  w w w  .  j  a va 2 s  .c  o m

        final int index = text.lastIndexOf(':');
        if (index == -1 || index == text.length() - 1) {
            throw new IllegalArgumentException("Port is not found in: " + text);
        }
        if (index == 0) {
            throw new IllegalArgumentException("Host is not found in: " + text);
        }

        final String host = text.substring(0, index);

        final String portStr = text.substring(index + 1, text.length());
        final int port;
        try {
            port = Integer.parseInt(portStr);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Port is not integer in address: " + text, e);
        }

        final InetSocketAddress address;
        try {
            address = new InetSocketAddress(host, port);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Failed to parse address: " + text, e);
        }

        return address;
    }
}

Related

  1. isValidMulticastAddress(final InetSocketAddress address)
  2. normalizeSocketAddr(InetSocketAddress address)
  3. parseAddress(SocketAddress address)
  4. parseInetSocketAddress(String address)
  5. parseInetSocketAddress(String endPoint)
  6. parseSocketAddress(String address)
  7. parseSocketAddress(String addressPort, int defaultPort)
  8. parseSocketAddress(String addrString)
  9. parseSocketAddress(String anAddress)