public final class IoUtils extends Object
Modifier and Type | Method and Description |
---|---|
static String |
appendPathComponent(String prefix,
String component) |
static Path |
buildPath(Path root,
FileSystem fs,
List<String> namesList)
Build a path from the list of path parts
|
static Path |
buildRelativePath(FileSystem fs,
List<String> namesList)
Build a relative path on the filesystem fs from the path parts in the namesList
|
static Boolean |
checkFileExists(Path path,
LinkOption... options)
Checks if a file exists - Note: according to the
Java tutorial - Checking a File or
Directory:
|
static Boolean |
checkFileExistsAnySymlinks(Path path,
boolean neverFollowSymlinks)
Checks that a file exists with or without following any symlinks.
|
static Path |
chroot(Path newRoot,
Path toSanitize)
Chroot a path under the new root
|
static IOException |
closeQuietly(Closeable... closeables)
Closes a bunch of resources suppressing any
IOException s their Closeable.close() method may have
thrown |
static IOException |
closeQuietly(Closeable c)
Closes the specified
Closeable resource |
static IOException |
closeQuietly(Collection<? extends 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 Path |
getFirstPartsOfPath(Path path,
int partsToExtract)
Extracts the first n parts of the path.
|
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 List<String> |
readAllLines(BufferedReader reader)
Reads all lines until no more available
|
static List<String> |
readAllLines(BufferedReader reader,
int lineCountHint)
Reads all lines until no more available
|
static List<String> |
readAllLines(InputStream stream)
Reads all lines until no more available
|
static List<String> |
readAllLines(Reader reader) |
static List<String> |
readAllLines(URL url)
Reads all lines until no more available
|
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 Path |
removeCdUpAboveRoot(Path toSanitize)
Remove any extra directory ups from the Path
|
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 byte[] |
toByteArray(InputStream inStream) |
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 FileAttribute<?>[] EMPTY_FILE_ATTRIBUTES
public static final String REGFILE_VIEW_ATTR
public static final String DIRECTORY_VIEW_ATTR
public static final String SYMLINK_VIEW_ATTR
public static final String NUMLINKS_VIEW_ATTR
public static final String OTHERFILE_VIEW_ATTR
public static final String EXECUTABLE_VIEW_ATTR
public static final String SIZE_VIEW_ATTR
public static final String OWNER_VIEW_ATTR
public static final String GROUP_VIEW_ATTR
public static final String USERID_VIEW_ATTR
public static final String GROUPID_VIEW_ATTR
public static final String PERMISSIONS_VIEW_ATTR
public static final String ACL_VIEW_ATTR
public static final String FILEKEY_VIEW_ATTR
public static final String CREATE_TIME_VIEW_ATTR
public static final String LASTMOD_TIME_VIEW_ATTR
public static final String LASTACC_TIME_VIEW_ATTR
public static final String EXTENDED_VIEW_ATTR
public static final int DEFAULT_COPY_SIZE
public static final String EOL
public static final Set<StandardOpenOption> WRITEABLE_OPEN_OPTIONS
Set
of StandardOpenOption
-s that indicate an intent to create/modify a filepublic 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 - null
if not exception. If more than one exception occurred, they are added as suppressed exceptions
to the first oneThrowable.getSuppressed()
public static IOException closeQuietly(Closeable c)
Closeable
resourcec
- The resource to close - ignored if null
IOException
when close()
was called - null
if no exception was
thrown (or no resource to close to begin with)public static IOException closeQuietly(Collection<? extends 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(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 Boolean checkFileExistsAnySymlinks(Path path, boolean neverFollowSymlinks)
path
- the path to checkneverFollowSymlinks
- whether to follow symlinkspublic static Path getFirstPartsOfPath(Path path, int partsToExtract)
path
- the path to extract parts ofpartsToExtract
- the number of parts to extractpublic 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)public static byte[] toByteArray(InputStream inStream) throws IOException
IOException
public static List<String> readAllLines(URL url) throws IOException
url
- The URL
to read fromList
of lines in the same order as it was readIOException
- If failed to read the linesreadAllLines(InputStream)
public static List<String> readAllLines(InputStream stream) throws IOException
stream
- The InputStream
- Note: assumed to contain UTF-8
encoded dataList
of lines in the same order as it was readIOException
- If failed to read the linesreadAllLines(Reader)
public static List<String> readAllLines(Reader reader) throws IOException
IOException
public static List<String> readAllLines(BufferedReader reader) throws IOException
reader
- The BufferedReader
to read all linesList
of lines in the same order as it was readIOException
- If failed to read the linesreadAllLines(BufferedReader, int)
public static List<String> readAllLines(BufferedReader reader, int lineCountHint) throws 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 readIOException
- If failed to read the linespublic static Path chroot(Path newRoot, Path toSanitize)
newRoot
- the new roottoSanitize
- the path to sanitize and chrootpublic static Path removeCdUpAboveRoot(Path toSanitize)
toSanitize
- the path to sanitizepublic static Path buildPath(Path root, FileSystem fs, List<String> namesList)
root
- the root pathfs
- the filesystemnamesList
- the parts of the path to buildpublic static Path buildRelativePath(FileSystem fs, List<String> namesList)
fs
- the filesystem for the pathnamesList
- the names listCopyright © 2008–2024 The Apache Software Foundation. All rights reserved.