Java Collections.list Examples

Java Collections.list - 30 examples found. These are the top rated real world Java examples of java.util.Collections.list extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  public static String getIPAddress(boolean useIPv4) throws SocketException {
    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) {
              if (sAddr.startsWith("fe80")
                  || sAddr.startsWith("FE80")) // skipping link-local addresses
              continue;
              int delim = sAddr.indexOf('%'); // drop ip6 port suffix
              return delim < 0 ? sAddr : sAddr.substring(0, delim);
            }
          }
        }
      }
    }

    return "";
  }
Example #2
0
  /**
   * Prefer something other than loopback.
   *
   * @return
   * @throws SocketException
   * @throws UnknownHostException
   */
  public static InetAddress getInetAddress() throws SocketException, UnknownHostException {

    // try to use eth0 if found
    NetworkInterface eth0 = NetworkInterface.getByName("eth0");
    if (eth0 != null) {
      Enumeration<InetAddress> addresses = eth0.getInetAddresses();
      for (InetAddress address : Collections.list(addresses)) {
        if (address instanceof Inet4Address) {
          return address;
        }
      }
    }

    // something other than eth0 or loopback
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface iface : Collections.list(interfaces)) {
      Enumeration<InetAddress> addresses = iface.getInetAddresses();
      for (InetAddress address : Collections.list(addresses)) {
        if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
          return address;
        }
      }
    }

    // default, probably loopback
    return InetAddress.getLocalHost();
  }
Example #3
0
  public static String getLocalIpAddress2() {
    String networkIp = null;
    try {
      List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
      for (NetworkInterface iface : interfaces) {
        if (iface.getDisplayName().equals("eth0")) {
          List<InetAddress> addresses = Collections.list(iface.getInetAddresses());
          for (InetAddress address : addresses) {
            if (address instanceof Inet4Address) {
              networkIp = address.getHostAddress();
            }
          }
        } else if (iface.getDisplayName().equals("wlan0")) {
          List<InetAddress> addresses = Collections.list(iface.getInetAddresses());
          for (InetAddress address : addresses) {
            if (address instanceof Inet4Address) {
              networkIp = address.getHostAddress();
            }
          }
        }
      }
    } catch (SocketException e) {
      e.printStackTrace();
    }

    return networkIp;
  }
Example #4
0
 /**
  * Get IP address from first non-localhost interface
  *
  * @param ipv4 true=return ipv4, false=return ipv6
  * @return address or empty string
  */
 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 "";
 }
  private NetworkInterface GetExternalNetworkInterface() {
    try {
      // iterate over the network interfaces known to java
      Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
      OUTER:
      for (NetworkInterface interface_ : Collections.list(interfaces)) {
        // we shouldn't care about loopback addresses
        if (interface_.isLoopback()) continue;

        // if you don't expect the interface to be up you can skip this
        // though it would question the usability of the rest of the code
        if (!interface_.isUp()) continue;

        // iterate over the addresses associated with the interface
        Enumeration<InetAddress> addresses = interface_.getInetAddresses();
        for (InetAddress address : Collections.list(addresses)) {
          // look only for ipv4 addresses
          if (address instanceof Inet6Address) continue;

          // use a timeout big enough for your needs
          if (!address.isReachable(3000)) continue;

          return interface_;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  public static Set<String> getIpAddresses() {
    if (_ipAddresses == null) {
      _ipAddresses = new HashSet<String>();

      try {
        List<NetworkInterface> networkInterfaces =
            Collections.list(NetworkInterface.getNetworkInterfaces());

        for (NetworkInterface networkInterface : networkInterfaces) {
          List<InetAddress> inetAddresses = Collections.list(networkInterface.getInetAddresses());

          for (InetAddress inetAddress : inetAddresses) {
            if (inetAddress.isLinkLocalAddress()
                || inetAddress.isLoopbackAddress()
                || !(inetAddress instanceof Inet4Address)) {

              continue;
            }

            _ipAddresses.add(inetAddress.getHostAddress());
          }
        }
      } catch (Exception e) {
        _log.error("Unable to read local server's IP addresses");

        _log.error(e, e);
      }
    }

    return new HashSet<String>(_ipAddresses);
  }