org.fuin.utils4j
Class Utils4J

java.lang.Object
  extended by org.fuin.utils4j.Utils4J

public final class Utils4J
extends java.lang.Object

Common utility methods for use in Java applications and libraries.


Nested Class Summary
static interface Utils4J.UnzipInputStreamWrapper
          Wraps a given input stream into another one an returns it.
 
Method Summary
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 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)
          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.For a description see http://www.cyanwerks.com/file-format-url.html.
static long dateToFileTime(java.util.Date date)
          Converts Date into a Windows FILETIME.
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.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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getPackagePath

public static java.lang.String getPackagePath(java.lang.Class clasz)
Returns the package path of a class.

Parameters:
clasz - Class to determine the path for.
Returns:
Package path for the class.

getResource

public 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.

Parameters:
clasz - Class with the same package where the resource is located.
name - Filename of the resource.
Returns:
Resource URL.

loadProperties

public static java.util.Properties loadProperties(java.lang.Class clasz,
                                                  java.lang.String filename)
Load properties from classpath.

Parameters:
clasz - Class in the same package as the properties file - Cannot be null.
filename - Name of the properties file (without path) - Cannot be null.
Returns:
Properties.

loadProperties

public static java.util.Properties loadProperties(java.io.File file)
Load properties from a file.

Parameters:
file - Properties file - Cannot be null.
Returns:
Properties.

checkValidFile

public static void checkValidFile(java.io.File file)
Check if the argument is an existing file. If the check fails an IllegalArgumentException is thrown.

Parameters:
file - File to check - Cannot be null.

checkValidDir

public static void checkValidDir(java.io.File dir)
Check if the argument is an existing directory. If the check fails an IllegalArgumentException is thrown.

Parameters:
dir - Directory to check - Cannot be null.

saveProperties

public static void saveProperties(java.io.File file,
                                  java.util.Properties props,
                                  java.lang.String comment)
Save properties to a file.

Parameters:
file - Destination file - Cannot be null.
props - Properties to save - Cannot be null.
comment - Comment for the file.

createInstance

public static java.lang.Object createInstance(java.lang.String className)
Create an instance with Class.forName(..) and wrap all exceptions into RuntimeExceptions.

Parameters:
className - Full qualified class name.
Returns:
New instance of the class.

addToClasspath

public static void addToClasspath(java.lang.String url)
Adds an URL to the classpath.

Parameters:
url - URL to add.

containsURL

public static boolean containsURL(java.net.URL[] urls,
                                  java.net.URL url)
Checks if the array or URLs contains the given URL.

Parameters:
urls - Array of URLs.
url - URL to find.
Returns:
If the URL is in the array TRUE else FALSE.

createHash

public static java.lang.String createHash(java.io.File file)
Creates an MD5 hash from a file.

Parameters:
file - File to create an hash for.
Returns:
Hash as text.

createUrl

public 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.

Parameters:
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.
Returns:
URL.

getRelativePath

public static java.lang.String getRelativePath(java.io.File baseDir,
                                               java.io.File dir)
Returns a relative path based on a base directory. If the dir is not inside baseDir an IllegalArgumentException is thrown.

Parameters:
baseDir - Base directory the path is relative to.
dir - Directory inside the base directory.
Returns:
Path of dir relative to baseDir. If both are equal an empty string is returned.

loadProperties

public static java.util.Properties loadProperties(java.net.URL baseUrl,
                                                  java.lang.String filename)
Load a file from an directory.

Parameters:
baseUrl - Directory URL.
filename - Filename without path.
Returns:
Properties.

loadProperties

public static java.util.Properties loadProperties(java.net.URL fileURL)
Load a file from an URL.

Parameters:
fileURL - Property file URL.
Returns:
Properties.

loadProperties

public static java.util.Properties loadProperties(java.lang.String baseUrl,
                                                  java.lang.String filename)
Load a file from an directory. Wraps a possible MalformedURLException exception into a RuntimeException.

