Class NIOHelper
- java.lang.Object
-
- de.uplanet.io.NIOHelper
-
@Scriptable public final class NIOHelper extends Object
NIOHelper class utility.- Since:
- Intrexx 8.0.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
contains(Path p_dir, DirectoryStream.Filter<Path> p_filter)
Check if the given directory contains files or directories that are accepted by the given filter.static void
copyDir(Path p_dirDest, Path p_dirSrc)
Copy a directory recursively.static void
copyDir(Path p_dirDest, Path p_dirSrc, boolean p_bCopyMarkedDirs)
Copy a directory recursively.static void
copyDir(Path p_dirDest, Path p_dirSrc, DirectoryStream.Filter<Path> p_filter)
Copy a directory recursively.static void
copyDir(Path p_dirDest, Path p_dirSrc, DirectoryStream.Filter<Path> p_filter, boolean p_bCopyMarkedDirs)
Copy a directory recursively.static Path
createBackupFile(Path p_file, Path p_dir)
Create a backup of the given file.static void
delete(Path p_path)
Deletes a directory tree recursively.static String
getFileName(Path p_path)
Get the name of the farthest element from the root of the given path as a string.static long
getFileSize(Path p_file)
Returns the size of the file or directory tree.static String
getFileText(Path p_file)
Get the content of a small UTF-8 encoded text file.static String
getFileText(Path p_file, Charset p_encoding)
Get the content of a small text file.static byte[]
getMessageDigest(Path p_file, MessageDigest p_md)
Create a message digest from the given file.static String
getMessageDigestHex(Path p_file, MessageDigest p_md)
Create a lower-case hexadecimal encoded message digest from the given file.static String
getNormalizedPath(Path p_file)
Get the normalized path of an existing file or directory.static Path
getPathBelow(String p_strPath)
Get a file object that represents a file or directory in a sub-path below the Java VM's working directory.static Path
getPathBelow(Path p_dir, String... p_subs)
Get a file object that represents a file or directory in a sub-path below a given directory.static String
getPathRelative(Path p_dir, Path p_file)
Get the path of an existing file or directory below the given existing directory relative to this directory.static Path
getUniqueNumberedFile(Path p_file)
static boolean
isDirectoryEmpty(Path p_dir)
Check if the given directory is empty.static boolean
isPathBelow(Path p_dir, Path p_path)
Check if the given file's or directory's location is below the given existing directory.static ArrayList<Path>
listFiles(Path p_dir, DirectoryStream.Filter<Path> p_filter)
Get a list of files or directories that are accepted by the given filter.static InputStream
newBufferedInputStream(Path p_file, OpenOption... p_options)
Get an input stream that provides better read performance than the input stream returned byFiles.newInputStream(Path, OpenOption...)
in cases where many small read operations occur.static ObjectInputStream
newBufferedObjectInputStream(Path p_file, OpenOption... p_options)
Get a buffered object input stream.static ObjectOutputStream
newBufferedObjectOutputStream(Path p_file, OpenOption... p_options)
Get a buffered object output stream.static OutputStream
newBufferedOutputStream(Path p_file, OpenOption... p_options)
Get an output stream that provides better write performance than the output stream returned byFiles.newOutputStream(Path, OpenOption...)
in cases where many small write operations occur.static Path
stripFileNumber(Path p_file)
-
-
-
Method Detail
-
getFileName
public static String getFileName(Path p_path)
Get the name of the farthest element from the root of the given path as a string. The call is equivalent top_path.getFileName().toString()
This method is intended to be referenced in places where otherwise clumsy lambda expressions were needed.- Parameters:
p_path
- The path.- Returns:
- The file name as a string.
- Since:
- Intrexx 9.0.
-
getNormalizedPath
public static String getNormalizedPath(Path p_file)
Get the normalized path of an existing file or directory.The returned path's file separators are replaced with forward slashes ('/') if the operating system's file separator is not the forward slash.
Note: This method is not equivalent to
IOHelper.getNormalizedPath(File)
since it does not necessarily return an absolute path and does not append a trailing slash to paths that refer to a directory.- Parameters:
p_file
- The file.- Returns:
- The normalized path.
- Since:
- Intrexx 9.0.
-
getPathRelative
public static String getPathRelative(Path p_dir, Path p_file) throws IOException
Get the path of an existing file or directory below the given existing directory relative to this directory.The returned path's file separators are normalized to forward slash ('/') if the operating system's file separator is not the forward slash.
If
p_file
denotes a directory the returned path, if notnull
, has a trailing forward slash.- Parameters:
p_dir
- The base directory.p_file
- The file for which the path relative top_dir
should be determined.- Returns:
- The path of
p_file
relative top_dir
, ornull
ifp_dir
is not a parent ofp_file
. - Throws:
IOException
- - If an I/O error occurred.- Since:
- Intrexx 9.0.
-
isDirectoryEmpty
public static boolean isDirectoryEmpty(Path p_dir) throws NotDirectoryException, IOException
Check if the given directory is empty.- Parameters:
p_dir
- The directory.- Returns:
true
if the directory is empty, orfalse
otherwise.- Throws:
NotDirectoryException
- If the given path does not represent an existing directory.IOException
- If an I/O error occurred.IllegalArgumentException
- If the given path isnull
.- Since:
- Intrexx 9.0.
-
contains
public static boolean contains(Path p_dir, DirectoryStream.Filter<Path> p_filter) throws NotDirectoryException, IOException
Check if the given directory contains files or directories that are accepted by the given filter.- Parameters:
p_dir
- The directory.p_filter
- The filter,- Returns:
true
if the directory contains files or directories that are accepted by the given filter, orfalse
otherwise.- Throws:
NotDirectoryException
- If the given path does not represent an existing directory.IOException
- If an I/O error occurred.IllegalArgumentException
- If the given path isnull
.- Since:
- Intrexx 9.0.
-
getFileSize
public static long getFileSize(Path p_file)
Returns the size of the file or directory tree.- Parameters:
p_file
- file or directory- Returns:
- size of all files of the specified tree or of a single file
- Since:
- Intrexx 8.0.
-
getMessageDigestHex
public static String getMessageDigestHex(Path p_file, MessageDigest p_md) throws IOException
Create a lower-case hexadecimal encoded message digest from the given file.- Parameters:
p_file
- The file.p_md
- The message digest to be used.- Returns:
- The message digest.
- Throws:
FileNotFoundException
- If the file does not exist.IOException
- If an I/O error occurred.- Since:
- Intrexx 9.0.
-
getMessageDigest
public static byte[] getMessageDigest(Path p_file, MessageDigest p_md) throws IOException
Create a message digest from the given file.- Parameters:
p_file
- The file.p_md
- The message digest to be used.- Returns:
- The message digest.
- Throws:
FileNotFoundException
- If the file does not exist.IOException
- If an I/O error occurred.- Since:
- Intrexx 9.0.
-
getFileText
public static String getFileText(Path p_file) throws IOException
Get the content of a small UTF-8 encoded text file.Note that this method is intended for simple cases where it is convenient to read all bytes into a byte array. It is not intended for reading in large files.
- Parameters:
p_file
- The text file.- Returns:
- The content of the given text file.
- Throws:
FileNotFoundException
- If the file does not exist.IOException
- If an I/O error occurred.OutOfMemoryError
- If the file is too large to be read into memory.- Since:
- Intrexx 9.0.
-
getFileText
public static String getFileText(Path p_file, Charset p_encoding) throws IOException
Get the content of a small text file.Note that this method is intended for simple cases where it is convenient to read all bytes into a byte array. It is not intended for reading in large files.
- Parameters:
p_file
- The text file.p_encoding
- The file encoding.- Returns:
- The content of the given text file.
- Throws:
FileNotFoundException
- If the file does not exist.IOException
- If an I/O error occurred.OutOfMemoryError
- If the file is too large to be read into memory.- Since:
- Intrexx 9.0.
-
listFiles
public static ArrayList<Path> listFiles(Path p_dir, DirectoryStream.Filter<Path> p_filter) throws NotDirectoryException, IOException
Get a list of files or directories that are accepted by the given filter.Note: This method may consume large amounts of memory. Avoid using it whenever possible and use a
DirectoryStream
instead.- Parameters:
p_dir
- The directory.p_filter
- The filter,- Returns:
- A list of files or directories that are accepted by the given
filter (never
null
). - Throws:
NotDirectoryException
- If the given path does not represent an existing directory.IOException
- If an I/O error occurred.IllegalArgumentException
- If the given path isnull
.- Since:
- Intrexx 9.0.
-
copyDir
public static void copyDir(Path p_dirDest, Path p_dirSrc) throws IOException
Copy a directory recursively.p_dest
is the destination directory. If this directory does not exist, it will be created. The content of the source directory will be copied into it.- Parameters:
p_dirDest
- The destination directory.p_dirSrc
- The directory to copy.- Throws:
IOException
- If an I/O error occurred.- Since:
- Intrexx 8.0.
-
copyDir
public static void copyDir(Path p_dirDest, Path p_dirSrc, boolean p_bCopyMarkedDirs) throws IOException
Copy a directory recursively.p_dest
is the destination directory. If this directory does not exist, it will be created. The content of the source directory will be copied into it.- Parameters:
p_dirDest
- The destination directory.p_dirSrc
- The directory to copy.p_bCopyMarkedDirs
- copy directories, event if they are marked with a ".noexport" file. If this parameter is set to false, the destination directory is created but left empty.- Throws:
IOException
- If an I/O error occurs.- Since:
- Intrexx 8.0.
-
copyDir
public static void copyDir(Path p_dirDest, Path p_dirSrc, DirectoryStream.Filter<Path> p_filter) throws IOException
Copy a directory recursively. Copy only the files matching the specified file filter.
p_dest
is the parent directory of the new directory.- Parameters:
p_dirDest
- The destination directory.p_dirSrc
- The directory to copy.p_filter
- File filter that filters the files that will be copied. Ifnull
is specified, no filter applies.- Throws:
IOException
- If an I/O error occurred.- Since:
- Intrexx 8.0.
-
copyDir
public static void copyDir(Path p_dirDest, Path p_dirSrc, DirectoryStream.Filter<Path> p_filter, boolean p_bCopyMarkedDirs) throws IOException
Copy a directory recursively. Copy only the files matching the specified file filter.
p_dest
is the parent directory of the new directory.- Parameters:
p_dirDest
- The destination directory.p_dirSrc
- The directory to copy.p_filter
- File filter that filters the files that will be copied. Ifnull
is specified, no filter applies.p_bCopyMarkedDirs
- copy directories, event if they are marked with a ".noexport" file. If this parameter is set to false, the destination directory is created with a .noxexport file as the only content.- Throws:
IOException
- If an I/O error occurred.- Since:
- Intrexx 8.0.
-
delete
public static void delete(Path p_path) throws IOException
Deletes a directory tree recursively. If the specified path points to a file, this method deletes the files. If it is a directory, all items of the tree are deleted as well as the specified directory.- Parameters:
p_path
- The directory path to delete.- Throws:
IOException
- If an I/O error occurred.- Since:
- Intrexx 8.0.
-
isPathBelow
public static boolean isPathBelow(Path p_dir, Path p_path) throws NoSuchFileException, IOException
Check if the given file's or directory's location is below the given existing directory. The file or directory given as the second parameter is not required to exist.If the given path is relative it is assumed to be relative to the current working directory.
- Parameters:
p_dir
- The containing directory (must exist physically).p_path
- The path to test (is not required to exist physically).- Returns:
true
if the given file's location is below the given, directory, orfalse
otherwise.- Throws:
NoSuchFileException
- If the containing directory does not exist.IOException
- If an I/O error occurred.IllegalArgumentException
- If the given directory or path isnull
.- Since:
- Intrexx 9.0.
-
getPathBelow
public static Path getPathBelow(String p_strPath)
Get a file object that represents a file or directory in a sub-path below the Java VM's working directory.- Parameters:
p_strPath
- The sub-path within the current working directory.- Returns:
- A path object that represents a file or directory in a sub-path of the current working directory.
- Throws:
IllegalArgumentException
- If the given path isnull
or empty.SecurityException
- If the given path references a file location outside the given directory.- Since:
- Intrexx 9.0.
-
getPathBelow
public static Path getPathBelow(Path p_dir, String... p_subs)
Get a file object that represents a file or directory in a sub-path below a given directory.- Parameters:
p_dir
- The base directory where the given sub-path should be rooted.p_subs
- The sub-path within the given directory.- Returns:
- A file object that represents a file or directory in a sub-path of the given directory.
- Throws:
IllegalArgumentException
- Ifp_dir
isnull
, or if the directoryp_dir
does not exist, or ifp_subs
isnull
or empty or containsnull
or empty path elements.SecurityException
- Ifp_path
references a file location outside the given directory.- Since:
- Intrexx 9.0.
-
getUniqueNumberedFile
public static Path getUniqueNumberedFile(Path p_file)
- Returns:
- A file path base on the given file that does not exist.
- Since:
- Intrexx 9.0.
-
stripFileNumber
public static Path stripFileNumber(Path p_file)
- Returns:
- The file with removed trailing file number.
- Since:
- Intrexx 9.0.
-
newBufferedInputStream
public static InputStream newBufferedInputStream(Path p_file, OpenOption... p_options) throws IOException
Get an input stream that provides better read performance than the input stream returned byFiles.newInputStream(Path, OpenOption...)
in cases where many small read operations occur.Note: The returned input stream is not guaranteed to be thread-safe.
Note: If you need an input stream that supports the
mark
andreset
operations you must not use this method.- Parameters:
p_file
- The path to the file to open.p_options
- Options specifying how the file is opened.- Returns:
- A new input stream.
- Throws:
IllegalArgumentException
- SeeFiles.newInputStream(Path, OpenOption...)
.UnsupportedOperationException
- SeeFiles.newInputStream(Path, OpenOption...)
.SecurityException
- SeeFiles.newInputStream(Path, OpenOption...)
.IOException
- SeeFiles.newInputStream(Path, OpenOption...)
.- Since:
- Intrexx 9.0.
-
newBufferedOutputStream
public static OutputStream newBufferedOutputStream(Path p_file, OpenOption... p_options) throws IOException
Get an output stream that provides better write performance than the output stream returned byFiles.newOutputStream(Path, OpenOption...)
in cases where many small write operations occur.Note: The returned output stream is not guaranteed to be thread-safe.
- Parameters:
p_file
- The path to the file to open.p_options
- Options specifying how the file is opened.- Returns:
- A new output stream.
- Throws:
IllegalArgumentException
- SeeFiles.newOutputStream(Path, OpenOption...)
.UnsupportedOperationException
- SeeFiles.newOutputStream(Path, OpenOption...)
.SecurityException
- SeeFiles.newOutputStream(Path, OpenOption...)
.IOException
- SeeFiles.newOutputStream(Path, OpenOption...)
.- Since:
- Intrexx 9.0.
-
newBufferedObjectInputStream
public static ObjectInputStream newBufferedObjectInputStream(Path p_file, OpenOption... p_options) throws IOException
Get a buffered object input stream.Note: The returned input stream is not guaranteed to be thread-safe.
- Parameters:
p_file
- The path to the file to open.p_options
- Options specifying how the file is opened.- Returns:
- A new object input stream.
- Throws:
IllegalArgumentException
- SeeFiles.newInputStream(Path, OpenOption...)
.UnsupportedOperationException
- SeeFiles.newInputStream(Path, OpenOption...)
.SecurityException
- SeeFiles.newInputStream(Path, OpenOption...)
.IOException
- SeeFiles.newInputStream(Path, OpenOption...)
.- Since:
- Intrexx 9.0.
-
newBufferedObjectOutputStream
public static ObjectOutputStream newBufferedObjectOutputStream(Path p_file, OpenOption... p_options) throws IOException
Get a buffered object output stream.Note: The returned output stream is not guaranteed to be thread-safe.
- Parameters:
p_file
- The path to the file to open.p_options
- Options specifying how the file is opened.- Returns:
- A new object output stream.
- Throws:
IllegalArgumentException
- SeeFiles.newOutputStream(Path, OpenOption...)
.UnsupportedOperationException
- SeeFiles.newOutputStream(Path, OpenOption...)
.SecurityException
- SeeFiles.newOutputStream(Path, OpenOption...)
.IOException
- SeeFiles.newOutputStream(Path, OpenOption...)
.- Since:
- Intrexx 9.0.
-
createBackupFile
public static Path createBackupFile(Path p_file, Path p_dir) throws IOException
Create a backup of the given file.The backuo file has the same name as the original file with the suffix
.yyyy-MM-dd-HH-mm-ss.bak
containing a timestamp in the default time zone appended.- Parameters:
p_file
- The file to backup.- Returns:
- The created backup file.
- Throws:
IOException
- If an I/O error occurred.- Since:
- Intrexx 9.0.
-
-