ru.novosoft.dc.core
Class IOService

java.lang.Object
  |
  +--ru.novosoft.dc.core.IOService

public final class IOService
extends java.lang.Object

The class contains miscellaneous static I/O service methods.


Method Summary
static java.lang.String addFileProtocol(java.lang.String path)
          Convert a path to the canonical form, add the "file:" protocol at the beginning of path, and return the result.
static java.lang.String appendExtension(java.lang.String path, java.lang.String extension)
          Append the specified extension to the path, if the path just not has this extension yet, and return the result.
static void closeOut(java.io.OutputStream out)
          Close the output stream and catch the IOException.
static boolean createDirectory(java.lang.String path)
          Try to create a directory with the specified path and return true if the directory already exists or the creation was successful.
static java.lang.String createPath(java.lang.String path, java.lang.String extension)
          Create a new path by replacing the extension of the specified path to the specified extension.
static java.lang.String findFileInResources(java.lang.String path)
          Try to find a file in resources (in path list specified in the Java classpath variable) and return the canonical path to it or null if search fails.
static java.lang.String findHomePath(java.lang.String propertyName)
          Find a home path using a value of system property with the specified propertyName name and return it.
static java.lang.String[] getFileList(java.lang.Class cls, java.lang.String homePath, java.lang.String relPath)
          Return an array of strings naming the files and directories found in resources.
static java.lang.String getFilePath(java.lang.String homePath, java.lang.String relPath, java.lang.String path)
          Prepare a path to file.
static java.io.InputStream getResourceAsStream(java.lang.Class cls, java.lang.String homePath, java.lang.String relPath, java.lang.String path)
          Try to open an input stream at the resource.
static boolean hasFileProtocol(java.lang.String path)
          Test a path to start with file protocol.
static java.util.Properties loadPropertiesFrom(java.io.File file)
          Try to load properties from a specified file.
static java.util.Properties loadPropertiesFrom(java.io.InputStream stream)
          Try to load properties from a specified stream.
static java.util.Properties loadPropertiesFrom(java.lang.String path)
          Try to load properties from file at the specified path.
static java.io.OutputStream openForWriting(java.lang.String path)
          Try to open a file in the specified path for writing with creation of all parent directories if necessary.
static java.lang.String removeFileProtocol(java.lang.String path)
          Test a path to start with the "file:" protocol, remove it from the path name, and convert the path to canonical form.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createPath

public static java.lang.String createPath(java.lang.String path,
                                          java.lang.String extension)
Create a new path by replacing the extension of the specified path to the specified extension. If the specified path already has this extension (we compare it ignoring the case of letters), the specified extension is appended to it.
Parameters:
path - a path to replace an extension
extension - a new extension starting from the dot (e.g., ".log")
Returns:
a path with the specified extension

appendExtension

public static java.lang.String appendExtension(java.lang.String path,
                                               java.lang.String extension)
Append the specified extension to the path, if the path just not has this extension yet, and return the result. If the path already has the specified extension, it will be returned itself. The extensions are compare in ignore case mode.
Parameters:
path - a path to append an extension to
extension - an extension to be appended (e.g., ".opt")
Returns:
a path with the specified extension

findFileInResources

public static java.lang.String findFileInResources(java.lang.String path)
Try to find a file in resources (in path list specified in the Java classpath variable) and return the canonical path to it or null if search fails.

getFileList

public static java.lang.String[] getFileList(java.lang.Class cls,
                                             java.lang.String homePath,
                                             java.lang.String relPath)
Return an array of strings naming the files and directories found in resources. The following algorithm is applied:
  1. If relPath starts with "file:" protocol, it should be a directory. The list of all files of the directory is returned;
  2. Test a resource jar containing the given cls object and select all files stored in the relPath directory relative to the package of cls;
  3. Test the relPath directory relative to the homePath and add all its files to the list prepared at the previous step;
  4. Return the prepared list as String array.
If cls==null, the second step is skipped. If homePath equals null or empty string, the last step is skipped. Return null if no directory for search found.

getResourceAsStream

