public final class IoUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_COPY_SIZE |
static CopyOption[] |
EMPTY_COPY_OPTIONS |
static LinkOption[] |
EMPTY_LINK_OPTIONS |
static OpenOption[] |
EMPTY_OPEN_OPTIONS |
static List<String> |
WINDOWS_EXECUTABLE_EXTENSIONS |
| Modifier and Type | Method and Description |
|---|---|
static Boolean |
checkFileExists(Path path,
LinkOption... options)
Checks if a file exists - Note: according to the
Java tutorial - Checking a File or Directory:
|
static void |
closeQuietly(Closeable... closeables) |
static long |
copy(InputStream source,
OutputStream sink) |
static long |
copy(InputStream source,
OutputStream sink,
int bufferSize) |
static Path |
ensureDirectory(Path path,
LinkOption... options) |
static boolean |
followLinks(LinkOption... options) |
static LinkOption[] |
getLinkOptions(boolean followLinks) |
static Set<PosixFilePermission> |
getPermissions(Path path,
LinkOption... options)
If the "posix" view is supported, then it returns
Files.getPosixFilePermissions(Path, LinkOption...), otherwise
uses the getPermissionsFromFile(File) method |
static Set<PosixFilePermission> |
getPermissionsFromFile(File f) |
static boolean |
isWindowsExecutable(String fileName) |
static int |
read(InputStream input,
byte[] buffer)
Read as many bytes as possible until EOF or achieved required length
|
static int |
read(InputStream input,
byte[] buffer,
int offset,
int length)
Read as many bytes as possible until EOF or achieved required length
|
static void |
readFully(InputStream input,
byte[] buffer)
Read the requested number of bytes or fail if there are not enough left.
|
static void |
readFully(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(Path path,
Set<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(File f,
Collection<PosixFilePermission> perms) |
static PosixFilePermission |
validateExcludedPermissions(Collection<PosixFilePermission> perms,
Collection<PosixFilePermission> excluded) |
public static final OpenOption[] EMPTY_OPEN_OPTIONS
public static final CopyOption[] EMPTY_COPY_OPTIONS
public static final LinkOption[] EMPTY_LINK_OPTIONS
public static final int DEFAULT_COPY_SIZE
public static LinkOption[] getLinkOptions(boolean followLinks)
public static long copy(InputStream source, OutputStream sink) throws IOException
IOExceptionpublic static long copy(InputStream source, OutputStream sink, int bufferSize) throws IOException
IOExceptionpublic static void closeQuietly(Closeable... closeables)
public static boolean isWindowsExecutable(String fileName)
fileName - The file name to be evaluated - ignored if null/emptytrue if the file ends in one of the WINDOWS_EXECUTABLE_EXTENSIONSpublic static Set<PosixFilePermission> getPermissions(Path path, LinkOption... options) throws IOException
Files.getPosixFilePermissions(Path, LinkOption...), otherwise
uses the getPermissionsFromFile(File) methodpath - The Pathoptions - The LinkOptions to use when querying the permissionsSet of PosixFilePermissionIOException - If failed to access the file system in order to
retrieve the permissionspublic static Set<PosixFilePermission> getPermissionsFromFile(File f)
f - The File to be checkedSet of PosixFilePermissions 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 void setPermissions(Path path, Set<PosixFilePermission> perms) throws IOException
Files.setPosixFilePermissions(Path, Set), otherwise
uses the setPermissionsToFile(File, Collection) methodpath - The Pathperms - The Set of PosixFilePermissionsIOException - If failed to access the file systempublic static void setPermissionsToFile(File f, Collection<PosixFilePermission> perms)
f - The Fileperms - A Collection of PosixFilePermissions 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 Boolean checkFileExists(Path path, 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 LinkOptions to useBoolean.TRUE/Boolean.FALSE or null
according to the file status as explained abovepublic static void readFully(InputStream input, byte[] buffer) throws IOException
input - where to read input frombuffer - destinationIOException - if there is a problem reading the fileEOFException - if the number of bytes read was incorrectpublic static void readFully(InputStream input, byte[] buffer, int offset, int length) throws IOException
input - where to read input frombuffer - destinationoffset - initial offset into bufferlength - length to read, must be ≥ 0IOException - if there is a problem reading the fileEOFException - if the number of bytes read was incorrectpublic static int read(InputStream input, byte[] buffer) throws IOException
input - where to read input frombuffer - destinationIOException - if a read error occurspublic static int read(InputStream input, byte[] buffer, int offset, int length) throws IOException
input - where to read input frombuffer - destinationoffset - initial offset into bufferlength - length to read - ignored if non-positiveIOException - if a read error occurspublic static PosixFilePermission validateExcludedPermissions(Collection<PosixFilePermission> perms, Collection<PosixFilePermission> excluded)
perms - The current PosixFilePermissions - ignored if null/emptyexcluded - The permissions not allowed to exist - ignored if null/emptyPosixFilePermission - null
if no violating permission foundpublic static Path ensureDirectory(Path path, LinkOption... options)
path - The Path to checkoptions - The LinkOptions to use when checking if path is a directoryUnsupportedOperationException - if input path not a directorypublic static boolean followLinks(LinkOption... options)
options - The LinkOptions - 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)Copyright © 2008–2015 The Apache Software Foundation. All rights reserved.