Class InternetHelper


  • public class InternetHelper
    extends Object
    Helper class for internet related stuff.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Field Detail

      • m_IPNetworkInterface

        protected static String m_IPNetworkInterface
        IP (from network intf) for faster access.
      • m_HostnameNetworkInterface

        protected static String m_HostnameNetworkInterface
        host name (from network intf) for faster access.
      • m_IP

        protected static String m_IP
        IP for faster access.
      • m_Hostname

        protected static String m_Hostname
        host name for faster access.
    • Constructor Detail

      • InternetHelper

        public InternetHelper()
    • Method Detail

      • debug

        protected static void debug​(String msg)
        Just outputs a message in the console.
        Parameters:
        msg - the debugging message
      • getIPFromNetworkInterface

        public static String getIPFromNetworkInterface​(boolean ipv4,
                                                       boolean debug)
        Returns the IP address determined from the network interfaces (using the IP address of the one with a proper host name).
        Parameters:
        ipv4 - whether to return IPv4 or IPv6
        debug - whether to output some debugging information
        Returns:
        the IP address
      • getIPFromNetworkInterface

        public static String getIPFromNetworkInterface()
        Returns the IPv4 address determined from the network interfaces (using the IP address of the one with a proper host name).
        Returns:
        the IPv4 address
      • getHostnameFromNetworkInterface

        public static String getHostnameFromNetworkInterface()
        Returns the host name determined from the network interfaces.
        Returns:
        the host name, null if none found
      • getLocalHostIP

        public static String getLocalHostIP()
        Returns the IP address of the local host as string.
        Returns:
        the IP address, null if not available
      • getLocalHostName

        public static String getLocalHostName()
        Returns the machine name of the local host as string.
        Returns:
        the machine name, null if not available
      • decodeBase64String

        public static String decodeBase64String​(String base64)
        Turns the base64 encoded string into plain text.
        Parameters:
        base64 - the encoded string
        Returns:
        the decoded string, null in case of an error
      • decodeBase64

        public static byte[] decodeBase64​(String base64)
        Turns the base64 encoded string into plain text.
        Parameters:
        base64 - the encoded string
        Returns:
        the decoded string, null in case of an error
      • encodeBase64

        public static String encodeBase64​(String raw)
        Encodes the given string in base64.
        Parameters:
        raw - the string to encode
        Returns:
        the encoded string
      • encodeBase64

        public static String encodeBase64​(byte[] raw)
        Encodes the given bytes in base64.
        Parameters:
        raw - the bytes to encode
        Returns:
        the encoded string
      • encodeUrlAsFilename

        public static String encodeUrlAsFilename​(String url)
        Turns a URL into a valid filename. Converts the following characters:
         : -> (c)
         / -> (s)
         & -> (a)
         ? -> (q)
         
        Parameters:
        url - the URL to convert
        Returns:
        the valid filename
      • decodeUrlFromFilename

        public static String decodeUrlFromFilename​(String filename)
        Converts a URL encoded in a filename back into a URL.
         (c) -> :
         (s) -> /
         (a) -> &
         (q) -> ?
         
        Parameters:
        filename - the filename to convert
        Returns:
        the URL
      • extractUrlFromFilename

        public static String extractUrlFromFilename​(String filename)
        Extracts the URL stored in the full filename provided. If no occurrences of "((" and "))" in the string, the complete string is interpreted as URL. Otherwise, the substring surrounded by "((" and "))" is extracted first. The URL is then decoded into a proper URL.
        Parameters:
        filename - the filename to extract the URL from
        Returns:
        the extracted/decoded URL
        See Also:
        decodeUrlFromFilename(String)
      • createUrlFilename

        public static String createUrlFilename​(String path,
                                               String prefix,
                                               String url,
                                               String suffix,
                                               String ext)
        Constructs a filename, ensuring that the URL is encoded properly.
        Parameters:
        path - the path to use, ignored if null (separator gets added automatically)
        prefix - the prefix for the name, ignored if null
        url - the URL to encode
        suffix - the suffix for the name, ignored if null
        ext - the file extension, ignored if null (dot gets automatically)