Java Socket Create createSocketAddr(String target)

Here you can find the source of createSocketAddr(String target)

Description

create Socket Addr

License

Apache License

Declaration

public static InetSocketAddress createSocketAddr(String target) throws MalformedURLException 

Method Source Code

//package com.java2s;
/**/*from ww  w .  j  ava2  s .  co m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.net.InetSocketAddress;
import java.net.MalformedURLException;

import java.net.URL;

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<String, String> hostToResolved = new HashMap<String, String>();

    public static InetSocketAddress createSocketAddr(String target) throws MalformedURLException {
        return createSocketAddr(target, 8080);
    }

    public static InetSocketAddress createSocketAddr(String target, int defaultPort) throws MalformedURLException {
        if (target == null) {
            throw new IllegalArgumentException("Target address cannot be null.");
        }
        int colonIndex = target.indexOf(':');
        if (colonIndex < 0 && defaultPort == -1) {
            throw new RuntimeException("Not a host:port pair: " + target);
        }
        String hostname;
        int port = -1;
        if (!target.contains("/")) {
            if (colonIndex == -1) {
                hostname = target;
            } else {
                // must be the old style <host>:<port>
                hostname = target.substring(0, colonIndex);
                port = Integer.parseInt(target.substring(colonIndex + 1));
            }
        } else {
            // a new uri
            URL addr = new URL(target);
            hostname = addr.getHost();
            port = addr.getPort();
        }
        if (port == -1) {
            port = defaultPort;
        }
        if (getStaticResolution(hostname) != null) {
            hostname = getStaticResolution(hostname);
        }
        return new InetSocketAddress(hostname, port);
    }

    public static String getStaticResolution(String host) {
        synchronized (hostToResolved) {
            return hostToResolved.get(host);
        }
    }
}

Related

  1. createSocket(String host, int port)
  2. createSocket(String[] hostPort)
  3. createSocketAddr(String addr)
  4. createSocketAddr(String target)
  5. createSocketAddress()
  6. createSocketAddress(String server)
  7. createSocketAddrForHost(String hostName, int port)