Package net.adoptopenjdk.icedteaweb.io
Class FileUtils
- java.lang.Object
-
- net.adoptopenjdk.icedteaweb.io.FileUtils
-
public final class FileUtils extends java.lang.ObjectThis class contains a few file-related utility functions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFileUtils.OpenFileResultIndicates whether a file was successfully opened.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.List<java.lang.Character>INVALID_NAME(package private) static java.util.List<java.lang.Character>INVALID_PATHlist of characters not allowed in filenamesprivate static LoggerLOGprivate static java.lang.StringMD5private static charSANITIZED_CHARprivate static java.lang.StringWIN_DRIVE_LETTER_COLON_WILDCHAR
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcreateParentDir(java.io.File f)Tries to create the ancestor directories of file f.static voidcreateParentDir(java.io.File f, java.lang.String eMsg)Tries to create the ancestor directories of file f.static voiddeleteWithErrMesg(java.io.File f, java.lang.String eMsg)Tries to delete file f.static java.lang.StringdisplayablePath(java.lang.String path)Returns a String that is suitable for using in GUI elements for displaying (long) paths to users.static java.lang.StringdisplayablePath(java.lang.String path, int visibleChars)Return a String that is suitable for using in GUI elements for displaying paths to users.static java.nio.channels.FileLockgetFileLock(java.lang.String path, boolean shared, boolean allowBlock)This will return a lock to the file specified.static byte[]getFileMD5Sum(java.io.File file)static java.lang.StringloadFileAsString(java.io.File f, java.nio.charset.Charset encoding)static java.lang.StringloadFileAsUtf8String(java.io.File f)static voidrecursiveDelete(java.io.File file, java.io.File base)Recursively delete everything under a directory.static java.lang.StringsanitizeFileName(java.lang.String filename)Given an input, return a sanitized form of the input suitable for use as a file/directory namestatic java.lang.StringsanitizeFileName(java.lang.String filename, char substitute)static java.lang.StringsanitizePath(java.lang.String path)Clean up a string by removing characters that can't appear in a local file name.static java.lang.StringsanitizePath(java.lang.String path, char substitute)static voidsaveFileUtf8(java.lang.String content, java.io.File f)Save String into a file in UTF-8 encoding.private static DirectoryCheckResultstestDirectoryPermissions(java.io.File file, boolean isDebug)Ensure that the parent directory of the file exists and that we are able to create and access files within this directorystatic FileUtils.OpenFileResulttestFilePermissions(java.io.File file, boolean isDebug)Verify that a given file object points to a real, accessible plain file.
-
-
-
Field Detail
-
LOG
private static final Logger LOG
-
MD5
private static final java.lang.String MD5
- See Also:
- Constant Field Values
-
WIN_DRIVE_LETTER_COLON_WILDCHAR
private static final java.lang.String WIN_DRIVE_LETTER_COLON_WILDCHAR
- See Also:
- Constant Field Values
-
INVALID_PATH
static final java.util.List<java.lang.Character> INVALID_PATH
list of characters not allowed in filenames
-
INVALID_NAME
private static final java.util.List<java.lang.Character> INVALID_NAME
-
SANITIZED_CHAR
private static final char SANITIZED_CHAR
- See Also:
- Constant Field Values
-
-
Method Detail
-
sanitizePath
public static java.lang.String sanitizePath(java.lang.String path)
Clean up a string by removing characters that can't appear in a local file name.- Parameters:
path- the path to sanitize- Returns:
- a sanitized version of the input which is suitable for using as a file path
-
sanitizePath
public static java.lang.String sanitizePath(java.lang.String path, char substitute)
-
sanitizeFileName
public static java.lang.String sanitizeFileName(java.lang.String filename)
Given an input, return a sanitized form of the input suitable for use as a file/directory name- Parameters:
filename- the filename to sanitize.- Returns:
- a sanitized version of the input
-
sanitizeFileName
public static java.lang.String sanitizeFileName(java.lang.String filename, char substitute)
-
createParentDir
public static void createParentDir(java.io.File f, java.lang.String eMsg) throws java.io.IOExceptionTries to create the ancestor directories of file f. Throws an IOException if it can't be created (but not if it was already there).- Parameters:
f- file to provide parent directoryeMsg- - the message to use for the exception. null if the file name is to be used.- Throws:
java.io.IOException- if the directory can't be created and doesn't exist.
-
createParentDir
public static void createParentDir(java.io.File f) throws java.io.IOExceptionTries to create the ancestor directories of file f. Throws an IOException if it can't be created (but not if it was already there).- Parameters:
f- file which parent will be created- Throws:
java.io.IOException- if the directory can't be created and doesn't exist.
-
deleteWithErrMesg
public static void deleteWithErrMesg(java.io.File f, java.lang.String eMsg)Tries to delete file f. If the file exists but couldn't be deleted, print an error message to stderr with the file name, or eMsg if eMsg is not null.- Parameters:
f- the file to be deletedeMsg- the message to print on failure (or null to print the the file name).
-
testDirectoryPermissions
private static DirectoryCheckResults testDirectoryPermissions(java.io.File file, boolean isDebug)
Ensure that the parent directory of the file exists and that we are able to create and access files within this directory- Parameters:
file- theFilerepresenting a Java Policy file to testisDebug- output debug information- Returns:
- a
DirectoryCheckResultsobject representing the results of the test
-
testFilePermissions
public static FileUtils.OpenFileResult testFilePermissions(java.io.File file, boolean isDebug)
Verify that a given file object points to a real, accessible plain file.- Parameters:
file- theFileto verifyisDebug- output debug information- Returns:
- an
FileUtils.OpenFileResultrepresenting the accessibility level of the file
-
displayablePath
public static java.lang.String displayablePath(java.lang.String path)
Returns a String that is suitable for using in GUI elements for displaying (long) paths to users.- Parameters:
path- a path that should be shortened- Returns:
- a shortened path suitable for displaying to the user
-
displayablePath
public static java.lang.String displayablePath(java.lang.String path, int visibleChars)Return a String that is suitable for using in GUI elements for displaying paths to users. If the path is longer than visibleChars, it is truncated in a display-friendly way- Parameters:
path- a path that should be shortedvisibleChars- the maximum number of characters that path should fit into. Also the length of the returned string- Returns:
- a shortened path that contains limited number of chars
-
recursiveDelete
public static void recursiveDelete(java.io.File file, java.io.File base) throws java.io.IOExceptionRecursively delete everything under a directory. Works on either files or directories- Parameters:
file- the file object representing what to delete. Can be either a file or a directory.base- the directory under which the file and its subdirectories must be located- Throws:
java.io.IOException- on an io exception or if trying to delete something outside the base
-
getFileLock
public static java.nio.channels.FileLock getFileLock(java.lang.String path, boolean shared, boolean allowBlock) throws java.io.FileNotFoundExceptionThis will return a lock to the file specified.- Parameters:
path- File path to file we want to lock.shared- Specify if the lock will be a shared lock.allowBlock- Specify if we should block when we can not get the lock. Getting a shared lock will always block.- Returns:
- FileLock if we were successful in getting a lock, otherwise null.
- Throws:
java.io.FileNotFoundException- If the file does not exist.
-
loadFileAsUtf8String
public static java.lang.String loadFileAsUtf8String(java.io.File f) throws java.io.IOException- Throws:
java.io.IOException
-
loadFileAsString
public static java.lang.String loadFileAsString(java.io.File f, java.nio.charset.Charset encoding) throws java.io.IOException- Throws:
java.io.IOException
-
saveFileUtf8
public static void saveFileUtf8(java.lang.String content, java.io.File f) throws java.io.IOExceptionSave String into a file in UTF-8 encoding.- Parameters:
content- which will be saved to the filef- file to be saved. No warnings provided- Throws:
java.io.IOException- if save fails
-
getFileMD5Sum
public static byte[] getFileMD5Sum(java.io.File file) throws java.security.NoSuchAlgorithmException, java.io.IOException- Throws:
java.security.NoSuchAlgorithmExceptionjava.io.IOException
-
-