public final class IoUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_COPY_SIZE
Size of preferred work buffer when reading / writing data to / from streams
|
static java.nio.file.CopyOption[] |
EMPTY_COPY_OPTIONS |
static java.nio.file.attribute.FileAttribute<?>[] |
EMPTY_FILE_ATTRIBUTES |
static java.nio.file.LinkOption[] |
EMPTY_LINK_OPTIONS |
static java.nio.file.OpenOption[] |
EMPTY_OPEN_OPTIONS |
static java.lang.String |
EOL
The local O/S line separator
|
private static byte[] |
EOL_BYTES |
private static java.nio.file.LinkOption[] |
NO_FOLLOW_OPTIONS |
static java.util.List<java.lang.String> |
WINDOWS_EXECUTABLE_EXTENSIONS |
static java.util.Set<java.nio.file.StandardOpenOption> |
WRITEABLE_OPEN_OPTIONS
A
Set of StandardOpenOption -s that indicate an intent
to create/modify a file |
Modifier | Constructor and Description |
---|---|
private |
IoUtils()
Private Constructor
|
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
appendPathComponent(java.lang.String prefix,
java.lang.String component) |
static java.lang.Boolean |
checkFileExists(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Checks if a file exists - Note: according to the
Java tutorial - Checking a File or Directory:
|
static java.io.IOException |
closeQuietly(java.io.Closeable... closeables)
Closes a bunch of resources suppressing any
IOException s their
Closeable.close() method may have thrown |
static java.io.IOException |
closeQuietly(java.util.Collection<? extends java.io.Closeable> closeables)
Closes a bunch of resources suppressing any
IOException s their
Closeable.close() method may have thrown |
static long |
copy(java.io.InputStream source,
java.io.OutputStream sink) |
static long |
copy(java.io.InputStream source,
java.io.OutputStream sink,
int bufferSize) |
static java.nio.file.Path |
ensureDirectory(java.nio.file.Path path,
java.nio.file.LinkOption... options) |
static boolean |
followLinks(java.nio.file.LinkOption... options) |
static byte[] |
getEOLBytes() |
static java.lang.String |
getFileOwner(java.nio.file.Path path,
java.nio.file.LinkOption... options)
Get file owner.
|
static java.nio.file.LinkOption[] |
getLinkOptions(boolean followLinks) |
static java.util.Set<java.nio.file.attribute.PosixFilePermission> |
getPermissions(java.nio.file.Path path,
java.nio.file.LinkOption... options)
If the "posix" view is supported, then it returns
Files.getPosixFilePermissions(Path, LinkOption...) , otherwise
uses the getPermissionsFromFile(File) method |
static java.util.Set<java.nio.file.attribute.PosixFilePermission> |
getPermissionsFromFile(java.io.File f) |
static boolean |
isExecutable(java.io.File f) |
static boolean |
isWindowsExecutable(java.lang.String fileName) |
static int |
read(java.io.InputStream input,
byte[] buffer)
Read as many bytes as possible until EOF or achieved required length
|
static int |
read(java.io.InputStream input,
byte[] buffer,
int offset,
int length)
Read as many bytes as possible until EOF or achieved required length
|
static java.util.List<java.lang.String> |
readAllLines(java.io.BufferedReader reader)
Reads all lines until no more available
|
static java.util.List<java.lang.String> |
readAllLines(java.io.BufferedReader reader,
int lineCountHint)
Reads all lines until no more available
|
static java.util.List<java.lang.String> |
readAllLines(java.io.InputStream stream)
Reads all lines until no more available
|
static java.util.List<java.lang.String> |
readAllLines(java.io.Reader reader) |
static java.util.List<java.lang.String> |
readAllLines(java.net.URL url)
Reads all lines until no more available
|
static void |
readFully(java.io.InputStream input,
byte[] buffer)
Read the requested number of bytes or fail if there are not enough left.
|
static void |
readFully(java.io.InputStream input,
byte[] buffer,
int offset,
int length)
Read the requested number of bytes or fail if there are not enough left.
|
static void |
setPermissions(java.nio.file.Path path,
java.util.Set<java.nio.file.attribute.PosixFilePermission> perms)
If the "posix" view is supported, then it invokes
Files.setPosixFilePermissions(Path, Set) , otherwise
uses the setPermissionsToFile(File, Collection) method |
static void |
setPermissionsToFile(java.io.File f,
java.util.Collection<java.nio.file.attribute.PosixFilePermission> perms) |
static byte[] |
toByteArray(java.io.InputStream inStream) |
static java.nio.file.attribute.PosixFilePermission |
validateExcludedPermissions(java.util.Collection<java.nio.file.attribute.PosixFilePermission> perms,
java.util.Collection<java.nio.file.attribute.PosixFilePermission> excluded) |
public static final java.nio.file.OpenOption[] EMPTY_OPEN_OPTIONS
public static final java.nio.file.CopyOption[] EMPTY_COPY_OPTIONS
public static final java.nio.file.LinkOption[] EMPTY_LINK_OPTIONS
public static final java.nio.file.attribute.FileAttribute<?>[] EMPTY_FILE_ATTRIBUTES
public static final java.util.List<java.lang.String> WINDOWS_EXECUTABLE_EXTENSIONS
public static final int DEFAULT_COPY_SIZE
public static final java.lang.String EOL
public static final java.util.Set<java.nio.file.StandardOpenOption> WRITEABLE_OPEN_OPTIONS
Set
of StandardOpenOption
-s that indicate an intent
to create/modify a fileprivate static final byte[] EOL_BYTES
private static final java.nio.file.LinkOption[] NO_FOLLOW_OPTIONS
public static byte[] getEOLBytes()
EOL
public static java.nio.file.LinkOption[] getLinkOptions(boolean followLinks)
public static long copy(java.io.InputStream source, java.io.OutputStream sink) throws java.io.IOException
java.io.IOException
public static long copy(java.io.InputStream source, java.io.OutputStream sink, int bufferSize) throws java.io.IOException
java.io.IOException
public static java.io.IOException closeQuietly(java.io.Closeable... closeables)
IOException
s their
Closeable.close()
method may have throwncloseables
- The Closeable
s to closeIOException
that occurred during closing
of a resource - null
if not exception. If more than one exception
occurred, they are added as suppressed exceptions to the first oneThrowable.getSuppressed()
public static java.io.IOException closeQuietly(java.util.Collection<? extends java.io.Closeable> closeables)
IOException
s their
Closeable.close()
method may have throwncloseables
- The Closeable
s to closeIOException
that occurred during closing
of a resource - null
if not exception. If more than one exception
occurred, they are added as suppressed exceptions to the first oneThrowable.getSuppressed()
public static boolean isWindowsExecutable(java.lang.String fileName)
fileName
- The file name to be evaluated - ignored if null
/emptytrue
if the file ends in one of the WINDOWS_EXECUTABLE_EXTENSIONS
public static java.util.Set<java.nio.file.attribute.PosixFilePermission> getPermissions(java.nio.file.Path path, java.nio.file.LinkOption... options) throws java.io.IOException
Files.getPosixFilePermissions(Path, LinkOption...)
, otherwise
uses the getPermissionsFromFile(File)
methodpath
- The Path
options
- The LinkOption
s to use when querying the permissionsSet
of PosixFilePermission
java.io.IOException
- If failed to access the file system in order to
retrieve the permissionspublic static java.util.Set<java.nio.file.attribute.PosixFilePermission> getPermissionsFromFile(java.io.File f)
f
- The File
to be checkedSet
of PosixFilePermission
s based on whether
the file is readable/writable/executable. If so, then all the
relevant permissions are set (i.e., owner, group and others)public static boolean isExecutable(java.io.File f)
public static void setPermissions(java.nio.file.Path path, java.util.Set<java.nio.file.attribute.PosixFilePermission> perms) throws java.io.IOException
Files.setPosixFilePermissions(Path, Set)
, otherwise
uses the setPermissionsToFile(File, Collection)
methodpath
- The Path
perms
- The Set
of PosixFilePermission
sjava.io.IOException
- If failed to access the file systempublic static void setPermissionsToFile(java.io.File f, java.util.Collection<java.nio.file.attribute.PosixFilePermission> perms)
f
- The File
perms
- A Collection
of PosixFilePermission
s to set on it.
Note: the file is set to readable/writable/executable not only by the
owner if any of relevant the owner/group/others permission is setpublic static java.lang.String getFileOwner(java.nio.file.Path path, java.nio.file.LinkOption... options) throws java.io.IOException
Get file owner.
path
- The Path
options
- The LinkOption
s to use when querying the ownerjava.io.IOException
- If failed to access the file systemFiles.getOwner(Path, LinkOption...)
public static java.lang.Boolean checkFileExists(java.nio.file.Path path, java.nio.file.LinkOption... options)
Checks if a file exists - Note: according to the Java tutorial - Checking a File or Directory:
The methods in the Path class are syntactic, meaning that they operate on the Path instance. But eventually you must access the file system to verify that a particular Path exists, or does not exist. You can do so with the exists(Path, LinkOption...) and the notExists(Path, LinkOption...) methods. Note that !Files.exists(path) is not equivalent to Files.notExists(path). When you are testing a file's existence, three results are possible: - The file is verified to exist. - The file is verified to not exist. - The file's status is unknown. This result can occur when the program does not have access to the file. If both exists and notExists return false, the existence of the file cannot be verified.
path
- The Path
to be testedoptions
- The LinkOption
s to useBoolean.TRUE
/Boolean.FALSE
or null
according to the file status as explained abovepublic static void readFully(java.io.InputStream input, byte[] buffer) throws java.io.IOException
input
- where to read input frombuffer
- destinationjava.io.IOException
- if there is a problem reading the filejava.io.EOFException
- if the number of bytes read was incorrectpublic static void readFully(java.io.InputStream input, byte[] buffer, int offset, int length) throws java.io.IOException
input
- where to read input frombuffer
- destinationoffset
- initial offset into bufferlength
- length to read, must be ≥ 0java.io.IOException
- if there is a problem reading the filejava.io.EOFException
- if the number of bytes read was incorrectpublic static int read(java.io.InputStream input, byte[] buffer) throws java.io.IOException
input
- where to read input frombuffer
- destinationjava.io.IOException
- if a read error occurspublic static int read(java.io.InputStream input, byte[] buffer, int offset, int length) throws java.io.IOException
input
- where to read input frombuffer
- destinationoffset
- initial offset into bufferlength
- length to read - ignored if non-positivejava.io.IOException
- if a read error occurspublic static java.nio.file.attribute.PosixFilePermission validateExcludedPermissions(java.util.Collection<java.nio.file.attribute.PosixFilePermission> perms, java.util.Collection<java.nio.file.attribute.PosixFilePermission> excluded)
perms
- The current PosixFilePermission
s - ignored if null
/emptyexcluded
- The permissions not allowed to exist - ignored if null
/emptyPosixFilePermission
- null
if no violating permission foundpublic static java.nio.file.Path ensureDirectory(java.nio.file.Path path, java.nio.file.LinkOption... options)
path
- The Path
to checkoptions
- The LinkOption
s to use when checking if path is a directoryjava.lang.UnsupportedOperationException
- if input path not a directorypublic static boolean followLinks(java.nio.file.LinkOption... options)
options
- The LinkOption
s - OK if null
/emptytrue
if the link options are null
/empty or do
not contain LinkOption.NOFOLLOW_LINKS
, false
otherwise
(i.e., the array is not empty and contains the special value)public static java.lang.String appendPathComponent(java.lang.String prefix, java.lang.String component)
public static byte[] toByteArray(java.io.InputStream inStream) throws java.io.IOException
java.io.IOException
public static java.util.List<java.lang.String> readAllLines(java.net.URL url) throws java.io.IOException
url
- The URL
to read fromList
of lines in the same order as it was readjava.io.IOException
- If failed to read the linesreadAllLines(InputStream)
public static java.util.List<java.lang.String> readAllLines(java.io.InputStream stream) throws java.io.IOException
stream
- The InputStream
- Note: assumed to
contain UTF-8
encoded dataList
of lines in the same order as it was readjava.io.IOException
- If failed to read the linesreadAllLines(Reader)
public static java.util.List<java.lang.String> readAllLines(java.io.Reader reader) throws java.io.IOException
java.io.IOException
public static java.util.List<java.lang.String> readAllLines(java.io.BufferedReader reader) throws java.io.IOException
reader
- The BufferedReader
to read all linesList
of lines in the same order as it was readjava.io.IOException
- If failed to read the linesreadAllLines(BufferedReader, int)
public static java.util.List<java.lang.String> readAllLines(java.io.BufferedReader reader, int lineCountHint) throws java.io.IOException
reader
- The BufferedReader
to read all lineslineCountHint
- A hint as to the expected number of lines - non-positive
means unknown - in which case some initial default value will be used to
initialize the list used to accumulate the lines.List
of lines in the same order as it was readjava.io.IOException
- If failed to read the lines