public final class Utils4J
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
Utils4J.UnzipInputStreamWrapper
Wraps a given input stream into another one an returns it.
|
Modifier and Type | Method and Description |
---|---|
static void |
addToClasspath(java.lang.String url)
Adds an URL to the classpath.
|
static void |
addToClasspath(java.net.URL url)
Adds an URL to the classpath.
|
static void |
checkNotEmpty(java.lang.String name,
java.lang.String value)
Checks if a variable is not
empty and throws an
IllegalNullArgumentException if this rule is violated. |
static void |
checkNotNull(java.lang.String name,
java.lang.Object value)
Checks if a variable is not
null and throws an
IllegalNullArgumentException if this rule is violated. |
static void |
checkValidDir(java.io.File dir)
Check if the argument is an existing directory.
|
static void |
checkValidFile(java.io.File file)
Check if the argument is an existing file.
|
static java.lang.String |
concatPathAndFilename(java.lang.String path,
java.lang.String filename,
java.lang.String separator)
Concatenate a path and a filename taking
null and empty
string values into account. |
static boolean |
containsURL(java.net.URL[] urls,
java.net.URL url)
Checks if the array or URLs contains the given URL.
|
static java.lang.String |
createHash(java.io.File file)
Deprecated.
Use
createHashMD5 instead. |
static java.lang.String |
createHash(java.io.File file,
java.lang.String algorithm)
Creates a HEX encoded hash from a file.
|
static java.lang.String |
createHash(java.io.InputStream inputStream,
java.lang.String algorithm)
Creates a HEX encoded hash from a stream.
|
static java.lang.String |
createHashMD5(java.io.File file)
Creates an MD5 hash from a file.
|
static java.lang.Object |
createInstance(java.lang.String className)
Create an instance with Class.forName(..) and wrap all exceptions into
RuntimeExceptions.
|
static java.net.URL |
createUrl(java.net.URL baseUrl,
java.lang.String path,
java.lang.String filename)
Creates an URL based on a directory a relative path and a filename.
|
static void |
createWindowsDesktopUrlLink(java.lang.String baseUrl,
java.lang.String url,
java.io.File workingDir,
java.lang.Integer showCommand,
java.lang.Integer iconIndex,
java.io.File iconFile,
java.lang.Integer hotKey,
java.lang.String linkFilenameWithoutExtension,
boolean overwrite,
java.util.Date modified)
Creates an URL Link on the Windows Desktop.
|
static java.lang.String |
createWindowsDesktopUrlLinkContent(java.lang.String baseUrl,
java.lang.String url,
java.io.File workingDir,
java.lang.Integer showCommand,
java.lang.Integer iconIndex,
java.io.File iconFile,
java.lang.Integer hotKey,
java.util.Date modified)
Creates the content of an URL Link file (.url) on the Windows Desktop.
|
static long |
dateToFileTime(java.util.Date date)
Converts Date into a Windows FILETIME.
|
static byte[] |
decodeHex(java.lang.String data)
Converts an array of characters representing hexidecimal values into an
array of bytes of those same values.
|
static java.lang.String |
encodeHex(byte[] data)
Converts an array of bytes into an array of characters representing the
hexidecimal values of each byte in order.
|
static java.lang.String |
getPackagePath(java.lang.Class clasz)
Returns the package path of a class.
|
static java.lang.String |
getRelativePath(java.io.File baseDir,
java.io.File dir)
Returns a relative path based on a base directory.
|
static java.net.URL |
getResource(java.lang.Class clasz,
java.lang.String name)
Get the path to a resource located in the same package as a given class.
|
static java.io.File |
getTempDir()
Returns the temporary directory and checks if it is valid and exists.
|
static java.io.File |
getUserHomeDir()
Returns the user home directory and checks if it is valid and exists.
|
static java.lang.Object |
invoke(java.lang.Object obj,
java.lang.String methodName,
java.lang.Class[] argTypes,
java.lang.Object[] args)
Calls a method with reflection and maps all errors into one exception.
|
static java.util.Properties |
loadProperties(java.lang.Class clasz,
java.lang.String filename)
Load properties from classpath.
|
static java.util.Properties |
loadProperties(java.io.File file)
Load properties from a file.
|
static java.util.Properties |
loadProperties(java.lang.String baseUrl,
java.lang.String filename)
Load a file from an directory.
|
static java.util.Properties |
loadProperties(java.net.URL fileURL)
Load a file from an URL.
|
static java.util.Properties |
loadProperties(java.net.URL baseUrl,
java.lang.String filename)
Load a file from an directory.
|
static java.nio.channels.FileLock |
lockRandomAccessFile(java.io.RandomAccessFile file,
int tryLockMax,
long tryWaitMillis)
Lock the file.
|
static java.lang.String |
replaceVars(java.lang.String str,
java.util.Map vars)
Replaces all variables inside a string with values from a map.
|
static void |
saveProperties(java.io.File file,
java.util.Properties props,
java.lang.String comment)
Save properties to a file.
|
static void |
unzip(java.io.File zipFile,
java.io.File destDir)
Unzips a file into a given directory.
|
static void |
unzip(java.io.File zipFile,
java.io.File destDir,
Utils4J.UnzipInputStreamWrapper wrapper,
Cancelable cancelable)
Unzips a file into a given directory.
|
static void |
zipDir(java.io.File srcDir,
java.io.FileFilter filter,
java.lang.String destPath,
java.io.File destFile)
Creates a ZIP file and adds all files in a directory and all it's sub
directories to the archive.
|
static void |
zipDir(java.io.File srcDir,
java.lang.String destPath,
java.io.File destFile)
Creates a ZIP file and adds all files in a directory and all it's sub
directories to the archive.
|
public static java.lang.String getPackagePath(java.lang.Class clasz)
clasz
- Class to determine the path for - Cannot be null
.public static java.net.URL getResource(java.lang.Class clasz, java.lang.String name)
clasz
- Class with the same package where the resource is located -
Cannot be null
.name
- Filename of the resource - Cannot be null
.public static java.util.Properties loadProperties(java.lang.Class clasz, java.lang.String filename)
clasz
- Class in the same package as the properties file - Cannot be
null
.filename
- Name of the properties file (without path) - Cannot be
null
.public static java.util.Properties loadProperties(java.io.File file)
file
- Properties file - Cannot be null
and must be a
valid file.public static void checkValidFile(java.io.File file)
IllegalArgumentException
is thrown.file
- File to check - Cannot be null
.public static void checkValidDir(java.io.File dir)
IllegalArgumentException
is thrown.dir
- Directory to check - Cannot be null
.public static void saveProperties(java.io.File file, java.util.Properties props, java.lang.String comment)
file
- Destination file - Cannot be null
and parent
directory must exist.props
- Properties to save - Cannot be null
.comment
- Comment for the file.public static java.lang.Object createInstance(java.lang.String className)
className
- Full qualified class name - Cannot be null
.public static void addToClasspath(java.lang.String url)
url
- URL to add - Cannot be null
.public static boolean containsURL(java.net.URL[] urls, java.net.URL url)
urls
- Array of URLs - Cannot be null
.url
- URL to find - Cannot be null
.public static java.lang.String createHash(java.io.File file)
createHashMD5
instead.file
- File to create an hash for - Cannot be null
.public static java.lang.String createHashMD5(java.io.File file)
file
- File to create an hash for - Cannot be null
.public static java.lang.String createHash(java.io.File file, java.lang.String algorithm)
file
- File to create a hash for - Cannot be null
.algorithm
- Hash algorithm like "MD5" or "SHA" - Cannot be
null
.public static java.lang.String createHash(java.io.InputStream inputStream, java.lang.String algorithm)
inputStream
- Stream to create a hash for - Cannot be null
.algorithm
- Hash algorithm like "MD5" or "SHA" - Cannot be
null
.public static java.net.URL createUrl(java.net.URL baseUrl, java.lang.String path, java.lang.String filename)
baseUrl
- Directory URL with or without slash ("/") at the end of the
string - Cannot be null
.path
- Relative path inside the base URL (with or without slash ("/")
at the end of the string) - Can be null
or an
empty string.filename
- Filename without path - Cannot be null
.public static java.lang.String getRelativePath(java.io.File baseDir, java.io.File dir)
dir
is not inside baseDir
an
IllegalArgumentException
is thrown.baseDir
- Base directory the path is relative to - Cannot be
null
.dir
- Directory inside the base directory - Cannot be
null
.dir
relative to baseDir
. If
both are equal an empty string is returned.public static java.util.Properties loadProperties(java.net.URL baseUrl, java.lang.String filename)
baseUrl
- Directory URL - Cannot be null
.filename
- Filename without path - Cannot be null
.public static java.util.Properties loadProperties(java.net.URL fileURL)
fileURL
- Property file URL - Cannot be null
.public static java.util.Properties loadProperties(java.lang.String baseUrl, java.lang.String filename)
MalformedURLException
exception into a
RuntimeException
.baseUrl
- Directory URL as String
- Cannot be
null
.filename
- Filename without path - Cannot be null
.public static void addToClasspath(java.net.URL url)
url
- URL to add - Cannot be null
.public static void checkNotNull(java.lang.String name, java.lang.Object value)
null
and throws an
IllegalNullArgumentException
if this rule is violated.name
- Name of the variable to be displayed in an error message.value
- Value to check for null
.public static void checkNotEmpty(java.lang.String name, java.lang.String value)
empty
and throws an
IllegalNullArgumentException
if this rule is violated. A
String with spaces is NOT considered empty!name
- Name of the variable to be displayed in an error message.value
- Value to check for an empty String - Cannot be
null
.public static java.lang.Object invoke(java.lang.Object obj, java.lang.String methodName, java.lang.Class[] argTypes, java.lang.Object[] args) throws InvokeMethodFailedException
obj
- The object the underlying method is invoked from - Cannot be
null
.methodName
- Name of the Method - Cannot be null
.argTypes
- The list of parameters - May be null
.args
- Arguments the arguments used for the method call - May be
null
if "argTypes" is also null
.obj
with parameters args
.InvokeMethodFailedException
- Invoking the method failed for some reason.public static void unzip(java.io.File zipFile, java.io.File destDir) throws java.io.IOException
zipFile
- Source ZIP file - Cannot be null
and must be a
valid ZIP file.destDir
- Destination directory - Cannot be null
and must
exist.java.io.IOException
- Error unzipping the file.public static void unzip(java.io.File zipFile, java.io.File destDir, Utils4J.UnzipInputStreamWrapper wrapper, Cancelable cancelable) throws java.io.IOException
zipFile
- Source ZIP file - Cannot be null
and must be a
valid ZIP file.destDir
- Destination directory - Cannot be null
and must
exist.wrapper
- Callback interface to give the caller the chance to wrap the
ZIP input stream into another one. This is useful for example
to display a progress bar - Can be null
if no
wrapping is required.cancelable
- Signals if the unzip should be canceled - Can be
null
if no cancel option is required.java.io.IOException
- Error unzipping the file.public static java.io.File getUserHomeDir()
IllegalStateException
is thrown.public static java.io.File getTempDir()
IllegalStateException
is thrown.public static java.lang.String replaceVars(java.lang.String str, java.util.Map vars)
str
- Text with variables (Format: ${key} ) - May be
null
or empty.vars
- Map with key/values (both of type String
- Cannot
be null
.public static long dateToFileTime(java.util.Date date)
org.apache.poi.hpsf.Util
class.date
- The date to be converted - Cannot be null
.public static void createWindowsDesktopUrlLink(java.lang.String baseUrl, java.lang.String url, java.io.File workingDir, java.lang.Integer showCommand, java.lang.Integer iconIndex, java.io.File iconFile, java.lang.Integer hotKey, java.lang.String linkFilenameWithoutExtension, boolean overwrite, java.util.Date modified) throws java.io.IOException
baseUrl
- Base URL for the link - Cannot be null
or empty.url
- Target URL - Cannot be null
or empty.workingDir
- It's the "working folder" that your URL file uses. The working
folder is possibly the folder to be set as the current folder
for the application that would open the file. However Internet
Explorer does not seem to be affected by this field - Can be
null
.showCommand
- Normal=null
, Minimized=7, Maximized=3iconIndex
- The Icon Index within the icon library specified by IconFile.
In an icon library, which can be generally be either a ICO,
DLL or EXE file, the icons are indexed with numbers. The first
icon index starts at 0 - Can be null
if the file
is not indexed.iconFile
- Specifies the path of the icon library file. Generally the
icon library can be an ICO, DLL or EXE file. The default icon
library used tends to be the URL.DLL library on the system's
Windows\System directory - Can be null
if no icon
is required.hotKey
- The HotKey field specifies what is the shortcut key used to
automatically launch the Internet shortcut. The field uses a
number to specify what hotkey is used. To get the appropriate
code simply create a shortcut with MSIE and examine the file's
content.linkFilenameWithoutExtension
- Name for the link file (displayed as text) - Cannot be
null
or empty.overwrite
- Overwrite an existing ".url" file.modified
- Timestamp.java.io.IOException
- Error writing the file.public static java.lang.String createWindowsDesktopUrlLinkContent(java.lang.String baseUrl, java.lang.String url, java.io.File workingDir, java.lang.Integer showCommand, java.lang.Integer iconIndex, java.io.File iconFile, java.lang.Integer hotKey, java.util.Date modified)
baseUrl
- Base URL for the link - Cannot be null
or empty.url
- Target URL - Cannot be null
or empty.workingDir
- It's the "working folder" that your URL file uses. The working
folder is possibly the folder to be set as the current folder
for the application that would open the file. However Internet
Explorer does not seem to be affected by this field - Can be
null
.showCommand
- Normal=null
, Minimized=7, Maximized=3iconIndex
- The Icon Index within the icon library specified by IconFile.
In an icon library, which can be generally be either a ICO,
DLL or EXE file, the icons are indexed with numbers. The first
icon index starts at 0 - Can be null
if the file
is not indexed.iconFile
- Specifies the path of the icon library file. Generally the
icon library can be an ICO, DLL or EXE file. The default icon
library used tends to be the URL.DLL library on the system's
Windows\System directory - Can be null
if no icon
is required.hotKey
- The HotKey field specifies what is the shortcut key used to
automatically launch the Internet shortcut. The field uses a
number to specify what hotkey is used. To get the appropriate
code simply create a shortcut with MSIE and examine the file's
content.modified
- Timestamp.public static java.lang.String concatPathAndFilename(java.lang.String path, java.lang.String filename, java.lang.String separator)
null
and empty
string values into account.path
- Path - Can be null
or an empty string.filename
- Filename - Cannot be null
.separator
- Separator for directories - Can be null
or an
empty string.public static java.lang.String encodeHex(byte[] data)
data
- A byte[] to convert to Hex characters - Cannot be
null
.public static byte[] decodeHex(java.lang.String data)
data
- An array of characters containing hexidecimal digits - Cannot
be null
.public static java.nio.channels.FileLock lockRandomAccessFile(java.io.RandomAccessFile file, int tryLockMax, long tryWaitMillis) throws LockingFailedException
file
- File to lock - Cannot be null
.tryLockMax
- Number of tries to lock before throwing an exception.tryWaitMillis
- Milliseconds to sleep between retries.LockingFailedException
- Locking the file failed.public static void zipDir(java.io.File srcDir, java.io.FileFilter filter, java.lang.String destPath, java.io.File destFile) throws java.io.IOException
srcDir
- Directory to add - Cannot be null
and must be a
valid directory.filter
- Filter or null
for all files/directories.destPath
- Path to use for the ZIP archive - May be null
or
an empyt string.destFile
- Target ZIP file - Cannot be null
.java.io.IOException
- Error writing to the output stream.public static void zipDir(java.io.File srcDir, java.lang.String destPath, java.io.File destFile) throws java.io.IOException
srcDir
- Directory to add - Cannot be null
and must be a
valid directory.destPath
- Path to use for the ZIP archive - May be null
or
an empyt string.destFile
- Target ZIP file - Cannot be null
.java.io.IOException
- Error writing to the output stream.