Java InetAddress Create getAddressInfoFindAll(Function convertValue, Predicate doBreak)

Here you can find the source of getAddressInfoFindAll(Function convertValue, Predicate doBreak)

Description

get Address Info Find All

License

Open Source License

Declaration

private static List<String> getAddressInfoFindAll(Function<InetAddress, String> convertValue,
        Predicate<String> doBreak) throws SocketException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

public class Main {

    private static List<String> getAddressInfoFindAll(Function<InetAddress, String> convertValue,
            Predicate<String> doBreak) throws SocketException {

        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        List<String> findAll = new ArrayList<>();

        END: while (networkInterfaces.hasMoreElements()) {
            NetworkInterface nextElement = networkInterfaces.nextElement();

            if (nextElement.isVirtual())
                continue;

            if (nextElement.isLoopback())
                continue;

            if (!nextElement.isUp())
                continue;

            Enumeration<InetAddress> inetAddresses = nextElement.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress netAddress = inetAddresses.nextElement();

                if (!netAddress.isLoopbackAddress() && !netAddress.isLinkLocalAddress()
                        && netAddress.isSiteLocalAddress()) {
                    String hostAddress = netAddress.getHostAddress();
                    if (hostAddress.startsWith("192.168"))
                        continue;

                    String val = convertValue.apply(netAddress);
                    findAll.add(val);
                    if (doBreak.test(val))
                        break END;
                }//from   w  w  w  .ja  v a 2s .  c  o m
            }
        }

        return findAll;
    }
}

Related

  1. getActiveInterfaceInetAddresses()
  2. getAddressPriority(InetAddress addr)
  3. getAddrInfo(InetAddress pAddr)
  4. getAllInetAddress(final String interfaceName)
  5. getAllIPv4InetAddresses()