public final class IoUtils extends 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 CopyOption[] |
EMPTY_COPY_OPTIONS |
static LinkOption[] |
EMPTY_LINK_OPTIONS |
static OpenOption[] |
EMPTY_OPEN_OPTIONS |
static String |
EOL
The local O/S line separator
|
static List<String> |
WINDOWS_EXECUTABLE_EXTENSIONS |
Modifier and Type | Method and Description |
---|---|
static String |
appendPathComponent(String prefix,
String component) |
static Boolean |
checkFileExists(Path path,
LinkOption... options)
Checks if a file exists - Note: according to the
Java tutorial - Checking a File or Directory:
|
static IOException |
closeQuietly(Closeable... closeables)
Closes a bunch of resources suppressing any
IOException s their
Closeable.close() method may have thrown |
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 byte[] |
getEOLBytes() |
static String |
getFileOwner(Path path,
LinkOption... options)
Get file owner.
|
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 |
isExecutable(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 final String EOL
public static byte[] getEOLBytes()
EOL
public static LinkOption[] getLinkOptions(boolean followLinks)
public static long copy(InputStream source, OutputStream sink) throws IOException
IOException
public static long copy(InputStream source, OutputStream sink, int bufferSize) throws IOException
IOException
public static IOException closeQuietly(Closeable... closeables)
IOException
s their
Closeable.close()
method may have throwncloseables
- The Closeable
s to closeIOException
that occurred during closing
of a resource - if more than one exception occurred, they are added as
suppressed exceptions to the first oneThrowable.getSuppressed()
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_EXTENSIONS
public static Set<PosixFilePermission> getPermissions(Path path, LinkOption... options) throws 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
IOException
- 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 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(File f)
public static void setPermissions(Path path, Set<PosixFilePermission> perms) throws IOException
Files.setPosixFilePermissions(Path, Set)
, otherwise
uses the setPermissionsToFile(File, Collection)
methodpath
- The Path
perms
- The Set
of PosixFilePermission
sIOException
- If failed to access the file systempublic static void setPermissionsToFile(File f, Collection<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 String getFileOwner(Path path, LinkOption... options) throws IOException
Get file owner.
path
- The Path
options
- The LinkOption
s to use when querying the ownerIOException
- If failed to access the file systemFiles.getOwner(Path, LinkOption...)
public 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 LinkOption
s 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 PosixFilePermission
s - 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 LinkOption
s to use when checking if path is a directoryUnsupportedOperationException
- if input path not a directorypublic static boolean followLinks(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)Copyright © 2008–2016 The Apache Software Foundation. All rights reserved.