<commit_before>/*
 * Copyright 2015 The OpenDCT Authors. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package opendct.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.channels.FileChannel;
import java.util.Enumeration;

public class Util {

    /**
     * Get a local IP address with a from a remote IP address that are on the same subnet.
     *
     * @param remoteAddress This is the address of the remote device.
     * @return The first local address that is on the same subnet as the remote device or null if no
     * IP address was found.
     * @throws SocketException Thrown if IO error happens.
     */
    public static InetAddress getLocalIPForRemoteIP(InetAddress remoteAddress) throws SocketException {
        byte remoteAddressBytes[] = remoteAddress.getAddress();

        Enumeration<NetworkInterface> networkInterface = NetworkInterface.getNetworkInterfaces();

        while (networkInterface.hasMoreElements()) {
            NetworkInterface currentInterface = networkInterface.nextElement();

            // Never try to match a loopback interface. You will sometimes lose!
            if (currentInterface.isLoopback()) {
                continue;
            }

            for (InterfaceAddress address : currentInterface.getInterfaceAddresses()) {
                byte localIPAddress[] = address.getAddress().getAddress();

                // Make sure these are the same kind of IP addresses. This should only differ if one of
                // the addresses is v4 and the other is v6.
                if (localIPAddress.length != remoteAddressBytes.length) {
                    continue;
                }

                byte localSubnet[] = new byte[localIPAddress.length];
                int localSubnetLength = address.getNetworkPrefixLength();

                boolean match = true;
                int bits = localSubnet.length * 8;
                for (int i = 0; i < localSubnet.length; i++) {
                    localSubnet[i] = (byte) (localSubnetLength >> bits);
                    bits -= 8;

                    byte localAnd = (byte) (localIPAddress[i] & localSubnet[i]);
                    byte remoteAnd = (byte) (remoteAddressBytes[i] & localSubnet[i]);

                    if (localAnd != remoteAnd) {
                        match = false;
                        break;
                    }
                }

                if (!match) {
                    continue;
                }

                return address.getAddress();
            }
        }

        return null;
    }

    public static NetworkInterface getNetworkInterfaceForRemoteIPAddress(InetAddress remoteAddress) throws SocketException {
        byte remoteAddressBytes[] = remoteAddress.getAddress();

        Enumeration<NetworkInterface> networkInteface = NetworkInterface.getNetworkInterfaces();

        while (networkInteface.hasMoreElements()) {
            NetworkInterface networkInterface = networkInteface.nextElement();

            for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
                byte localIPAddress[] = address.getAddress().getAddress();

                // Make sure these are the same kind of IP addresses. This should only differ if one of
                // the addresses is v4 and the other is v6.
                if (localIPAddress.length != remoteAddressBytes.length) {
                    continue;
                }

                byte localSubnet[] = new byte[localIPAddress.length];
                int localSubnetLength = address.getNetworkPrefixLength();

                boolean match = true;
                int bits = localSubnet.length * 8;
                for (int i = 0; i < localSubnet.length; i++) {
                    localSubnet[i] = (byte) (localSubnetLength >> bits);
                    bits -= 8;

                    byte localAnd = (byte) (localIPAddress[i] & localSubnet[i]);
                    byte remoteAnd = (byte) (remoteAddressBytes[i] & localSubnet[i]);

                    if (localAnd != remoteAnd) {
                        match = false;
                        break;
                    }
                }

                if (!match) {
                    continue;
                }

                return networkInterface;
            }
        }

        return null;
    }

    public static String[] arrayToStringArray(boolean[] values) {
        String returnValues[] = new String[values.length];

        for (int i = 0; i < values.length; i++) {
            returnValues[i] = String.valueOf(values[i]);
        }

        return returnValues;
    }

    public static String[] arrayToStringArray(int[] values) {
        String returnValues[] = new String[values.length];

        for (int i = 0; i < values.length; i++) {
            returnValues[i] = String.valueOf(values[i]);
        }

        return returnValues;
    }<commit_msg>Fixed local IP address detection finally.<commit_after>/*
 * Copyright 2015 The OpenDCT Authors. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package opendct.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.channels.FileChannel;
import java.util.Enumeration;

public class Util {

    /**
     * Get a local IP address with a from a remote IP address that are on the same subnet.
     *
     * @param remoteAddress This is the address of the remote device.
     * @return The first local address that is on the same subnet as the remote device or null if no
     * IP address was found.
     * @throws SocketException Thrown if IO error happens.
     */
    public static InetAddress getLocalIPForRemoteIP(InetAddress remoteAddress) throws SocketException {
        byte remoteAddressBytes[] = remoteAddress.getAddress();

        Enumeration<NetworkInterface> networkInterface = NetworkInterface.getNetworkInterfaces();

        while (networkInterface.hasMoreElements()) {
            NetworkInterface currentInterface = networkInterface.nextElement();

            // Never try to match a loopback interface.
            if (currentInterface.isLoopback()) {
                continue;
            }

            for (InterfaceAddress address : currentInterface.getInterfaceAddresses()) {
                byte localIPAddress[] = address.getAddress().getAddress();

                // Make sure these are the same kind of IP addresses. This should only differ if one of
                // the addresses is v4 and the other is v6.
                if (localIPAddress.length != remoteAddressBytes.length) {
                    continue;
                }

                byte localSubnet[] = new byte[localIPAddress.length];
                int localSubnetLength = address.getNetworkPrefixLength();

                boolean match = true;
                int bits = localSubnetLength;
                for (int i = 0; i < localSubnet.length; i++) {
                    if (bits < 8) {
                        localSubnet[i] = (byte) ~((1 << 8 - bits) - 1);

                        byte localAnd = (byte) (localIPAddress[i] & localSubnet[i]);
                        byte remoteAnd = (byte) (remoteAddressBytes[i] & localSubnet[i]);

                        if (localAnd != remoteAnd) {
                            match = false;
                            break;
                        }
                    } else if (!(localIPAddress[i] == remoteAddressBytes[i])) {
                        match = false;
                        break;
                    }

                    bits -= 8;
                }

                if (!match) {
                    continue;
                }

                return address.getAddress();
            }
        }

        return null;
    }

    public static NetworkInterface getNetworkInterfaceForRemoteIPAddress(InetAddress remoteAddress) throws SocketException {
        byte remoteAddressBytes[] = remoteAddress.getAddress();

        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface currentInterface = networkInterfaces.nextElement();

            // Never try to match a loopback interface.
            if (currentInterface.isLoopback()) {
                continue;
            }

            for (InterfaceAddress address : currentInterface.getInterfaceAddresses()) {
                byte localIPAddress[] = address.getAddress().getAddress();

                // Make sure these are the same kind of IP addresses. This should only differ if one of
                // the addresses is v4 and the other is v6.
                if (localIPAddress.length != remoteAddressBytes.length) {
                    continue;
                }

                byte localSubnet[] = new byte[localIPAddress.length];
                int localSubnetLength = address.getNetworkPrefixLength();

                boolean match = true;
                int bits = localSubnetLength;
                for (int i = 0; i < localSubnet.length; i++) {
                    if (bits < 8) {
                        localSubnet[i] = (byte) ~((1 << 8 - bits) - 1);

                        byte localAnd = (byte) (localIPAddress[i] & localSubnet[i]);
                        byte remoteAnd = (byte) (remoteAddressBytes[i] & localSubnet[i]);

                        if (localAnd != remoteAnd) {
                            match = false;
                            break;
                        }
                    } else if (!(localIPAddress[i] == remoteAddressBytes[i])) {
                        match = false;
                        break;
                    }

                    bits -= 8;
                }

                if (!match) {
                    continue;
                }

                return currentInterface;
            }
        }

        return null;
    }

    public static String[] arrayToStringArray(boolean[] values) {
        String returnValues[] = new String[values.length];

        for (int i = 0; i < values.length; i++) {
            returnValues[i] = String.valueOf(values[i]);
        }

        return returnValues;
    }

    public static String[] arrayToStringArray(int[] values) {
        String returnValues[] = new String[values.length];

        for (int i = 0; i < values.length; i++) {
            returnValues[i] = String.valueOf(values[i]);
        }

        return returnValues;
    }
{'fraction_non_alphanumeric': 0.08434950803128416, 'fraction_numerical': 0.0038684719535783366, 'mean_word_length': 1.8080283353010627, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '507977', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2738, 'n_words': 901}