Package net.sourceforge.jnlp.util
Class FileUtils
java.lang.Object
net.sourceforge.jnlp.util.FileUtils
This class contains a few file-related utility functions.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Indicates whether a file was successfully opened. -
Field Summary
FieldsModifier and TypeFieldDescriptionlist of characters not allowed in filenames -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Tries to create the ancestor directories of file f.static void
createParentDir
(File f, String eMsg) Tries to create the ancestor directories of file f.static void
createRestrictedDirectory
(File directory) Creates a new directory with minimum permissions.static void
createRestrictedFile
(File file, boolean writableByOwner) Creates a new file with minimum permissions.static void
Tries to delete file f.static void
deleteWithErrMesg
(File f, String eMsg) Tries to delete file f.static String
displayablePath
(String path) Returns a String that is suitable for using in GUI elements for displaying (long) paths to users.static String
displayablePath
(String path, int visibleChars) Return a String that is suitable for using in GUI elements for displaying paths to users.static String
static String
utility method which can read from any stream as one long Stringstatic String
getContentOfStream
(InputStream is, String encoding) utility method which can read from any stream as one long Stringstatic FileLock
getFileLock
(String path, boolean shared, boolean allowBlock) This will return a lock to the file specified.static byte[]
getFileMD5Sum
(File file, String algorithm) static String
static String
loadFileAsString
(File f, String encoding) static void
recursiveDelete
(File file, File base) Recursively delete everything under a directory.static String
sanitizeFileName
(String filename) Given an input, return a sanitized form of the input suitable for use as a file/directory namestatic String
sanitizeFileName
(String filename, char substitute) static String
sanitizePath
(String path) Clean up a string by removing characters that can't appear in a local file name.static String
sanitizePath
(String path, char substitute) static void
Method to save String as file in UTF-8 encoding.static void
Method to save String as file in specified encoding/.static void
showCouldNotOpenDialog
(Component frame, String message) Show a dialog informing the user that the file could not be openedstatic void
showCouldNotOpenFileDialog
(Component frame, String filePath, FileUtils.OpenFileResult reason) Show an error dialog indicating the file could not be opened, with a particular reasonstatic void
showCouldNotOpenFilepathDialog
(Component frame, String filePath) Show a generic error dialog indicating the file could not be openedstatic void
showReadOnlyDialog
(Component frame) Show a dialog informing the user that the file is currently read-onlytestDirectoryPermissions
(File file) Ensure that the parent directory of the file exists and that we are able to create and access files within this directorystatic FileUtils.OpenFileResult
testFilePermissions
(File file) Verify that a given file object points to a real, accessible plain file.
-
Field Details
-
INVALID_PATH
list of characters not allowed in filenames -
INVALID_NAME
-
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
sanitizePath
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
-
sanitizeFileName
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
-
createRestrictedDirectory
Creates a new directory with minimum permissions. The directory is not readable or writable by anyone other than the owner. The parent directories are not created; they must exist before this is called.- Parameters:
directory
- directory to be created- Throws:
IOException
- if IO fails
-
createRestrictedFile
Creates a new file with minimum permissions. The file is not readable or writable by anyone other than the owner. If writeableByOnwer is false, even the owner can not write to it.- Parameters:
file
- path to filewritableByOwner
- true if can be writable by owner- Throws:
IOException
- if IO fails
-
createParentDir
Tries 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:
IOException
- if the directory can't be created and doesn't exist.
-
createParentDir
Tries 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:
IOException
- if the directory can't be created and doesn't exist.
-
deleteWithErrMesg
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).
-
deleteWithErrMesg
Tries to delete file f. If the file exists but couldn't be deleted, print an error message to stderr with the file name.- Parameters:
f
- the file to be deleted
-
testDirectoryPermissions
Ensure that the parent directory of the file exists and that we are able to create and access files within this directory- Parameters:
file
- theFile
representing a Java Policy file to test- Returns:
- a
DirectoryValidator.DirectoryCheckResults
object representing the results of the test
-
testFilePermissions
Verify that a given file object points to a real, accessible plain file.- Parameters:
file
- theFile
to verify- Returns:
- an
FileUtils.OpenFileResult
representing the accessibility level of the file
-
showReadOnlyDialog
Show a dialog informing the user that the file is currently read-only- Parameters:
frame
- aJFrame
to act as parent to this dialog
-
showCouldNotOpenFilepathDialog
Show a generic error dialog indicating the file could not be opened -
showCouldNotOpenFileDialog
public static void showCouldNotOpenFileDialog(Component frame, String filePath, FileUtils.OpenFileResult reason) Show an error dialog indicating the file could not be opened, with a particular reason- Parameters:
frame
- aJFrame
to act as parent to this dialogfilePath
- aString
representing the path to the file we failed to openreason
- aFileUtils.OpenFileResult
specifying more precisely why we failed to open the file
-
showCouldNotOpenDialog
Show a dialog informing the user that the file could not be opened -
displayablePath
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
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
Recursively 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:
IOException
- on an io exception or if trying to delete something outside the base
-
getFileLock
public static FileLock getFileLock(String path, boolean shared, boolean allowBlock) throws FileNotFoundException This 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:
FileNotFoundException
- If the file does not exist.
-
saveFile
Method to save String as file in UTF-8 encoding.- Parameters:
content
- which will be saved as it is saved in this Stringf
- file to be saved. No warnings provided- Throws:
IOException
- if save fails
-
saveFile
Method to save String as file in specified encoding/.- Parameters:
content
- which will be saved as it is saved in this Stringf
- file to be saved. No warnings providedencoding
- of output byte representation- Throws:
IOException
- if save fails
-
getContentOfStream
utility method which can read from any stream as one long String- Parameters:
is
- streamencoding
- the encoding to use to convert the bytes from the stream- Returns:
- stream as string
- Throws:
IOException
- if connection can't be established or resource does not exist
-
getContentOfReader
- Throws:
IOException
-
getContentOfStream
utility method which can read from any stream as one long String- Parameters:
is
- stream- Returns:
- stream as string
- Throws:
IOException
- if connection can't be established or resource does not exist
-
loadFileAsString
- Throws:
IOException
-
loadFileAsString
- Throws:
IOException
-
getFileMD5Sum
public static byte[] getFileMD5Sum(File file, String algorithm) throws NoSuchAlgorithmException, FileNotFoundException, IOException
-