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 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 findFile(java.lang.String path)
          Try to find a file at the specified path.
static java.lang.String findFile(java.lang.String homePath, java.lang.String path)
          Try to find a file at the specified path by the following algorithm: If the path starts with "file:" or "file:/" protocol, the protocol is removed and the file is searched as is.
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.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 creating of all parent directories if necessary.
static java.lang.String removeFileProtocol(java.lang.String path)
          Test a path to start with the "file:" protocol and remove it from the path name.
static java.lang.String removeFileProtocol(java.lang.String path, boolean mustExist)
          Test a path to start with the "file:" protocol and remove it from the path name.
 
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

findFile

public static java.lang.String findFile(java.lang.String path)
Try to find a file at the specified path.
Returns:
the canocical path if search successfull or null in other cases

findFile

public static java.lang.String findFile(java.lang.String homePath,
                                        java.lang.String path)
Try to find a file at the specified path by the following algorithm:
  1. If the path starts with "file:" or "file:/" protocol, the protocol is removed and the file is searched as is.
  2. If the path starts with "/", it is searched at first attempt relative to the homePath and after unsuccessful search it is searched as is.
  3. In other cases, the file is searched at first as is and after unsuccessful search it is searched relative to the homePath.
If homePath is null, the last two steps are reduced to searching file as is.
Returns:
the canonical path if successful or null otherwise

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.

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
Throws:
NSDCException - if the method fails in any reason

removeFileProtocol

public static java.lang.String removeFileProtocol(java.lang.String path)
Test a path to start with the "file:" protocol and remove it from the path name. If the slash "/" goes right after the "file:" protocol, we try to recognize is this an extra shash or not. If the path is absolute and points to an existing file, the extra shash is not removed. Otherwise, it is removed.
Returns:
the path without file protocol or the original path if no "file:" protocol is specified

removeFileProtocol

public static java.lang.String removeFileProtocol(java.lang.String path,
                                                  boolean mustExist)
Test a path to start with the "file:" protocol and remove it from the path name. If the slash "/" goes right after the "file:" protocol, we try to recognize is this an extra shash or not. If the mustExist parameter is true, we test it to be an absolute path pointing to an existing file. If this file exist, we return it. Otherwise, the extra slash is removed.
Returns:
the path without file protocol or the original path if no "file:" protocol is 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 creating of all parent directories if necessary.

closeOut

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


Other Novosoft LLC products:
Handy Backup Handy Password manager