Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static String getWifiIpv6() {
        try {
            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() && inetAddress instanceof Inet6Address) {
                        String[] ipv6 = inetAddress.getHostAddress().split("%");
                        String port = ipv6[1];
                        if (port.matches("wlan\\d+")) {
                            return ipv6[0];
                        }
                    }
                }
            }
        } catch (SocketException ex) { /*CONSUME*/
        }
        return null;
    }

    private static boolean matches(String[] filters, Matcher matcher) {
        for (String filter : filters) {
            // Is logcat filter?
            if (filter.matches("(\\w+|\\*):[\\w|\\*]")) {
                String[] matches = filter.split(":");
                // Tag and level match?
                if ((matches[0].equals("*") || matches[0].equals(matcher.group(6)))
                        && (matches[1].equals("*") || matches[1].equals(matcher.group(5)))) {
                    return true;
                }
            } else {
                Pattern pattern = Pattern.compile(".*" + Pattern.quote(filter) + ".*", Pattern.CASE_INSENSITIVE);
                for (int i = 1; i < 8; i++) {
                    if (pattern.matcher(matcher.group(i)).matches()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}