nl.lxtreme.ols.util
Class HostUtils

java.lang.Object
  extended by nl.lxtreme.ols.util.HostUtils
All Implemented Interfaces:
HostInfo

public final class HostUtils
extends Object
implements HostInfo

Provides some host/OS specific utilities.


Method Summary
static boolean closeResource(Closeable aResource)
          Closes a given resource.
static File createLocalDataFile(String aName, String aExtension)
          Creates an OS-specific file location to store data.
static File createLocalPropertiesFile(String aName)
          Creates an OS-specific file location to store properties.
static void flushInputStream(InputStream aResource)
          Flushes the given input stream by reading as many bytes as there are still available.
static String getFileExtension(File aFile)
          Returns the "presumed" filename extension (like '.jpg', '.zip') from a given file.
static HostInfo getHostInfo()
          Returns the current value of hostinfo.
static boolean handleInterruptedException(Throwable aThrowable)
          This method calls Thread.currentThread().interrupt() if any exception in the hierarchy (including all parent causes) is either an InterruptedIOException or InterruptedException.
static void initLogging(InputStream aInputStream)
          Allows the logging properties of the JVM to be set at any moment in time providing the logging configuration in an input-stream.
 boolean isLinux()
          Returns whether the current host's operating system is Linux or any other UNIX-like operating system, such as Solaris (SunOS).
 boolean isMacOS()
          Returns whether the current host's operating system is Mac OS X.
 boolean isSolaris()
          Returns whether the current host's operating system is Sun/Open Solaris.
 boolean isUnix()
          Returns whether the current host's operating system is Linux or any other UNIX-like operating system, such as Solaris (SunOS).
 boolean isWindows()
          Returns whether the current host's operating system is Windows.
 boolean needsAboutMenuItem()
          
 boolean needsExitMenuItem()
          
 boolean needsPreferencesMenuItem()
          
static void reverse(int[] aArray)
          Reverses the elements in the given array.
static
<T> void
reverse(T[] aArray)
          Reverses the elements in the given array.
static File setFileExtension(File aFile, String aFileExtension)
          Sets the filename to end with the given file extension, if this is not already the case.
static String stripFileExtension(File aFile, String... aExtensions)
          Returns the "presumed" filename extension (like '.jpg', '.zip') from a given file.
static String stripFileExtension(String aFilename, String... aExtensions)
          Returns the "presumed" filename extension (like '.jpg', '.zip') from a given file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

closeResource

public static final boolean closeResource(Closeable aResource)
Closes a given resource.

If the given resource also implements the Flushable interface, the resource is flushed before being closed.

Parameters:
aResource - the resource to close, can be null, it might already be closed.
Returns:
true if the close operation succeeded, false if it is unsure whether it succeeded.

createLocalDataFile

public static final File createLocalDataFile(String aName,
                                             String aExtension)
Creates an OS-specific file location to store data.

Parameters:
aName - the name of the data file, excluding the file extension, cannot be null or empty;
aExtension - the extension of the data file to use, note that this is an indication an might not be used for a particular host operating system.
Returns:
the file pointing to the OS-specific properties file location, never null.

createLocalPropertiesFile

public static final File createLocalPropertiesFile(String aName)
Creates an OS-specific file location to store properties.

Parameters:
aName - the name of the properties file, excluding .properties, cannot be null or empty. By convention, the name of a properties file should be in the "reverse package name", e.g., "com.foo.bar".
Returns:
the file pointing to the OS-specific properties file location, never null.

flushInputStream

public static final void flushInputStream(InputStream aResource)
                                   throws IOException
Flushes the given input stream by reading as many bytes as there are still available.

Parameters:
aResource - the resource to flush, can be null.
Throws:
IOException - in case of I/O problems/

getFileExtension

public static final String getFileExtension(File aFile)
Returns the "presumed" filename extension (like '.jpg', '.zip') from a given file.

Parameters:
aFile - the file to return the extension for, cannot be null.
Returns:
the file extension (always in lower case), never null but can be empty if the given file has no file extension.

getHostInfo

public static HostInfo getHostInfo()
Returns the current value of hostinfo.

Returns:
the host information, never null.

handleInterruptedException

