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:com.connectsdk.core.Util.java

public static boolean isIPv4Address(String ipAddress) {
    return InetAddressUtils.isIPv4Address(ipAddress);
}

From source file:org.mariotaku.twidere.util.net.HostResolvedSocketFactory.java

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
    final String resolvedHost = resolver.resolve(host);
    if (resolvedHost != null && !resolvedHost.equals(host)) {
        if (InetAddressUtils.isIPv6Address(resolvedHost)) {
            final byte[] resolvedAddress = Inet6Address.getByName(resolvedHost).getAddress();
            return new Socket(InetAddress.getByAddress(host, resolvedAddress), port);
        } else if (InetAddressUtils.isIPv4Address(resolvedHost)) {
            final byte[] resolvedAddress = Inet4Address.getByName(resolvedHost).getAddress();
            return new Socket(InetAddress.getByAddress(host, resolvedAddress), port);
        }/*w ww  .j a  v a2  s. c om*/
    }
    return defaultFactory.createSocket(host, port, localHost, localPort);
}

From source file:to.sven.androidrccar.host.communication.impl.SocketConnector.java

/**
 * Returns the first non-local IPv4 address of the device. 
 * @return IPv4 address as String or unknown, if no address is found.
 *//*from  ww w. ja v  a 2s  .com*/
private String getLocalIpAddress() {
    try {
        for (NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
            for (InetAddress address : Collections.list(iface.getInetAddresses())) {
                if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(address.getHostAddress())) {
                    return address.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return dc.getContext().getString(android.R.string.unknownName);
}

From source file:com.frame.network.utils.NetworkUtil.java

public static String getLocalIpv4Address() {
    try {//from w w w  . j  av  a 2 s.  com
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return null;
}

From source file:org.apache.tajo.validation.NetworkAddressValidator.java

@Override
protected <T> boolean validateInternal(T object) {
    boolean result = false;

    if (object != null) {
        if (object instanceof CharSequence) {
            String valueString = object.toString();
            if (valueString.isEmpty()) {
                result = true;/*www . j  a  v a 2  s . co  m*/
            } else {
                int separatorIdx = valueString.indexOf(':');
                String hostOrIpAddress = null;

                if (separatorIdx > -1) {
                    if (valueString.indexOf(':', separatorIdx + 1) > -1) {
                        // it is IPV6 representation.
                        int leftBracketsIdx = valueString.indexOf('[');
                        int rightBracketsIdx = valueString.indexOf(']');
                        int periodIdx = valueString.indexOf('.');

                        if ((leftBracketsIdx > -1) && (rightBracketsIdx > -1)
                                && (valueString.length() > (rightBracketsIdx + 1))
                                && valueString.charAt(rightBracketsIdx + 1) == ':') {
                            hostOrIpAddress = valueString.substring(leftBracketsIdx + 1, rightBracketsIdx);
                            separatorIdx = rightBracketsIdx + 1;
                        } else if ((periodIdx > -1)) {
                            hostOrIpAddress = valueString.substring(0, periodIdx);
                            separatorIdx = periodIdx;
                        } else {
                            separatorIdx = valueString.lastIndexOf(':');
                            hostOrIpAddress = valueString.substring(0, separatorIdx);
                        }
                    } else {
                        hostOrIpAddress = valueString.substring(0, separatorIdx);
                    }
                } else {
                    hostOrIpAddress = valueString;
                }

                result = ((hostnamePattern.matcher(hostOrIpAddress).find())
                        | InetAddressUtils.isIPv4Address(hostOrIpAddress)
                        | InetAddressUtils.isIPv6Address(hostOrIpAddress));

                if (separatorIdx > -1) {
                    result &= portNumberPattern.matcher(valueString.substring(separatorIdx + 1)).find();
                }
            }
        }
    } else {
        result = true;
    }

    return result;
}

From source file:com.smartdevicelink.tcpdiscovery.DiscoveredDevice.java

private String getLocalAddressForIface(String ifaceName) throws Exception {
    NetworkInterface iface = NetworkInterface.getByName(ifaceName);
    Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
        if (InetAddressUtils.isIPv4Address(inetAddress.toString())) {
            return inetAddress.toString();
        }/*from w  w  w.ja  v  a2  s .  com*/
    }
    return "";
}

From source file:org.mariotaku.twidere.util.net.HostResolvedSocketFactory.java

@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
    final String hostName = host.getHostName();
    final String resolvedHost = resolver.resolve(hostName);
    if (resolvedHost != null && !resolvedHost.equals(hostName)) {
        if (InetAddressUtils.isIPv6Address(resolvedHost)) {
            final byte[] resolvedAddress = Inet6Address.getByName(resolvedHost).getAddress();
            return new Socket(InetAddress.getByAddress(hostName, resolvedAddress), port);
        } else if (InetAddressUtils.isIPv4Address(resolvedHost)) {
            final byte[] resolvedAddress = Inet4Address.getByName(resolvedHost).getAddress();
            return new Socket(InetAddress.getByAddress(hostName, resolvedAddress), port);
        }//from   w  ww  . j  a  v a 2s .  co  m
    }
    return defaultFactory.createSocket(host, port);
}

From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java

@SuppressWarnings("deprecation")
private String readWifiIP4Address() {
    String ip4Address = "0.0.0.0";
    byte[] ipAddress = BigInteger.valueOf(mWifiManager.getConnectionInfo().getIpAddress()).toByteArray();
    try {//from  ww  w.ja  va  2  s.co m
        InetAddress address = InetAddress.getByAddress(ipAddress);
        String concreteAddressString = address.getHostAddress().toUpperCase(Locale.getDefault());
        if (InetAddressUtils.isIPv4Address(concreteAddressString)) {
            ip4Address = Formatter.formatIpAddress(mWifiManager.getConnectionInfo().getIpAddress());
        }
    } catch (UnknownHostException e) {
        return ip4Address;
    }
    return ip4Address;
}

From source file:com.motelabs.chromemote.bridge.MainActivity.java

private String getLocalIpAddress() {
    try {//from ww w. ja va 2 s.c  om
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                    String ipAddr = inetAddress.getHostAddress();
                    return ipAddr;
                }
            }
        }
    } catch (SocketException ex) {
        //Log.d(TAG, ex.toString());
    }
    return null;
}

From source file:com.jean.farCam.SettingsActivity.java

public static String getIPAddress() {
    try {//from   www.  j av  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 sAddr = addr.getHostAddress().toUpperCase();
                    if (InetAddressUtils.isIPv4Address(sAddr)) {
                        return sAddr;
                    }
                }
            }
        }
    } catch (Exception ex) {
    }
    return "";
}