public final class Utils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static void |
close(java.io.Closeable c)
Close a
Closable and ignore any exceptions thrown. |
static long |
copy(Files files,
java.io.InputStream in,
Path target,
boolean truncate)
Copies all bytes from an input stream to a file.
|
static long |
copy(Files files,
Path source,
java.io.OutputStream out)
Copies all bytes from a file to an output stream.
|
static long |
copy(java.io.InputStream in,
java.io.OutputStream out,
int bufferSize)
Copy all bytes from an input stream to an output stream.
|
static <K,V> java.util.Map<K,V> |
emptyMap(int capacity)
Creates empty mutable Map with sufficient initial capacity.
|
static Path |
fromLocalPath(Files files,
java.lang.String path)
Takes the String representation of a local path (for example "/bin/foo" or "C:\dir\test.txt") and converts it into a
Path . |
static java.lang.String |
getCWD()
Return the current working directory as a String.
|
static java.lang.String |
getHome()
Return the home directory of the current user as a String.
|
static Path |
getLocalCWD(Files files)
Returns a
Path that represents the current working directory. |
static FileSystem[] |
getLocalFileSystems(Files files)
Returns all local FileSystems.
|
static Path |
getLocalHome(Files files)
Returns a
Path that represents the home directory of the current user. |
static java.lang.String |
getLocalRoot(java.lang.String p)
Return the locally valid root element of an
String representation of an absolute path. |
static Scheduler |
getLocalScheduler(Jobs jobs)
Returns a
Scheduler that can be used to run jobs locally. |
static char |
getLocalSeparator()
Returns the local file system path separator character.
|
static RelativePath |
getRelativePath(java.lang.String path,
java.lang.String root)
Provided with an absolute
path and a root , this method returns a RelativePath that
represents the part of path that is realtive to the root . |
static boolean |
isLinux()
Returns if we are currently running on Linux.
|
static boolean |
isLinuxRoot(java.lang.String root)
Check if
root only contains a valid Linux root element, which is "/". |
static boolean |
isLocalRoot(java.lang.String root)
Check if
root contains a locally valid root element, such as "C:" on Windows or "/" on Linux and OSX. |
static boolean |
isOSX()
Returns if we are currently running on OSX.
|
static boolean |
isOSXRoot(java.lang.String root)
Check if
root contains a valid OSX root element, which is "/". |
static boolean |
isWindows()
Returns if we are currently running on Windows.
|
static boolean |
isWindowsRoot(java.lang.String root)
Check if
root only contains a valid Windows root element such as "C:". |
static java.io.BufferedReader |
newBufferedReader(Files files,
Path source,
java.nio.charset.Charset cs)
Opens a file for reading, returning a
BufferedReader that may be used to read text from the file in an
efficient manner. |
static java.io.BufferedWriter |
newBufferedWriter(Files files,
Path target,
java.nio.charset.Charset cs,
boolean truncate)
Opens or creates a file for writing, returning a BufferedWriter that may be used to write text to the file in an efficient
manner.
|
static Scheduler |
newScheduler(Jobs jobs,
java.lang.String scheme)
Create a
Scheduler for the given scheme, but without using a location, credential, or properties. |
static OpenOption[] |
openOptionsForWrite(boolean truncate)
Return an
OpenOption array containing all options needed to open a file for writing. |
static byte[] |
readAllBytes(Files files,
Path source)
Read all the bytes from a file and return them as a
byte[] . |
static byte[] |
readAllBytes(java.io.InputStream in)
Read all bytes from the input stream and return them in a byte array.
|
static java.util.List<java.lang.String> |
readAllLines(Files files,
Path source,
java.nio.charset.Charset cs)
Read all lines from a file and return them in a
List . |
static java.util.List<java.lang.String> |
readLines(java.io.InputStream in,
java.nio.charset.Charset cs)
Read all lines from a InputStream and return them in a
List . |
static java.lang.String |
readToString(Files files,
Path source,
java.nio.charset.Charset cs)
Read all the bytes from a file and return them as a
String using the Charset for conversion. |
static java.lang.String |
readToString(java.io.InputStream in)
Read all bytes from the input stream and return them in as a single String.
|
static java.lang.String |
readToString(java.io.InputStream in,
java.nio.charset.Charset cs)
Read all bytes from the input stream and return them in as a single String.
|
static void |
recursiveCopy(Files files,
Path source,
Path target,
CopyOption... options)
Recursively copies directories, files and symbolic links from source to target.
|
static void |
recursiveDelete(Files files,
Path path)
Recursively removes all directories, files and symbolic links in path.
|
static Path |
resolveWithEntryPath(Files files,
FileSystem fileSystem,
java.lang.String... path)
Resolve a relative
path with the entry path of the fileSystem and return a new Path that
represents this location. |
static Path |
resolveWithRoot(Files files,
Path root,
java.lang.String... path)
|
static boolean |
startsWithLinuxRoot(java.lang.String path)
Checks if the provide path starts with a valid Linux root, that is "/".
|
static boolean |
startWithRoot(java.lang.String path)
Checks if the provide path starts with a valid root, such as "/" or "C:".
|
static boolean |
startWithWindowsRoot(java.lang.String path)
Checks if the provide path starts with a valid Windows root, for example "C:".
|
static void |
walkFileTree(Files files,
Path start,
boolean followLinks,
int maxDepth,
FileVisitor visitor)
Walks a file tree.
|
static void |
walkFileTree(Files files,
Path start,
FileVisitor visitor)
Walks over a file tree.
|
static void |
write(Files files,
Path target,
byte[] bytes,
boolean truncate)
Writes bytes to a file.
|
static void |
write(Files files,
Path target,
java.lang.Iterable<? extends java.lang.CharSequence> lines,
java.nio.charset.Charset cs,
boolean truncate)
Write lines of text to a file.
|
static void |
writeLines(java.lang.Iterable<? extends java.lang.CharSequence> lines,
java.nio.charset.Charset cs,
java.io.OutputStream out)
Write lines of text to a file.
|
public static Scheduler getLocalScheduler(Jobs jobs) throws XenonException
Scheduler
that can be used to run jobs locally.jobs
- the Jobs
to use to instantiate the scheduler.Scheduler
.XenonException
- If the creation of the Scheduler failed.public static Scheduler newScheduler(Jobs jobs, java.lang.String scheme) throws XenonException
Scheduler
for the given scheme, but without using a location, credential, or properties.
This methods is a shortcut for calling Jobs.newScheduler(String, String, Credential, Map)
with only the
scheme
set.jobs
- the Jobs
to use to instantiate the scheduler.scheme
- the scheme to use to access the scheduler.Scheduler
.XenonException
- If the creation of the Scheduler failed.public static long copy(java.io.InputStream in, java.io.OutputStream out, int bufferSize) throws java.io.IOException
bufferSize
is used. If bufferSize <= 0
then the
DEFAULT_BUFFER_SIZE
will be used.
NOTE: in
and out
will NOT be explicitly closed once the end of the stream is reached.
in
- the InputStream to read from.out
- the OutputStream to write to.bufferSize
- the size of the temporary buffer, or <= 0 to use the DEFAULT_BUFFER_SIZE
.java.io.IOException
- if an I/O error occurs during the copy operation.public static byte[] readAllBytes(java.io.InputStream in) throws java.io.IOException
NOTE: in
will NOT be explicitly closed once the end of the stream is reached.
in
- the input stream to read.java.io.IOException
- if an I/O error was produced while reading the stream.public static java.lang.String readToString(java.io.InputStream in, java.nio.charset.Charset cs) throws java.io.IOException
cs
.
NOTE: in
will NOT be explicitly closed once the end of the stream is reached.
in
- the input stream to read.cs
- the Charset to usejava.io.IOException
- if an I/O error was produced while reading the stream.public static java.lang.String readToString(java.io.InputStream in) throws java.io.IOException
NOTE: in
will NOT be explicitly closed once the end of the stream is reached.
in
- the input stream to read.java.io.IOException
- if an I/O error was produced while reading the stream.public static java.util.List<java.lang.String> readLines(java.io.InputStream in, java.nio.charset.Charset cs) throws java.io.IOException
List
.
NOTE: in
will NOT be explicitly closed once the end of the stream is reached.
in
- the InputStream to read fromcs
- the Charset to use.List<String>
containing all lines in the file.java.io.IOException
- if an I/O error was produced while reading the stream.public static void writeLines(java.lang.Iterable<? extends java.lang.CharSequence> lines, java.nio.charset.Charset cs, java.io.OutputStream out) throws java.io.IOException
lines
- the lines of text to write.cs
- the Charset to use.out
- the output stream to write tojava.io.IOException
- if an I/O error was produced while writing the stream.public static void close(java.io.Closeable c)
Closable
and ignore any exceptions thrown.c
- the Closable
to close.public static OpenOption[] openOptionsForWrite(boolean truncate)
OpenOption
array containing all options needed to open a file for writing.
The truncate
parameter determines if an existing file will be truncated or appended.truncate
- Should an existing file be truncated ?OpenOption
array containing all options needed to open a file for writing.public static java.lang.String getHome() throws XenonException
XenonException
- If the home directory could not be retrieved.public static java.lang.String getCWD() throws XenonException
XenonException
- If the current working directory could not be retrieved.public static java.lang.String getLocalRoot(java.lang.String p) throws XenonException
String
representation of an absolute path.
Examples of a root elements are "/" or "C:". If the provided path does not contain a locally valid root element, an
exception will be thrown. For example, providing "/user/local" will return "/" on Linux or OSX, but throw an exception on
Windows; providing "C:\test" will return "C:" on Windows but throw an exception on Linux or OSX.p
- The absolute path for which to determine the root element.XenonException
- If the provided path
is not absolute, or does not contain a locally valid root.public static boolean isWindows()
public static boolean isOSX()
public static boolean isLinux()
public static boolean isWindowsRoot(java.lang.String root)
root
only contains a valid Windows root element such as "C:".
If root
is null
or empty, false
will be returned.
If root
contains more than just a root element, false
will be returned.root
- The root to check.root
only contains a valid Windows root element.public static boolean isLinuxRoot(java.lang.String root)
root
only contains a valid Linux root element, which is "/".
If root
is null
or empty, false
will be returned.
If root
contains more than just a root element, false
will be returned.root
- The root to check.root
only contains a valid Linux root element.public static boolean isOSXRoot(java.lang.String root)
root
contains a valid OSX root element, which is "/".
If root
is null
or empty, false
will be returned.
If root
contains more than just a root element, false
will be returned.root
- The root to check.root
only contains a valid OSX root element.public static boolean isLocalRoot(java.lang.String root)
root
contains a locally valid root element, such as "C:" on Windows or "/" on Linux and OSX.
If root
is null
or empty, false
will be returned.
If root
contains more than just a root element, false
will be returned.
Note that the result of this method depends on the OS the application is running on.root
- The root to check.root
only contains a valid OSX root element.public static boolean startsWithLinuxRoot(java.lang.String path)
path
- The path to check.public static boolean startWithWindowsRoot(java.lang.String path)
path
- The path to check.public static boolean startWithRoot(java.lang.String path)
path
- The path to check.public static RelativePath getRelativePath(java.lang.String path, java.lang.String root) throws XenonException
path
and a root
, this method returns a RelativePath
that
represents the part of path
that is realtive to the root
.
For example, if "C:\dir\file" is provided as path
and "C:" as root
, a RelativePath
will be returned that represents "dir\file".path
- The absolute path.root
- The root element.RelativePath
that contains the part of path
that is relative to root
.XenonException
- If the path
does not start with root
.public static char getLocalSeparator()
public static Path fromLocalPath(Files files, java.lang.String path) throws XenonException
Path
.
path
must contain an absolute path starting with a root such as "/" or "C:".files
- the files interface used to create the Path
.path
- the local path to convert.Path
representing the same location as path
.XenonException
- If the creation of the FileSystem failed.public static Path getLocalCWD(Files files) throws XenonException
Path
that represents the current working directory.
This method retrieves the current working directory using getCWD()
, and converts this into a path using
fromLocalPath(Files, String)
.files
- the files interface used to create the Path
.Path
that represents the current working directory.XenonException
- If an I/O error occurred or if the creation of the FileSystem failed.public static Path getLocalHome(Files files) throws XenonException
Path
that represents the home directory of the current user.
This method retrieves the home directory using getHome()
, and converts this into a path using
fromLocalPath(Files, String)
.files
- the files interface used to create the Path
.Path
that represents the home directory of the current user.XenonException
- If the creation of the FileSystem failed.public static FileSystem[] getLocalFileSystems(Files files) throws XenonException
FileSystems
representing each of
these roots.files
- the files interface to use to create the FileSystems
.XenonException
- If the creation of the FileSystem failed.public static long copy(Files files, java.io.InputStream in, Path target, boolean truncate) throws XenonException
files
- the files interface to use for file access.in
- the InputStream
to read from.target
- the file to write to.truncate
- should the file be truncated before writing data into it ?PathAlreadyExistsException
- if the target file exists but cannot be replaced because the REPLACE_EXISTING
option is not specified
(optional specific exception)DirectoryNotEmptyException
- the REPLACE_EXISTING
option is specified but the file cannot be replaced because it is a non-empty
directory (optional specific exception) *InvalidCopyOptionsException
- if options
contains a copy option that is not supportedXenonException
- if an I/O error occurs when reading or writingpublic static long copy(Files files, Path source, java.io.OutputStream out) throws XenonException
files
- the files interface to use for file access.source
- the file to read from.out
- the OutputStream
to write to.XenonException
- if an I/O error occurs while reading or writingpublic static java.io.BufferedReader newBufferedReader(Files files, Path source, java.nio.charset.Charset cs) throws XenonException
BufferedReader
that may be used to read text from the file in an
efficient manner.files
- the files interface to use for file access.source
- the file to read from.cs
- the Charset to use.XenonException
- if an I/O error occurs while opening or reading the file.public static java.io.BufferedWriter newBufferedWriter(Files files, Path target, java.nio.charset.Charset cs, boolean truncate) throws XenonException
files
- the files interface to use for file access.target
- the file to write to.cs
- the Charset to use.truncate
- should the file be truncated before writing data into it ?XenonException
- if an I/O error occurs while opening or writing the file.public static byte[] readAllBytes(Files files, Path source) throws XenonException
byte[]
.files
- the files interface to use for file access.source
- the file to read from.byte[]
containing all bytes in the file.XenonException
- if an I/O error occurs while opening or reading the file.public static java.lang.String readToString(Files files, Path source, java.nio.charset.Charset cs) throws XenonException
String
using the Charset
for conversion.files
- the files interface to use for file access.source
- the file to read from.cs
- the Charset to use.String
containing all data from the file as converted using cs
.XenonException
- if an I/O error occurs while opening or reading the file.public static java.util.List<java.lang.String> readAllLines(Files files, Path source, java.nio.charset.Charset cs) throws XenonException
List
.files
- the files interface to use for file access.source
- the file to read from.cs
- the Charset to use.List<String>
containing all lines in the file.XenonException
- if an I/O error occurs while opening or reading the file.public static void write(Files files, Path target, byte[] bytes, boolean truncate) throws XenonException
files
- the files interface to use for file access.target
- the file to write to.bytes
- the bytes to write to the file.truncate
- should the file be truncated before writing data into it ?XenonException
- if an I/O error occurs while opening or writing to the file.public static void write(Files files, Path target, java.lang.Iterable<? extends java.lang.CharSequence> lines, java.nio.charset.Charset cs, boolean truncate) throws XenonException
files
- the files interface to use for file access.target
- the file to write to.lines
- the text to write to the file.cs
- the Charset to use.truncate
- should the file be truncated before writing data into it ?XenonException
- if an I/O error occurs while opening or writing to the file.public static void walkFileTree(Files files, Path start, FileVisitor visitor) throws XenonException
This method is equivalent to invoking walkFileTree(files,
start, false, Integer.MAX_VALUE, visitor
.
files
- the files interface to use for file access.start
- the path to start from.visitor
- a FileVisitor
that will be invoked for every Path
encountered during the walk.XenonException
- if an I/O error occurs during the walk.public static void walkFileTree(Files files, Path start, boolean followLinks, int maxDepth, FileVisitor visitor) throws XenonException
This method walks over a file tree, starting at start
and then recursively applying the following steps:
visitor.visitFile
and the result of this call will be returned.followLinks
is true
the link is followed,
walkFileTree
is called on the target of the link, and the result of this call is returned.followLinks
is false
the link is not followed. Instead the link
itself is forwarded to visitor.visitFile
and the result of this
call is returned.start
is more than maxDepth
, the
directory is forwarded to visitor.visitFile
and the result of
this call is returned.start
is less or equal to
maxDepth
the path is forwarded to visitor.preVisitDirectory
. The subsequent behavior then depends on the result of this call:
FileVisitResult.TERMINATE
is returned, the walk is terminated immediately and walkFileTree
returns
FileVisitResult.TERMINATE
.FileVisitResult.SKIP_SUBTREE
is returned, the elements in the directory will not be visited. Instead
visitor.postVisitDirectory
and
FileVisitResult.CONTINUE
is returned.FileVisitResult.SKIP_SIBLINGS
is returned, the elements in the directory will not be visited and
FileVisitResult.SKIP_SIBLINGS
is returned immediately.FileVisitResult.CONTINUE
is returned walkFileTree
is called on each of the elements in the
directory. If any of these calls returns FileVisitResult.SKIP_SIBLINGS
the remaining elements will be skipped,
visitor.postVisitDirectory
will be called, and its
result will be returned. If any of these calls returns FileVisitResult.TERMINATE
the walk is terminated immediately
and FileVisitResult.TERMINATE
is returned.files
- the files interface to use for file access.start
- the path to start from.followLinks
- should links be followed ?maxDepth
- the maximum distance from the start to walk to.visitor
- a FileVisitor
that will be invoked for every Path
encountered during the walk.XenonException
- if an I/O error occurs during the walk.public static void recursiveCopy(Files files, Path source, Path target, CopyOption... options) throws XenonException
files
- the files interface to use for file access.source
- the path to copy from.target
- the path to copy to.options
- the options to use while copying. See CopyOption
for details.InvalidCopyOptionsException
- if an invalid combination of options is used.XenonException
- if an I/O error occurs during the copyingpublic static void recursiveDelete(Files files, Path path) throws XenonException
files
- the files interface to use for file access.path
- the path to delete.XenonException
- if an I/O error occurs during the copyingpublic static Path resolveWithRoot(Files files, Path root, java.lang.String... path) throws XenonException
files
- the files interface to use for file access.root
- the root to resolve against.path
- the relative path to resolve.Path
that represents the location.XenonException
- if an I/O error occurs during the resolve.public static Path resolveWithEntryPath(Files files, FileSystem fileSystem, java.lang.String... path) throws XenonException
path
with the entry path of the fileSystem
and return a new Path
that
represents this location.files
- the files interface to use for file access.fileSystem
- the FileSystem for which to use the entry path.path
- the relative path to resolve.Path
that represents the location.XenonException
- if an I/O error occurs during the resolve.public static <K,V> java.util.Map<K,V> emptyMap(int capacity)
K
- key typeV
- value typecapacity
- maximum size without resizing underlying data structure