Example usage for org.apache.http.conn.util InetAddressUtils isIPv4Address

List of usage examples for org.apache.http.conn.util InetAddressUtils isIPv4Address

Introduction

In this page you can find the example usage for org.apache.http.conn.util InetAddressUtils isIPv4Address.

Prototype

public static boolean isIPv4Address(final String input) 

Source Link

Document

Checks whether the parameter is a valid IPv4 address

Usage

From source file:Main.java

static public boolean isValidIpAddress(String ip) {
    boolean v4 = InetAddressUtils.isIPv4Address(ip);
    boolean v6 = InetAddressUtils.isIPv6Address(ip);
    if (!v4 && !v6)
        return false;
    try {/*from   ww  w  .j a  v  a2s  .co m*/
        InetAddress inet = InetAddress.getByName(ip);
        return inet.isLinkLocalAddress() || inet.isSiteLocalAddress();
    } catch (UnknownHostException e) {
        //Log.e(TAG, e.toString());
        return false;
    }
}

From source file:Main.java

public static String getIP() {
    try {/*from   w w  w .ja v  a 2  s  . co  m*/
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String addr1 = addr.getHostAddress().toUpperCase().replaceAll("^/", "");
                    if (InetAddressUtils.isIPv4Address(addr1)) {
                        return addr1;
                    }
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

@SuppressLint("DefaultLocale")
public static String getLocalHostAddress() {
    boolean useIPv4 = true;

    try {/* w w w .  j  a  v a 2  s .  c o  m*/
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4) {
                            return sAddr;
                        }
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}

From source file:Main.java

public static String getLocalIpAddress(Context context) {
    try {/*w w  w.j a v a2  s.  c om*/
        String ipv4;

        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ni : nilist) {
            if (ni.getName().toLowerCase().contains("usbnet"))
                continue;
            List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
            for (InetAddress address : ialist) {
                if (!address.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) {
                    return ipv4;
                }
            }

        }

    } catch (SocketException ex) {
        Log.d("socket_err", ex.toString());
    }
    return null;
}

From source file:Main.java

/**
 * Get IP address from first non-localhost interface
 * @param ipv4  true=return ipv4, false=return ipv6
 * @return  address or empty string/*from   w  ww .j a v a  2s.  co  m*/
 */
@SuppressLint("DefaultLocale")
public static String getIPAddress() {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (isIPv4)
                        return sAddr;
                }
            }
        }
    } catch (Exception ex) {
    }
    return "";
}

From source file:Main.java

/**
 * Get IP address from first non-localhost interface
 * @param useIPv4  true=return ipv4, false=return ipv6
 * @return  address or empty string//ww  w  .  ja va2s  .c o m
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}

From source file:com.github.christiangda.utils.ip.IP.java

/**
 * @param ip String/*from  w w  w .  j  a v  a  2s .  c o  m*/
 * @return boolean
 */
public static boolean isValidIP(String ip) {

    if (ip == null) {
        return false;
    }

    if (ip.isEmpty()) {
        return false;
    }

    return InetAddressUtils.isIPv4Address(ip) || InetAddressUtils.isIPv6Address(ip);
}

From source file:io.github.lucaseasedup.logit.util.Validators.java

public static boolean validateIp(String ip) {
    if (StringUtils.isBlank(ip))
        return false;

    return InetAddressUtils.isIPv4Address(ip) || InetAddressUtils.isIPv6Address(ip);
}

From source file:com.air.mobilebrowser.NetworkUtil.java

public static String getIPAddress() {
    boolean useIPv4 = true;
    try {//ww  w  . jav  a 2  s  .c o m
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions

    return "";
}

From source file:com.github.christiangda.utils.ip.IP.java

/**
 * @param ip InetAddress//from   w w  w  .  ja va 2  s  .  c  o  m
 * @return boolean
 */
public static boolean isValidIP(InetAddress ip) {
    return ip != null && (InetAddressUtils.isIPv4Address(ip.getHostAddress())
            || InetAddressUtils.isIPv6Address(ip.getHostAddress()));
}