HOME PCB
..ByteObject.javaGenericThread.javaJsonObject.javaStaticValues.javaUtils.java
package Common;

import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.lang.reflect.Array;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;

public class Utils
{
    private Utils(){}

    public static File getFolderForJarContainingClass(Class c)
    {
        return new File(c.getProtectionDomain().getCodeSource().getLocation().getPath()).getParentFile();
    }

    public static ArrayList getHostAddresses()
    {
        ArrayList hostAddresses = new ArrayList<>();
        try
        {
            Enumeration e = NetworkInterface.getNetworkInterfaces();
            while (e.hasMoreElements())
            {
                NetworkInterface n = (NetworkInterface) e.nextElement();
                Enumeration ee = n.getInetAddresses();
                while (ee.hasMoreElements())
                {
                    InetAddress i = (InetAddress) ee.nextElement();
                    String hostAddress = i.getHostAddress();
                    if (!hostAddress.equals("127.0.0.1"))
                    {
                        hostAddresses.add(hostAddress);
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return hostAddresses;
    }

    public static  T[] joinArrayGeneric(T[]... arrays)
    {
        int length = 0;

        for (T[] array : arrays)
        {
            length += array.length;
        }

        //T[] result = new T[length];
        final T[] result = (T[]) Array.newInstance(arrays[0].getClass().getComponentType(), length);

        int offset = 0;
        for (T[] array : arrays)
        {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }

        return result;
    }

    public static byte[] joinByteArray(byte[]... arrays)
    {
        int length = 0;
        for (byte[] array : arrays) {
            length += array.length;
        }

        final byte[] result = new byte[length];

        int offset = 0;
        for (byte[] array : arrays) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }

        return result;
    }

    public static void setDefaultCharset(Charset charset)
    {
        System.setProperty("file.encoding", charset.name());
    }

    public static Charset getDefaultCharset()
    {
        return Charset.forName(System.getProperty("file.encoding"));
    }

    public static byte[] intToBytes(int integer)
    {
        return ByteBuffer.allocate(4).putInt(integer).array();
    }

    /**
    public static Integer getTomcatPortFromConfigXml(File serverXml) {
        Integer tcpPort;
        try {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true); // never forget this!
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse(serverXml);
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile
                    ("/Server/Service[@name='Catalina']/Connector[count(@scheme)=0]/@tcpPort[1]");
            String result = (String) expr.evaluate(doc, XPathConstants.STRING);
            tcpPort = result != null && result.length() > 0 ? Integer.valueOf(result) : null;
        } catch (Exception e) {
            tcpPort = null;
        }
        return tcpPort;
    }**/
}