public static java.io.InputStream getResourceAsStream(java.lang.Class cls,
                                                      java.lang.String homePath,
                                                      java.lang.String relPath,
                                                      java.lang.String path)
Try to open an input stream at the resource. The following algorithm is applied:
  1. If path parameter starts with the "file:" protocol, the protocol is removed and a file at the given absolute path is openned;
  2. If path parameter specifies an absolute path of existing file, the file is openned;
  3. Concatenate the relPath with path and delimit them with slash symbol if necessary;
  4. Test the resulting path to start with the "file:" protocol. If the protocol found, it is removed and a file at the given path is openned;
  5. Otherwise, try to open a file at the resulting path relative to the homePath;
  6. If the previous step failed, try to open resource at the resulting path relative to the given class.
If path equals null or empty string, the first three steps are omitted. If homePath equals null or empty string, the fifth step is skipped.
Parameters:
cls - a base class to get resource at
homePath - an absolute path to a home directory
relPath - a relative subdirectory path (can start with "file:" protocol)
path - a path to the resource to be openned (can start with "file:" protocol)
Returns:
an input stream or null if no resource found

getFilePath

public static java.lang.String getFilePath(java.lang.String homePath,
                                           java.lang.String relPath,
                                           java.lang.String path)
Prepare a path to file. The following algorithm is applied:
  1. If path parameter starts with "file:" protocol, the protocol is removed and the given absolute path is returned;
  2. If path parameter specifies an absolute path, it is returned;
  3. Concatenate the relPath with path and delimit them with slash symbol if necessary;
  4. Test the resulting path to start with the "file:" protocol. If the protocol found, it is removed and the remaining path is returned;
  5. Otherwise, concatenate the homePath with resulting path, delimit them with slash symbol and return.
If path equals null or empty string, the first three steps are omitted. If homePath equals null or empty string, the fifth step returns null.
Parameters:
homePath - an absolute path to a home directory
relPath - a relative subdirectory path (can start with "file:" protocol)
path - a path to the resource to be openned (can start with "file:" protocol)
Returns:
the resulting path

findHomePath

public static java.lang.String findHomePath(java.lang.String propertyName)
Find a home path using a value of system property with the specified propertyName name and return it. If the property is undefined, the first element of the CLASSPATH list is used as the home path.
Returns:
a home path in the canonical form or null if the path cannot be specified
Throws:
NSDCException - if the given path is not a directory path

hasFileProtocol

public static final boolean hasFileProtocol(java.lang.String path)
Test a path to start with file protocol.

addFileProtocol

public static java.lang.String addFileProtocol(java.lang.String path)
Convert a path to the canonical form, add the "file:" protocol at the beginning of path, and return the result. If canonical path has no slash at the beginning, the slash is inserted right after the "file:". If the input parameter already starts with "file:" protocol, it is returned without changes.

removeFileProtocol

public static java.lang.String removeFileProtocol(java.lang.String path)
Test a path to start with the "file:" protocol, remove it from the path name, and convert the path to canonical form.
Returns:
the canonical path without file protocol or the unchanged path if no "file:" protocol specified

createDirectory

public static boolean createDirectory(java.lang.String path)
Try to create a directory with the specified path and return true if the directory already exists or the creation was successful. Otherwise, the false is returned.

loadPropertiesFrom

public static final java.util.Properties loadPropertiesFrom(java.lang.String path)
Try to load properties from file at the specified path. Return the properties loaded or null if the operation failed on error.

loadPropertiesFrom

public static java.util.Properties loadPropertiesFrom(java.io.File file)
Try to load properties from a specified file. Return the properties loaded or null if the operation failed on error.

loadPropertiesFrom

public static java.util.Properties loadPropertiesFrom(java.io.InputStream stream)
Try to load properties from a specified stream. Return the properties loaded or null if the operation failed on error.

openForWriting

public static java.io.OutputStream openForWriting(java.lang.String path)
                                           throws java.io.IOException
Try to open a file in the specified path for writing with creation of all parent directories if necessary.

closeOut

public static final void closeOut(java.io.OutputStream out)
Close the output stream and catch the IOException.