Java InetAddress Create getInetAddress(final String addressOrName)

Here you can find the source of getInetAddress(final String addressOrName)

Description

get Inet Address

License

Apache License

Declaration

@Nonnull
    public static InetAddress getInetAddress(final String addressOrName) 

Method Source Code

//package com.java2s;
/*/*from   w  ww  .  ja v  a 2  s.  c  o m*/
 * @(#)$Id$$
 *
 * Copyright 2006-2008 Makoto YUI
 *
 * 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.
 * 
 * Contributors:
 *     Makoto YUI - initial implementation
 */

import java.net.*;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class Main {
    @Nonnull
    public static InetAddress getInetAddress(final String addressOrName) {
        if (isIPAddress(addressOrName)) {
            return getInetAddress(addressOrName);
        } else {
            return getInetAddressByName(addressOrName);
        }
    }

    public static boolean isIPAddress(final String ip) {
        return ip.matches(
                "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
    }

    @Nullable
    public static InetAddress getInetAddressByName(final String host) {
        try {
            return InetAddress.getByName(host);
        } catch (UnknownHostException e) {
            throw new IllegalArgumentException("Cannot find InetAddress for host: " + host);
        }
    }
}

Related

  1. getInetAddress()
  2. getInetAddress()
  3. getInetAddress()
  4. getInetAddress()
  5. getInetAddress(final int[] octets, final int offset, final int length)
  6. getInetAddress(long ip)
  7. getInetAddress(Socket socket)
  8. getInetAddress(String dottedNotation)
  9. getInetAddress(String host)