Class FileHelper


  • public class FileHelper
    extends Object
    Author:
    giovanni.antino@h2tcoin.com
    • Field Detail

      • KEYLOCK

        public static final Object KEYLOCK
    • Constructor Detail

      • FileHelper

        public FileHelper()
    • Method Detail

      • getDefaultApplicationDirectoryPath

        public static Path getDefaultApplicationDirectoryPath()
        return the default path to application directory
        Returns:
      • getHotmokaTestDirectoryPath

        public static Path getHotmokaTestDirectoryPath()
      • getHotmokaFilesDirectoryPath

        public static Path getHotmokaFilesDirectoryPath()
      • getUserWalletsDirectoryPath

        public static Path getUserWalletsDirectoryPath()
        return the default user wallets directory
        Returns:
      • getDefaultStatePath

        public static Path getDefaultStatePath()
        return the path of the application state file
        Returns:
      • defaultStatePathExists

        public static boolean defaultStatePathExists()
        true if the application state file exists
        Returns:
      • homeDirExists

        public static boolean homeDirExists()
        Returns:
        true if the default project home dir exist
      • getDefaultWalletDirectoryPath

        public static Path getDefaultWalletDirectoryPath()
        Returns:
        default wallet directory path
      • getPublicKeyDirectoryPath

        public static Path getPublicKeyDirectoryPath()
        Returns:
        default public keys directory path
      • getTransactionsDirectoryPath

        public static Path getTransactionsDirectoryPath()
        Returns:
        default transactions directory path
      • getEphemeralWalletDirectoryPath

        public static Path getEphemeralWalletDirectoryPath()
        Returns:
        ephemeral wallet directory path
      • getDefaultAddressBookDirectoryPath

        public static Path getDefaultAddressBookDirectoryPath()
        Returns:
        default address book directory path
      • getDefaultLogsDirectoryPath

        public static Path getDefaultLogsDirectoryPath()
        Returns:
        default logs directory path
      • logFile

        public static Path logFile​(String pattern)
        convert pattern from Log.getLogFilePattern() to path
        Parameters:
        pattern -
        Returns:
      • getChainDirectory

        public static Path getChainDirectory()
        return the path of default chain directory
        Returns:
      • getChainLockFile

        public static Path getChainLockFile()
        default path for chain lock file
        Returns:
      • getEpochDirectory

        public static Path getEpochDirectory​(int epochNumber)
        return the path to the epoch folder
        Parameters:
        epochNumber -
        Returns:
      • getSlotDirectory

        public static Path getSlotDirectory​(int epochNumber,
                                            int slotNumber)
        return the path to the slot directory
        Parameters:
        epochNumber -
        slotNumber -
        Returns:
      • getDefaultZeroBlockDirectory

        public static Path getDefaultZeroBlockDirectory()
        return the path to the zero block folder
        Returns:
      • getDefaultZeroBlockFile

        public static Path getDefaultZeroBlockFile()
        return the path to the block required to initialize the chain
        Returns:
      • getKeyWriterBucketDirectory

        public static Path getKeyWriterBucketDirectory​(int epoch,
                                                       int slot,
                                                       String uid,
                                                       FixedParameters.HexKeyWriter hkw)
        return the bucket directory
        Parameters:
        epoch -
        slot -
        uid -
        hkw -
        Returns:
      • defaultZeroBlockDirectoryExists

        public static boolean defaultZeroBlockDirectoryExists()
        First block directory
        Returns:
        true if exists and is a directory
      • defaultZeroBlockFileExists

        public static boolean defaultZeroBlockFileExists()
        First block
        Returns:
        true if is file and exixsts
      • slotDirectoryExists

        public static boolean slotDirectoryExists​(int epochNumber,
                                                  int slotNumber)
        Slot directory
        Parameters:
        epochNumber -
        slotNumber -
        Returns:
        true if exists and is a directory
      • epochDirectoryExists

        public static boolean epochDirectoryExists​(int epochNumber)
        Epoch directory
        Parameters:
        epochNumber -
        Returns:
        true if exists and is a directory
      • getNodeNetworkSettings

        public static Path getNodeNetworkSettings()
      • chainDirectoryExists

        public static boolean chainDirectoryExists()
        Chain directory
        Returns:
        true if exists and is a directory
      • logsDirectoryExists

        public static boolean logsDirectoryExists()
        Log directory
        Returns:
        true if exists and is a directory
      • createSlotDirectory

        public static Path createSlotDirectory​(int epochNumber,
                                               int slotNumber)
                                        throws IOException
        Create slot directory and return path, or return slot directory path if exists
        Parameters:
        epochNumber -
        slotNumber -
        Returns:
        slot directory path
        Throws:
        IOException
      • createFolderAtPathIfNoneExist

        public static void createFolderAtPathIfNoneExist​(Path path)
                                                  throws IOException
        Creates a directory at the indicated path if none exist already
        Parameters:
        path -
        Throws:
        IOException
      • getTransactionBoxFolder

        public static Path getTransactionBoxFolder​(int epoch,
                                                   int slot)
        Returns:
      • getTransactionBoxDateFolder

        public static Path getTransactionBoxDateFolder​(Date input)
        Returns:
      • getBlockName

        public static String getBlockName​(String blockUID)
        convert uid into database name
        Parameters:
        blockUID -
        Returns:
      • getBlockPath

        public static Path getBlockPath​(int epochNumber,
                                        int slotNumber,
                                        String blockHashNumber)
        return the path without extension
        Parameters:
        epochNumber -
        slotNumber -
        blockHashNumber - StringUtils.getBlockHashNumber(blockHash)
        Returns:
        return the path for the requested block
      • getBlockPathWithExension

        public static Path getBlockPathWithExension​(int epochNumber,
                                                    int slotNumber,
                                                    String blockHashNumber)
        return the path with extension
        Parameters:
        epochNumber -
        slotNumber -
        blockHashNumber - StringUtils.getBlockHashNumber(blockHash)
        Returns:
        return the path for the requested block
      • blockExists

        public static boolean blockExists​(int epochNumber,
                                          int slotNumber,
                                          String blockHashNumber)
        Parameters:
        epochNumber -
        slotNumber -
        blockHashNumber -
        Returns:
        true if file exists and is file
      • walletDirExists

        public static boolean walletDirExists()
        Returns:
        true if the directory exist
      • publicKeyDirExists

        public static boolean publicKeyDirExists()
        Returns:
        true if the directory exist
      • transactionsDirExists

        public static boolean transactionsDirExists()
        Returns:
        true if the directory exist
      • walletEphemeralDirExists

        public static boolean walletEphemeralDirExists()
        Returns:
        true if the directory exist
      • addressBookDirExists

        public static boolean addressBookDirExists()
        Returns:
        true if the address book exist
      • createDir

        public static void createDir​(Path directoryPathAndName)
                              throws IOException
        create directory the path is built using Paths.get()
        Parameters:
        directoryPathAndName -
        Throws:
        IOException
      • getSettingsPathFolder

        public static Path getSettingsPathFolder()
      • getTransactionsDumpPathFolder

        public static Path getTransactionsDumpPathFolder()
      • getSimulationPathFolder

        public static Path getSimulationPathFolder()
      • getTransactionsDumpPathFolder

        public static Path getTransactionsDumpPathFolder​(String pidName)
      • extractFromJar

        public static void extractFromJar​(String resourceName,
                                          Path destinationPath)
        Parameters:
        resourceName - resource name
        destinationPath - destination folder path
      • initProjectFiles

        public static void initProjectFiles()
                                     throws IOException
        create project directory and folder structure using default parameters
        Throws:
        IOException
      • getQteslaReferenceFolder

        public static Path getQteslaReferenceFolder()
      • getQteslaReferenceAddressFolder

        public static Path getQteslaReferenceAddressFolder​(String addr)
      • createIfNotExistQteslaReferenceFolder

        public static Path createIfNotExistQteslaReferenceFolder​(String addr)
      • deflateInflateAddress

        public static String deflateInflateAddress​(String addr,
                                                   boolean deflate)
      • saveQteslaAddress

        public static String saveQteslaAddress​(String qAddr)
      • getReferenceKeyStringNoSave

        public static String getReferenceKeyStringNoSave​(String qAddr)
      • retriveQaddrByReference

        public static String retriveQaddrByReference​(String refAddr)
      • fileExists

        public static boolean fileExists​(Path file)
        Parameters:
        file -
        Returns:
        true if exists and is a file
      • directoryExists

        public static boolean directoryExists​(Path filePointer)
        Parameters:
        filePointer -
        Returns:
        true if exists and is a directory
      • writeStringToFile

        public static boolean writeStringToFile​(Path directory,
                                                String fileName,
                                                String content,
                                                boolean overwrite)
                                         throws IOException
        Write a string to file
        If file does not exist the string will be written to file and the function return true.
         FileHelper.writeStringToFile(
                      FileHelper.getDefaultApplicationDirectoryPath(),
                      "TestFile.txt", "Only create, no overwrite", false)
         

        If file exists and overwite is specified the original file is deleted, new one is created, the string is written in it and the function return true.
         FileHelper.writeStringToFile(
                      FileHelper.getDefaultApplicationDirectoryPath(),
                      "TestFile.txt", "Create or Overwrite", true)
         

        If file exists, overvrite is denied function terminate and return false
        Parameters:
        directory - destination directory
        fileName - file name
        content - string to be writed to file
        overwrite - true for overwriting content if file exists, ignored otherwise
        Returns:
        true if succesfully written
        Throws:
        IOException
      • getFileList

        public static ArrayList<String> getFileList​(Path folderPath,
                                                    String filter)
        return the list of wallets inside folder path
        Parameters:
        folderPath -
        filter -
        Returns:
      • getFolderContent

        public static File[] getFolderContent​(Path folderPath)
        list file in the passed folder
        Parameters:
        folderPath -
        Returns:
      • delete

        public static void delete​(File f)
                           throws IOException
        delete passed file or the folder and all its content, recursively. If f is dir f is deleted
        Parameters:
        f -
        Throws:
        IOException
      • deleteSingleFile

        public static void deleteSingleFile​(Path filePath)
        Parameters:
        filePath -
      • delete

        public static void delete​(Path file)
                           throws IOException
        delete RECURSIVELY everithing in the path
        Parameters:
        file -
        Throws:
        IOException
      • deleteFolderContent

        public static void deleteFolderContent​(File f)
                                        throws IOException
        recursively delete folder content TODO delete root folder?
        Parameters:
        f -
        Throws:
        IOException
      • rename

        public static void rename​(Object sourcePath,
                                  Object targetPath,
                                  Boolean overwrite)
                           throws IOException
        moves the target file/folder to the new destination and can overwrite the existing file/folder if it exists
        Parameters:
        sourcePath - can be of type String/File/Path
        targetPath - can be of type String/File/Path
        overwrite - indicated whether to overwrite existing files/folders
        Throws:
        IOException
      • copy

        public static void copy​(Path sources,
                                Path destination)
                         throws IOException
        OVERWRITE
        Parameters:
        sources -
        destination -
        Throws:
        IOException
      • moveToSlot

        public static void moveToSlot​(int sourceEpoch,
                                      int sourceSlot,
                                      String sourceFile,
                                      int targetEpoch,
                                      int targetSlot,
                                      String targetFile)
                               throws Exception
        Moves and renames any existing file or folder within the source epoch and slot to the target epoch and slot, overwrites if file/folder already exists
        Parameters:
        sourceEpoch -
        sourceSlot -
        sourceFile -
        targetEpoch -
        targetSlot -
        targetFile -
        Throws:
        Exception
      • deleteSettings

        public static void deleteSettings​(String option)
      • blockExists

        public static boolean blockExists​(StatePointerBean spb)
        Parameters:
        spb -
        Returns:
        true if file exists and is file
      • writeTransactionBoxToFile

        public static void writeTransactionBoxToFile​(Path path,
                                                     Object input)
        Parameters:
        path -
        input -
      • writeObjectToFile

        public static void writeObjectToFile​(Path path,
                                             String sithNumber,
                                             Object input)
        Parameters:
        path -
        sithNumber -
        input -
      • readObjectFromFile

        public static Object readObjectFromFile​(Path filePath)
        Parameters:
        filePath -
        Returns:
      • readObjectFromFile

        public static Object readObjectFromFile​(String filepath)
        Parameters:
        filepath -
        Returns:
      • testMandatoryStatusBean

        public static boolean testMandatoryStatusBean​(int epoch,
                                                      int slot,
                                                      String uid)
        Parameters:
        epoch -
        slot -
        uid -
        Returns:
        false if cs is not consistent