Class JSONataUtils

    • Field Detail

      • HEX_BYTES

        public static final byte[] HEX_BYTES
      • SEED_RANDOM

        public static Random SEED_RANDOM
        If this class is initialized in a different JVM is started at the same millisecond, its s_r will generate the same sequence of random numbers.
      • SEED_SECURE_RANDOM

        public static SecureRandom SEED_SECURE_RANDOM
    • Constructor Detail

      • JSONataUtils

        public JSONataUtils()
    • Method Detail

      • closeTextFile

        public static void closeTextFile​(BufferedReader br)
        Close a buffered reader opened using openTextFile(String)
        Parameters:
        br - buffered reader to be closed
      • byteToHexChars

        public static char[] byteToHexChars​(byte bIn)
        Converts a byte into an array of char's containing the hexadecimal digits. For example, 0x2f would return char[] {'2','F'}
        Parameters:
        bIn - byte to be converted to hexadecimal digits
        Returns:
        array of chars containing the hexadecimal digits for the value of the input byte.
      • getUniqueID

        public static String getUniqueID()
        Returns:
        a 40 byte String random number based on invoking the com.ibm.crypto.fips.provider.SecureRandom class.
      • hexDecode

        public static byte[] hexDecode​(String strHex)
                                throws InvalidParameterException
        Transform the string of hexadecimal digits into a byte array.
        Parameters:
        strHex - a String containing pairs of hexadecimal digits
        Returns:
        a byte array created by transforming pairs of hexadecimal digits into a byte. For example "7F41" would become byte [] { 0x7f, 0x41}
        Throws:
        InvalidParameterException - thrown if the input string is null or empty, or if it does not contain an even number of hexadecimal digits, or if it contains something other than a hexadecimal digit.
      • hexEncode

        public static String hexEncode​(byte[] bArray)
        Convert the byte array into a String of hexadecimal digits. For example, the bytes[] {0x31,0x0a} would become "310A".
        Parameters:
        bArray - the array of bytes to be converted.
        Returns:
        a String of hexadecimal digits formed by the hexadecimal digit for each nibble of the byte.
      • prompt

        public static String prompt​(String strPrompt)
        Print the supplied prompt (if not null) and return the trimmed response
        Parameters:
        strPrompt - the prompt to be displayed
        Returns:
        the trimmed response to the prompt (may be the empty String ("") if nothing entered)
      • prompt

        public static String prompt​(String strPrompt,
                                    boolean bTrim)
        Print the supplied prompt (if not null) and return the trimmed response according to the supplied trim control
        Parameters:
        strPrompt - the prompt to be displayed
        bTrim - whether or not to trim the input
        Returns:
        the trimmed response (if so commanded) to the prompt (may be the empty String ("") if nothing entered)
      • saveJSONFile

        public static com.fasterxml.jackson.databind.JsonNode saveJSONFile​(String jsonFileName,
                                                                           com.fasterxml.jackson.databind.JsonNode jsonData)
                                                                    throws Exception
        Save the specified JSONObject in serialized form to the specified file or throw the appropriate exception.
        Parameters:
        jsonFileName - fully qualified name of the JSON file to be saved
        jsonData - the JSONObject to be saved to a file.
        Returns:
        the jsonData that was saved
        Throws:
        Exception - IOException) if there is a problem writing the file
      • listSourceFiles

        public static List<Path> listSourceFiles​(Path dir,
                                                 String ext)
                                          throws IOException
        Construct and return a sorted list of files in a directory identified by the dir that have extensions matching the ext
        Parameters:
        dir - the path to the directory containing files to be returned in the list
        ext - the file extension (without the leading period) used to filter files in the dir
        Returns:
        sorted list of files in a directory identified by the dir that have extensions matching the ext
        Throws:
        IOException - if there is difficulty accessing the files in the supplied dir
      • loadJSONFile

        public static com.fasterxml.jackson.databind.JsonNode loadJSONFile​(String jsonFQFileName)
                                                                    throws Exception
        Load the specified JSON file from the fully qualified file name or throw the appropriate exception.
        Parameters:
        jsonFQFileName - name of the JSON file to be loaded
        Returns:
        the JSONObject contained in the file, or an empty JSONObject if no object exists
        Throws:
        Exception - If the file can no be located, or if there is a problem reading the file
      • readLine

        public static String readLine​(BufferedReader br)
                               throws IOException
        Reads a buffered reader line up to a newline and returns the content read as a String that does not contain the linefeed.
        Parameters:
        br - buffered reader
        Returns:
        String containing the characters read through the terminator character. If the end of file has been reached with nothing available to be returned, then null is returned.
        Throws:
        IOException - if an error occurs while reading the buffered reader.
        See Also:
        readLine(BufferedReader, HashSet)
      • readLine

        public static String readLine​(BufferedReader br,
                                      HashSet<Integer> terminators)
                               throws IOException
        Reads a buffered reader line up to any of the terminator characters (e.g., 0x0a for newline) and returns the content read as a String that does not contain the terminator.
        Parameters:
        br - buffered reader
        terminators - the set of line terminators used to signal return of the next "line" from the buffered reader.
        Returns:
        String containing the characters read through the terminator character. If the end of file has been reached with nothing available to be returned, then null is returned.
        Throws:
        IOException - if an error occurs while reading the buffered reader.
      • createSequence

        public static Sequence createSequence​(com.fasterxml.jackson.databind.JsonNode... arguments)
      • isSequence

        public static boolean isSequence​(Object value)
      • decodeURIComponent

        public static String decodeURIComponent​(String s)
                                         throws UnsupportedEncodingException
        Decodes the passed UTF-8 String using an algorithm that's compatible with JavaScript's decodeURIComponent function. Returns null if the String is null.
        Parameters:
        s - The UTF-8 encoded String to be decoded
        Returns:
        the decoded String
        Throws:
        UnsupportedEncodingException
      • encodeURIComponent

        public static String encodeURIComponent​(String s)
                                         throws URISyntaxException
        Encodes the passed String as UTF-8 using an algorithm that's compatible with JavaScript's encodeURIComponent function. Returns null if the String is null.
        Parameters:
        s - The String to be encoded
        Returns:
        the encoded String
        Throws:
        URISyntaxException
      • substr

        public static String substr​(String str,
                                    Integer start,
                                    Integer length)
        Parameters:
        str -
        start - Location at which to begin extracting characters. If a negative number is given, it is treated as strLength - start where strLength is the length of the string. For example, str.substr(-3) is treated as str.substr(str.length - 3)
        length - The number of characters to extract. If this argument is null, all the characters from start to the end of the string are extracted.
        Returns:
        A new string containing the extracted section of the given string. If length is 0 or a negative number, an empty string is returned.