Package util

Class IO


public final class IO extends Object
Utility class for I/O related operations.

This class provides static methods for various file and data handling operations, such as reading, writing, compressing, and hashing files and strings.

  • Method Details

    • formatFrequency

      public static String formatFrequency(double value)
      Formats a frequency value into scientific notation.

      This method formats the given frequency value using the frequencyFormat. If the formatted value equals ".10E1", it is replaced with "1.00E0" for consistency.

      Parameters:
      value - The frequency value to format.
      Returns:
      A String representing the formatted frequency in scientific notation.
    • formatNumber

      public static String formatNumber(double value)
      Formats a number to three decimal places.

      This method formats the given number using the decimalFormat. The result is a string representation of the number with up to three decimal places.

      Parameters:
      value - The number to format.
      Returns:
      A String representing the formatted number.
    • randomAlphanumeric

      public static String randomAlphanumeric(int length)
      Generates a random alphanumeric string of the specified length.

      This method uses the RandomStringUtils instance to generate a random string consisting of both letters and digits. The generated string is suitable for use in scenarios where a random identifier or token is needed.

      Parameters:
      length - The length of the random alphanumeric string to generate.
      Returns:
      A String containing the generated random alphanumeric characters.
    • writeFile

      public static void writeFile(Path path, String content) throws IOException
      Writes the specified content to a file at the given path.

      This method ensures that the parent directories of the target file are created if they do not exist. It then writes the provided content to the file using UTF-8 encoding. If the file already exists, its content is overwritten.

      Parameters:
      path - The Path where the file will be written.
      content - The String content to write to the file.
      Throws:
      IOException - If an I/O error occurs during directory creation or file writing.
    • readTabularFileAsNestedMap

      public static Map<String,Map<String,String>> readTabularFileAsNestedMap(File file) throws IOException
      Reads a tabular file and converts its content into a nested map structure.

      This method reads a tabular file where the first row contains headers and each subsequent row contains data. The first column is treated as the key for the outer map, and the remaining columns are stored in an inner map with their corresponding headers as keys. The file can use tab or comma as delimiters.

      Parameters:
      file - The File object representing the tabular file to read.
      Returns:
      A HashMap where the outer map's key is the first column's value, and the value is another HashMap containing the remaining columns as key-value pairs.
      Throws:
      IOException - If an I/O error occurs or the file format is invalid.
    • copyResourceToFile

      public static void copyResourceToFile(String resourceName, Path targetPath) throws MusialException
      Copies a resource from the application's classpath to a specified target Path.

      This method retrieves a resource as an InputStream from the application's classpath using the specified resource path. The resource is then copied to the target file path, overwriting any existing file at the target location.

      Parameters:
      resourceName - The path to the resource within the application's classpath.
      targetPath - The file path where the resource should be copied.
      Throws:
      MusialException - If the resource cannot be found or an I/O error occurs during the copy operation.
    • gzipCompress

      public static String gzipCompress(String content) throws IOException
      Compresses a string using GZIP compression and encodes the result in Base64.

      This method compresses the input string using the GZIP algorithm and then encodes the compressed byte array into a Base64 string. The method ensures proper resource management by using a try-with-resources block for the output streams.

      Parameters:
      content - The String to be compressed.
      Returns:
      A Base64-encoded String representing the GZIP-compressed content.
      Throws:
      IOException - If an I/O error occurs during compression.
    • gzipDecompress

      public static String gzipDecompress(String content) throws IOException
      Decompresses a Base64-encoded GZIP-compressed string.

      This method decodes the input string from Base64, decompresses the resulting GZIP-compressed data, and returns the decompressed content as a string. It uses a buffer to read the decompressed data in chunks and appends it to a StringBuilder.

      Parameters:
      content - The Base64-encoded GZIP-compressed string to decompress.
      Returns:
      A String containing the decompressed content.
      Throws:
      IOException - If an I/O error occurs during decompression.
    • md5Hash

      public static String md5Hash(String content)
      Generates the MD5 hash of the given string.

      This method computes the MD5 hash of the input string and returns it as a hexadecimal string. It uses the DigestUtils.md5Hex(String) method from the Apache Commons Codec library to perform the hashing.

      Parameters:
      content - The String to hash.
      Returns:
      A String representing the MD5 hash of the input content in hexadecimal format.