Package org.apache.sshd.common.util.io
Class IoUtils
- java.lang.Object
-
- org.apache.sshd.common.util.io.IoUtils
-
public final class IoUtils extends Object
TODO Add javadoc- Author:
- Apache MINA SSHD Project
-
-
Field Summary
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringappendPathComponent(String prefix, String component)static BooleancheckFileExists(Path path, LinkOption... options)Checks if a file exists - Note: according to the Java tutorial - Checking a File or Directory:static IOExceptioncloseQuietly(Closeable c)Closes the specifiedCloseableresourcestatic IOExceptioncloseQuietly(Closeable... closeables)Closes a bunch of resources suppressing anyIOExceptions theirCloseable.close()method may have thrownstatic IOExceptioncloseQuietly(Collection<? extends Closeable> closeables)Closes a bunch of resources suppressing anyIOExceptions theirCloseable.close()method may have thrownstatic longcopy(InputStream source, OutputStream sink)static longcopy(InputStream source, OutputStream sink, int bufferSize)static PathensureDirectory(Path path, LinkOption... options)static booleanfollowLinks(LinkOption... options)static byte[]getEOLBytes()static StringgetFileOwner(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 returnsFiles.getPosixFilePermissions(Path, LinkOption...), otherwise uses thegetPermissionsFromFile(File)methodstatic Set<PosixFilePermission>getPermissionsFromFile(File f)static booleanisExecutable(File f)static booleanisWindowsExecutable(String fileName)static intread(InputStream input, byte[] buffer)Read as many bytes as possible until EOF or achieved required lengthstatic intread(InputStream input, byte[] buffer, int offset, int length)Read as many bytes as possible until EOF or achieved required lengthstatic List<String>readAllLines(BufferedReader reader)Reads all lines until no more availablestatic List<String>readAllLines(BufferedReader reader, int lineCountHint)Reads all lines until no more availablestatic List<String>readAllLines(InputStream stream)Reads all lines until no more availablestatic List<String>readAllLines(Reader reader)static List<String>readAllLines(URL url)Reads all lines until no more availablestatic voidreadFully(InputStream input, byte[] buffer)Read the requested number of bytes or fail if there are not enough left.static voidreadFully(InputStream input, byte[] buffer, int offset, int length)Read the requested number of bytes or fail if there are not enough left.static voidsetPermissions(Path path, Set<PosixFilePermission> perms)If the "posix" view is supported, then it invokesFiles.setPosixFilePermissions(Path, Set), otherwise uses thesetPermissionsToFile(File, Collection)methodstatic voidsetPermissionsToFile(File f, Collection<PosixFilePermission> perms)static byte[]toByteArray(InputStream inStream)static PosixFilePermissionvalidateExcludedPermissions(Collection<PosixFilePermission> perms, Collection<PosixFilePermission> excluded)
-
-
-
Field Detail
-
EMPTY_OPEN_OPTIONS
public static final OpenOption[] EMPTY_OPEN_OPTIONS
-
EMPTY_COPY_OPTIONS
public static final CopyOption[] EMPTY_COPY_OPTIONS
-
EMPTY_LINK_OPTIONS
public static final LinkOption[] EMPTY_LINK_OPTIONS
-
EMPTY_FILE_ATTRIBUTES
public static final FileAttribute<?>[] EMPTY_FILE_ATTRIBUTES
-
REGFILE_VIEW_ATTR
public static final String REGFILE_VIEW_ATTR
- See Also:
- Constant Field Values
-
DIRECTORY_VIEW_ATTR
public static final String DIRECTORY_VIEW_ATTR
- See Also:
- Constant Field Values
-
SYMLINK_VIEW_ATTR
public static final String SYMLINK_VIEW_ATTR
- See Also:
- Constant Field Values
-
NUMLINKS_VIEW_ATTR
public static final String NUMLINKS_VIEW_ATTR
- See Also:
- Constant Field Values
-
OTHERFILE_VIEW_ATTR
public static final String OTHERFILE_VIEW_ATTR
- See Also:
- Constant Field Values
-
EXECUTABLE_VIEW_ATTR
public static final String EXECUTABLE_VIEW_ATTR
- See Also:
- Constant Field Values
-
SIZE_VIEW_ATTR
public static final String SIZE_VIEW_ATTR
- See Also:
- Constant Field Values
-
OWNER_VIEW_ATTR
public static final String OWNER_VIEW_ATTR
- See Also:
- Constant Field Values
-
GROUP_VIEW_ATTR
public static final String GROUP_VIEW_ATTR
- See Also:
- Constant Field Values
-
USERID_VIEW_ATTR
public static final String USERID_VIEW_ATTR
- See Also:
- Constant Field Values
-
GROUPID_VIEW_ATTR
public static final String GROUPID_VIEW_ATTR
- See Also:
- Constant Field Values
-
PERMISSIONS_VIEW_ATTR
public static final String PERMISSIONS_VIEW_ATTR
- See Also:
- Constant Field Values
-
ACL_VIEW_ATTR
public static final String ACL_VIEW_ATTR
- See Also:
- Constant Field Values
-
FILEKEY_VIEW_ATTR
public static final String FILEKEY_VIEW_ATTR
- See Also:
- Constant Field Values
-
CREATE_TIME_VIEW_ATTR
public static final String CREATE_TIME_VIEW_ATTR
- See Also:
- Constant Field Values
-
LASTMOD_TIME_VIEW_ATTR
public static final String LASTMOD_TIME_VIEW_ATTR
- See Also:
- Constant Field Values
-
LASTACC_TIME_VIEW_ATTR
public static final String LASTACC_TIME_VIEW_ATTR
- See Also:
- Constant Field Values
-
EXTENDED_VIEW_ATTR
public static final String EXTENDED_VIEW_ATTR
- See Also:
- Constant Field Values
-
DEFAULT_COPY_SIZE
public static final int DEFAULT_COPY_SIZE
Size of preferred work buffer when reading / writing data to / from streams- See Also:
- Constant Field Values
-
EOL
public static final String EOL
The local O/S line separator
-
WRITEABLE_OPEN_OPTIONS
public static final Set<StandardOpenOption> WRITEABLE_OPEN_OPTIONS
ASetofStandardOpenOption-s that indicate an intent to create/modify a file
-
-
Method Detail
-
getEOLBytes
public static byte[] getEOLBytes()
- Returns:
- The local platform line separator bytes as UTF-8. Note: each call returns a new instance in order to avoid inadvertent changes in shared objects
- See Also:
EOL
-
getLinkOptions
public static LinkOption[] getLinkOptions(boolean followLinks)
-
copy
public static long copy(InputStream source, OutputStream sink) throws IOException
- Throws:
IOException
-
copy
public static long copy(InputStream source, OutputStream sink, int bufferSize) throws IOException
- Throws:
IOException
-
closeQuietly
public static IOException closeQuietly(Closeable... closeables)
Closes a bunch of resources suppressing anyIOExceptions theirCloseable.close()method may have thrown- Parameters:
closeables- TheCloseables to close- Returns:
- The first
IOExceptionthat occurred during closing of a resource -nullif not exception. If more than one exception occurred, they are added as suppressed exceptions to the first one - See Also:
Throwable.getSuppressed()
-
closeQuietly
public static IOException closeQuietly(Closeable c)
Closes the specifiedCloseableresource- Parameters:
c- The resource to close - ignored ifnull- Returns:
- The thrown
IOExceptionwhenclose()was called -nullif no exception was thrown (or no resource to close to begin with)
-
closeQuietly
public static IOException closeQuietly(Collection<? extends Closeable> closeables)
Closes a bunch of resources suppressing anyIOExceptions theirCloseable.close()method may have thrown- Parameters:
closeables- TheCloseables to close- Returns:
- The first
IOExceptionthat occurred during closing of a resource -nullif not exception. If more than one exception occurred, they are added as suppressed exceptions to the first one - See Also:
Throwable.getSuppressed()
-
isWindowsExecutable
public static boolean isWindowsExecutable(String fileName)
- Parameters:
fileName- The file name to be evaluated - ignored ifnull/empty- Returns:
trueif the file ends in one of theWINDOWS_EXECUTABLE_EXTENSIONS
-
getPermissions
public static Set<PosixFilePermission> getPermissions(Path path, LinkOption... options) throws IOException
If the "posix" view is supported, then it returnsFiles.getPosixFilePermissions(Path, LinkOption...), otherwise uses thegetPermissionsFromFile(File)method- Parameters:
path- ThePathoptions- TheLinkOptions to use when querying the permissions- Returns:
- A
SetofPosixFilePermission - Throws:
IOException- If failed to access the file system in order to retrieve the permissions
-
getPermissionsFromFile
public static Set<PosixFilePermission> getPermissionsFromFile(File f)
- Parameters:
f- TheFileto be checked- Returns:
- A
SetofPosixFilePermissions based on whether the file is readable/writable/executable. If so, then all the relevant permissions are set (i.e., owner, group and others)
-
isExecutable
public static boolean isExecutable(File f)
-
setPermissions
public static void setPermissions(Path path, Set<PosixFilePermission> perms) throws IOException
If the "posix" view is supported, then it invokesFiles.setPosixFilePermissions(Path, Set), otherwise uses thesetPermissionsToFile(File, Collection)method- Parameters:
path- ThePathperms- TheSetofPosixFilePermissions- Throws:
IOException- If failed to access the file system
-
setPermissionsToFile
public static void setPermissionsToFile(File f, Collection<PosixFilePermission> perms)
- Parameters:
f- TheFileperms- ACollectionofPosixFilePermissions 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 set
-
getFileOwner
public static String getFileOwner(Path path, LinkOption... options) throws IOException
Get file owner.
- Parameters:
path- ThePathoptions- TheLinkOptions to use when querying the owner- Returns:
- Owner of the file or null if unsupported. Note: for Windows it strips any prepended domain or group name
- Throws:
IOException- If failed to access the file system- See Also:
Files.getOwner(Path, LinkOption...)
-
checkFileExists
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.
- Parameters:
path- ThePathto be testedoptions- TheLinkOptions to use- Returns:
Boolean.TRUE/Boolean.FALSEornullaccording to the file status as explained above
-
readFully
public static void readFully(InputStream input, byte[] buffer) throws IOException
Read the requested number of bytes or fail if there are not enough left.- Parameters:
input- where to read input frombuffer- destination- Throws:
IOException- if there is a problem reading the fileEOFException- if the number of bytes read was incorrect
-
readFully
public static void readFully(InputStream input, byte[] buffer, int offset, int length) throws IOException
Read the requested number of bytes or fail if there are not enough left.- Parameters:
input- where to read input frombuffer- destinationoffset- initial offset into bufferlength- length to read, must be ≥ 0- Throws:
IOException- if there is a problem reading the fileEOFException- if the number of bytes read was incorrect
-
read
public static int read(InputStream input, byte[] buffer) throws IOException
Read as many bytes as possible until EOF or achieved required length- Parameters:
input- where to read input frombuffer- destination- Returns:
- actual length read; may be less than requested if EOF was reached
- Throws:
IOException- if a read error occurs
-
read
public static int read(InputStream input, byte[] buffer, int offset, int length) throws IOException
Read as many bytes as possible until EOF or achieved required length- Parameters:
input- where to read input frombuffer- destinationoffset- initial offset into bufferlength- length to read - ignored if non-positive- Returns:
- actual length read; may be less than requested if EOF was reached
- Throws:
IOException- if a read error occurs
-
validateExcludedPermissions
public static PosixFilePermission validateExcludedPermissions(Collection<PosixFilePermission> perms, Collection<PosixFilePermission> excluded)
- Parameters:
perms- The currentPosixFilePermissions - ignored ifnull/emptyexcluded- The permissions not allowed to exist - ignored ifnull/empty- Returns:
- The violating
PosixFilePermission-nullif no violating permission found
-
ensureDirectory
public static Path ensureDirectory(Path path, LinkOption... options)
- Parameters:
path- ThePathto checkoptions- TheLinkOptions to use when checking if path is a directory- Returns:
- The same input path if it is a directory
- Throws:
UnsupportedOperationException- if input path not a directory
-
followLinks
public static boolean followLinks(LinkOption... options)
- Parameters:
options- TheLinkOptions - OK ifnull/empty- Returns:
trueif the link options arenull/empty or do not containLinkOption.NOFOLLOW_LINKS,falseotherwise (i.e., the array is not empty and contains the special value)
-
toByteArray
public static byte[] toByteArray(InputStream inStream) throws IOException
- Throws:
IOException
-
readAllLines
public static List<String> readAllLines(URL url) throws IOException
Reads all lines until no more available- Parameters:
url- TheURLto read from- Returns:
- The
Listof lines in the same order as it was read - Throws:
IOException- If failed to read the lines- See Also:
readAllLines(InputStream)
-
readAllLines
public static List<String> readAllLines(InputStream stream) throws IOException
Reads all lines until no more available- Parameters:
stream- TheInputStream- Note: assumed to containUTF-8encoded data- Returns:
- The
Listof lines in the same order as it was read - Throws:
IOException- If failed to read the lines- See Also:
readAllLines(Reader)
-
readAllLines
public static List<String> readAllLines(Reader reader) throws IOException
- Throws:
IOException
-
readAllLines
public static List<String> readAllLines(BufferedReader reader) throws IOException
Reads all lines until no more available- Parameters:
reader- TheBufferedReaderto read all lines- Returns:
- The
Listof lines in the same order as it was read - Throws:
IOException- If failed to read the lines- See Also:
readAllLines(BufferedReader, int)
-
readAllLines
public static List<String> readAllLines(BufferedReader reader, int lineCountHint) throws IOException
Reads all lines until no more available- Parameters:
reader- TheBufferedReaderto 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.- Returns:
- The
Listof lines in the same order as it was read - Throws:
IOException- If failed to read the lines
-
-