Parameters:
baseUrl - Directory URL as String.
filename - Filename without path.
Returns:
Properties.

addToClasspath

public static void addToClasspath(java.net.URL url)
Adds an URL to the classpath.

Parameters:
url - URL to add.

checkNotNull

public 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.

Parameters:
name - Name of the variable to be displayed in an error message.
value - Value to check for null.

checkNotEmpty

public 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. A String with spaces is NOT considered empty!

Parameters:
name - Name of the variable to be displayed in an error message.
value - Value to check for an empty String - Cannot be null.

invoke

public static java.lang.Object invoke(java.lang.Object obj,
                                      java.lang.String methodName,
                                      java.lang.Class[] argTypes,
                                      java.lang.Object[] args)
                               throws InvokeMethodFailedException
Calls a method with reflection and maps all errors into one exception.

Parameters:
obj - The object the underlying method is invoked from.
methodName - Name of the Method.
argTypes - The list of parameters.
args - Arguments the arguments used for the method call.
Returns:
The result of dispatching the method represented by this object on obj with parameters args.
Throws:
InvokeMethodFailedException - Invoking the method failed for some reason.

unzip

public static void unzip(java.io.File zipFile,
                         java.io.File destDir)
                  throws java.io.IOException
Unzips a file into a given directory. WARNING: Only relative path entries are allowed inside the archive!

Parameters:
zipFile - Source ZIP file - Cannot be null and must be a valid ZIP file.
destDir - Destination directory - Cannot be null and must exist.
Throws:
java.io.IOException - Error unzipping the file.

unzip

public static void unzip(java.io.File zipFile,
                         java.io.File destDir,
                         Utils4J.UnzipInputStreamWrapper wrapper,
                         Cancelable cancelable)
                  throws java.io.IOException
Unzips a file into a given directory. WARNING: Only relative path entries are allowed inside the archive!

Parameters:
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.
Throws:
java.io.IOException - Error unzipping the file.

getUserHomeDir

public static java.io.File getUserHomeDir()
Returns the user home directory and checks if it is valid and exists. If not a IllegalStateException is thrown.

Returns:
Directory.

getTempDir

public static java.io.File getTempDir()
Returns the temporary directory and checks if it is valid and exists. If not a IllegalStateException is thrown.

Returns:
Directory.

replaceVars

public static java.lang.String replaceVars(java.lang.String str,
                                           java.util.Map vars)
Replaces all variables inside a string with values from a map.

Parameters:
str - Text with variables (Format: ${key} ).
vars - Map with key/values (both of type String.
Returns:
String with replaced variables. Unknown variables will remain unchanged.

dateToFileTime

public static long dateToFileTime(java.util.Date date)
Converts Date into a Windows FILETIME. The Windows FILETIME structure holds a date and time associated with a file. The structure identifies a 64-bit integer specifying the number of 100-nanosecond intervals which have passed since January 1, 1601. This code is copied from the org.apache.poi.hpsf.Util class.

Parameters:
date - The date to be converted
Returns:
The file time

createWindowsDesktopUrlLink

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
Creates an URL Link on the Windows Desktop. This is done by creating a file (URL File Format) with an ".url" extension. For a description see http://www.cyanwerks.com/file-format-url.html

Parameters:
baseUrl - Base URL for the link.
url - Target URL.
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=3
iconIndex - 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).
overwrite - Overwrite an existing ".url" file.
modified - Timestamp.
Throws:
java.io.IOException - Error writing the file.

createWindowsDesktopUrlLinkContent

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)
Creates the content of an URL Link file (.url) on the Windows Desktop.For a description see http://www.cyanwerks.com/file-format-url.html.

Parameters:
baseUrl - Base URL for the link.
url - Target URL.
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=3
iconIndex - 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.
Returns:
INI file text.


Copyright © 2009 Future Invent Informationsmanagement GmbH. All Rights Reserved.