Package com.h2tcoin.takamakachain.utils
Class FileHelper
- java.lang.Object
-
- com.h2tcoin.takamakachain.utils.FileHelper
-
public class FileHelper extends Object
- Author:
- giovanni.antino@h2tcoin.com
-
-
Constructor Summary
Constructors Constructor Description FileHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanaddressBookDirExists()static booleanblockExists(int epochNumber, int slotNumber, String blockHashNumber)static booleanblockExists(StatePointerBean spb)static booleanchainDirectoryExists()Chain directorystatic voidcopy(Path sources, Path destination)OVERWRITEstatic voidcreateDir(Path directoryPathAndName)create directory the path is built usingPaths.get()static voidcreateFolderAtPathIfNoneExist(Path path)Creates a directory at the indicated path if none exist alreadystatic PathcreateIfNotExistQteslaReferenceFolder(String addr)static PathcreateSlotDirectory(int epochNumber, int slotNumber)Create slot directory and return path, or return slot directory path if existsstatic booleandefaultStatePathExists()true if the application state file existsstatic booleandefaultZeroBlockDirectoryExists()First block directorystatic booleandefaultZeroBlockFileExists()First blockstatic StringdeflateInflateAddress(String addr, boolean deflate)static voiddelete(File f)delete passed file or the folder and all its content, recursively.static voiddelete(Path file)delete RECURSIVELY everithing in the pathstatic voiddeleteFolderContent(File f)recursively delete folder content TODO delete root folder?static voiddeleteSettings(String option)static voiddeleteSingleFile(Path filePath)static booleandirectoryExists(Path filePointer)static booleanepochDirectoryExists(int epochNumber)Epoch directorystatic voidextractFromJar(String resourceName, Path destinationPath)static booleanfileExists(Path file)static StringgetBlockName(String blockUID)convert uid into database namestatic PathgetBlockPath(int epochNumber, int slotNumber, String blockHashNumber)return the path without extensionstatic PathgetBlockPathWithExension(int epochNumber, int slotNumber, String blockHashNumber)return the path with extensionstatic PathgetChainDirectory()return the path of default chain directorystatic PathgetChainLockFile()default path for chain lock filestatic PathgetDefaultAddressBookDirectoryPath()static PathgetDefaultApplicationDirectoryPath()return the default path to application directorystatic PathgetDefaultLogsDirectoryPath()static PathgetDefaultStatePath()return the path of the application state filestatic PathgetDefaultWalletDirectoryPath()static PathgetDefaultZeroBlockDirectory()return the path to the zero block folderstatic PathgetDefaultZeroBlockFile()return the path to the block required to initialize the chainstatic PathgetEphemeralWalletDirectoryPath()static PathgetEpochDirectory(int epochNumber)return the path to the epoch folderstatic ArrayList<String>getFileList(Path folderPath, String filter)return the list of wallets inside folder pathstatic File[]getFolderContent(Path folderPath)list file in the passed folderstatic PathgetHotmokaFilesDirectoryPath()static PathgetHotmokaTestDirectoryPath()static PathgetKeyWriterBucketDirectory(int epoch, int slot, String uid, FixedParameters.HexKeyWriter hkw)return the bucket directorystatic ConcurrentSkipListMap<String,MainAliasBean>getMainAliasList()static PathgetNodeNetworkSettings()static PathgetPublicKeyDirectoryPath()static PathgetQteslaReferenceAddressFolder(String addr)static PathgetQteslaReferenceFolder()static StringgetReferenceKeyStringNoSave(String qAddr)static PathgetSettingsPathFolder()static PathgetSimulationPathFolder()static PathgetSlotDirectory(int epochNumber, int slotNumber)return the path to the slot directorystatic PathgetStatePath(StatePointerBean spb)return the state path pointed by spbstatic PathgetTransactionBoxDateFolder(Date input)static PathgetTransactionBoxFolder(int epoch, int slot)static PathgetTransactionsDirectoryPath()static PathgetTransactionsDumpPathFolder()static PathgetTransactionsDumpPathFolder(String pidName)static PathgetUserWalletsDirectoryPath()return the default user wallets directorystatic ArrayList<String>getWalletList()static booleanhomeDirExists()static PathinitKeyHexWriterDirectoryTree(int epoch, int slot, String uid, FixedParameters.HexKeyWriter hkw)static voidinitProjectFiles()create project directory and folder structure using default parametersstatic PathlogFile(String pattern)convert pattern fromLog.getLogFilePattern()to pathstatic booleanlogsDirectoryExists()Log directorystatic voidmoveToSlot(int sourceEpoch, int sourceSlot, String sourceFile, int targetEpoch, int targetSlot, String targetFile)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 existsstatic booleanpublicKeyDirExists()static ObjectreadObjectFromFile(String filepath)static ObjectreadObjectFromFile(Path filePath)static StringreadStringFromFile(Path file)reads a string from the file indicated in the path if it existsstatic voidrename(Object sourcePath, Object targetPath, Boolean overwrite)moves the target file/folder to the new destination and can overwrite the existing file/folder if it existsstatic StringretriveQaddrByReference(String refAddr)static StringsaveQteslaAddress(String qAddr)static booleanslotDirectoryExists(int epochNumber, int slotNumber)Slot directorystatic booleantestMandatoryStatusBean(int epoch, int slot, String uid)static booleantransactionsDirExists()static booleanwalletDirExists()static booleanwalletEphemeralDirExists()static voidwriteFavouriteToBookmarkList(ConcurrentSkipListMap<String,FavouriteToBookmark> getListMapFavouriteToBookmark)static voidwriteObjectToFile(Path path, String sithNumber, Object input)static booleanwriteStringToFile(Path directory, String fileName, String content, boolean overwrite)Write a string to file
If file does not exist the string will be written to file and the function return true.static voidwriteTransactionBoxToFile(Path path, Object input)
-
-
-
Field Detail
-
KEYLOCK
public static final Object KEYLOCK
-
-
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:
-
getStatePath
public static Path getStatePath(StatePointerBean spb) throws IOException
return the state path pointed by spb- Parameters:
spb-- Returns:
- Throws:
IOException
-
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 fromLog.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:
-
initKeyHexWriterDirectoryTree
public static Path initKeyHexWriterDirectoryTree(int epoch, int slot, String uid, FixedParameters.HexKeyWriter hkw)
-
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:
-
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 usingPaths.get()- Parameters:
directoryPathAndName-- Throws:
IOException
-
getSettingsPathFolder
public static Path getSettingsPathFolder()
-
getTransactionsDumpPathFolder
public static Path getTransactionsDumpPathFolder()
-
getSimulationPathFolder
public static Path getSimulationPathFolder()
-
extractFromJar
public static void extractFromJar(String resourceName, Path destinationPath)
- Parameters:
resourceName- resource namedestinationPath- destination folder path
-
initProjectFiles
public static void initProjectFiles() throws IOExceptioncreate project directory and folder structure using default parameters- Throws:
IOException
-
getQteslaReferenceFolder
public static Path getQteslaReferenceFolder()
-
createIfNotExistQteslaReferenceFolder
public static Path createIfNotExistQteslaReferenceFolder(String addr)
-
getMainAliasList
public static ConcurrentSkipListMap<String,MainAliasBean> getMainAliasList()
-
writeFavouriteToBookmarkList
public static void writeFavouriteToBookmarkList(ConcurrentSkipListMap<String,FavouriteToBookmark> getListMapFavouriteToBookmark)
-
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 directoryfileName- file namecontent- string to be writed to fileoverwrite- 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
-
readStringFromFile
public static String readStringFromFile(Path file) throws FileNotFoundException
reads a string from the file indicated in the path if it exists- Parameters:
file-Path- Returns:
- Throws:
FileNotFoundException
-
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/PathtargetPath- can be of type String/File/Pathoverwrite- 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 ExceptionMoves 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
-
-