Class Util


  • public class Util
    extends java.lang.Object
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Util.PrivateInputThread
      A helper thread to mask private user input.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String CHMOD  
      private static java.lang.String DMSG  
    • Constructor Summary

      Constructors 
      Constructor Description
      Util()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.io.File createFile​(java.lang.String filename)
      Attempts to create a new file in an atomic way.
      static java.lang.String decode​(java.lang.String s)
      The function decodes URL-encoded strings into a regular string.
      static boolean destroy​(java.io.File file)
      Overwrites the contents of the file with a random string and then deletes the file.
      static boolean destroy​(java.lang.String file)
      Overwrites the contents of the file with a random string and then deletes the file.
      static java.lang.String formatTimeSec​(long time)
      Generates string representation of given time specified as long.
      static java.lang.String getInput​(java.lang.String prompt)
      Displays a prompt and then reads in the input from System.in.
      static java.lang.String getLocalHostAddress()
      Returns the ip address of the local machine.
      static java.lang.String getPrivateInput​(java.lang.String prompt)
      Displays a prompt and then reads in private input from System.in.
      static java.lang.String quote​(java.lang.String str)
      Quotifies a specified string.
      static boolean setFilePermissions​(java.lang.String file, int mode)
      Sets permissions on a given file.
      static boolean setOwnerAccessOnly​(java.lang.String file)
      Sets permissions on a given file to be only accessible by the current user.
      static java.lang.String unquote​(java.lang.String str)
      Dequotifies a specified string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Util

        public Util()
    • Method Detail

      • createFile

        public static java.io.File createFile​(java.lang.String filename)
                                       throws java.lang.SecurityException,
                                              java.io.IOException
        Attempts to create a new file in an atomic way. If the file already exists, it if first deleted.
        Parameters:
        filename - the name of file to create.
        Returns:
        the created file.
        Throws:
        java.lang.SecurityException - if the existing file cannot be deleted.
        java.io.IOException - if an I/O error occurred.
      • setOwnerAccessOnly

        public static boolean setOwnerAccessOnly​(java.lang.String file)
        Sets permissions on a given file to be only accessible by the current user.
        See Also:
        setFilePermissions(String, int)
      • setFilePermissions

        public static boolean setFilePermissions​(java.lang.String file,
                                                 int mode)
        Sets permissions on a given file. The permissions are set using the chmod command and will only work on Linux/Unix machines. Chmod command must be in the path.

        Note: This function executes an external program; thus, its behavior is influenced by environment variables such as the caller's PATH and the environment variables that control dynamic loading. Care should be used if calling this function from a program that will be run as a Unix setuid program, or in any other manner in which the owner of the Unix process does not completely control its runtime environment.
        Parameters:
        file - the file to set the permissions of.
        mode - the Unix style permissions.
        Returns:
        true, if change was successful, otherwise false. It can return false, in many instances, e.g. when file does not exits, when chmod is not found, or other error occurs.
      • destroy

        public static boolean destroy​(java.lang.String file)
        Overwrites the contents of the file with a random string and then deletes the file.
        Parameters:
        file - file to remove
      • destroy

        public static boolean destroy​(java.io.File file)
        Overwrites the contents of the file with a random string and then deletes the file.
        Parameters:
        file - file to remove
      • getInput

        public static java.lang.String getInput​(java.lang.String prompt)
        Displays a prompt and then reads in the input from System.in.
        Parameters:
        prompt - the prompt to be displayed
        Returns:
        String the input read in (entered after the prompt)
      • getPrivateInput

        public static java.lang.String getPrivateInput​(java.lang.String prompt)
        Displays a prompt and then reads in private input from System.in. Characters typed by the user are replaced with a space on the screen.
        Parameters:
        prompt - the prompt to be displayed
        Returns:
        String the input read in (entered after the prompt)
      • quote

        public static java.lang.String quote​(java.lang.String str)
        Quotifies a specified string. The entire string is encompassed by double quotes and each " is replaced with \", \ is replaced with \\.
        Parameters:
        str - the string to quotify
        Returns:
        quotified and escaped string
      • unquote

        public static java.lang.String unquote​(java.lang.String str)
                                        throws java.lang.Exception
        Dequotifies a specified string. The quotes are removed and each \" is replaced with " and each \\ is replaced with \.
        Parameters:
        str - the string to dequotify.
        Returns:
        unquotified string.
        Throws:
        java.lang.Exception
      • decode

        public static java.lang.String decode​(java.lang.String s)
        The function decodes URL-encoded strings into a regular string. This function is mostly copied from the Java source code. To convert to a String, each character is examined in turn:
        • The remaining characters are represented by 3-character strings which begin with the percent sign, "%xy", where xy is the two-digit hexadecimal representation of the lower 8-bits of the character.
      • formatTimeSec

        public static java.lang.String formatTimeSec​(long time)
        Generates string representation of given time specified as long. The format is: [days][,][h][,][min][,][sec]
      • getLocalHostAddress

        public static java.lang.String getLocalHostAddress()
        Returns the ip address of the local machine. If the 'ip' system property is defined it is returned, otherwise, the local ip address is lookup using the InetAddress class. In case the lookup fails, the address 127.0.0.1 is returned.
        Returns:
        local ip address