public static boolean handleInterruptedException(Throwable aThrowable)
This method calls Thread.currentThread().interrupt() if any exception in the hierarchy (including all parent causes) is either an InterruptedIOException or InterruptedException. This method should be called in every catch(IOException), catch(Exception) or catch(Throwable) block.

Parameters:
aThrowable - the exception to be checked for interruption. Does nothing if null.
Returns:
true if the exception is "handled" by this method, false otherwise.

initLogging

public static final void initLogging(InputStream aInputStream)
Allows the logging properties of the JVM to be set at any moment in time providing the logging configuration in an input-stream.

Parameters:
aInputStream - the input stream providing the logging properties, cannot be null.

reverse

public static final void reverse(int[] aArray)
Reverses the elements in the given array.

Parameters:
aArray - the array to reverse, cannot be null.
Throws:
IllegalArgumentException - in case the given array was null.

reverse

public static final <T> void reverse(T[] aArray)
Reverses the elements in the given array.

Parameters:
aArray - the array to reverse, cannot be null.
Throws:
IllegalArgumentException - in case the given array was null.

setFileExtension

public static final File setFileExtension(File aFile,
                                          String aFileExtension)
Sets the filename to end with the given file extension, if this is not already the case.

Parameters:
aFile - the file that should get the given file extension, cannot be null;
aFileExtension - the new file extension to add to the given file, cannot be null.
Returns:
a file with the given file extension, never null.

stripFileExtension

public static final String stripFileExtension(File aFile,
                                              String... aExtensions)
Returns the "presumed" filename extension (like '.jpg', '.zip') from a given file.

Parameters:
aFile - the file to return the extension for, cannot be null.
Returns:
the file extension (always in lower case), never null but can be empty if the given file has no file extension.

stripFileExtension

public static final String stripFileExtension(String aFilename,
                                              String... aExtensions)
Returns the "presumed" filename extension (like '.jpg', '.zip') from a given file.

Parameters:
aFilename - the name of the file to strip the extension from, cannot be null;
aExtensions - the (optional) extensions that should be stripped. If omitted, everything behind the last dot will be removed.
Returns:
the file extension (always in lower case), never null but can be empty if the given file has no file extension.

isLinux

public boolean isLinux()
Returns whether the current host's operating system is Linux or any other UNIX-like operating system, such as Solaris (SunOS).

Specified by:
isLinux in interface HostInfo
Returns:
true if running on Linux or any other UNIX system, false otherwise.

isMacOS

public boolean isMacOS()
Returns whether the current host's operating system is Mac OS X.

Specified by:
isMacOS in interface HostInfo
Returns:
true if running on Mac OS X, false otherwise.

isSolaris

public boolean isSolaris()
Returns whether the current host's operating system is Sun/Open Solaris.

Specified by:
isSolaris in interface HostInfo
Returns:
true if running on Sun/Open Solaris system, false otherwise.

isUnix

public boolean isUnix()
Returns whether the current host's operating system is Linux or any other UNIX-like operating system, such as Solaris (SunOS).

Specified by:
isUnix in interface HostInfo
Returns:
true if running on Linux or any other UNIX system, false otherwise.

isWindows

public boolean isWindows()
Returns whether the current host's operating system is Windows.

Specified by:
isWindows in interface HostInfo
Returns:
true if running on Windows, false otherwise.

needsAboutMenuItem

public boolean needsAboutMenuItem()

Specified by:
needsAboutMenuItem in interface HostInfo
Returns:
true if an explicit about menu item needs to be added to a UI created on the running host, false if the running host already provides an about menu item by default.

needsExitMenuItem

public final boolean needsExitMenuItem()

Specified by:
needsExitMenuItem in interface HostInfo
Returns:
true if an explicit exit menu item needs to be added to a UI created on the running host, false if the running host already provides an exit menu item by default.

needsPreferencesMenuItem

public boolean needsPreferencesMenuItem()

Specified by:
needsPreferencesMenuItem in interface HostInfo
Returns:
true if an explicit preferences menu item needs to be added to a UI created on the running host, false if the running host already provides a preferences menu item by default.


Copyright © 2012 L'Xtreme IT consultancy. All Rights Reserved.