Top Description Fields Constructors Methods
org.apache.sshd.common.util.io

public final Class IoUtils

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .ByteArrayOutputStream, .Closeable, .EOFException, .File, .IOException, .InputStream, .InputStreamReader, .OutputStream, .Reader, java.net.URL, java.nio.charset.StandardCharsets, java.nio.file.CopyOption, .FileSystem, .Files, .LinkOption, .NoSuchFileException, .OpenOption, .Path, .StandardOpenOption, java.nio.file.attribute.BasicFileAttributes, .FileAttribute, .PosixFilePermission, .UserPrincipal, java.util.ArrayList, .Arrays, .Collection, .Collections, .EnumSet, .Iterator, .List, .Objects, .Set, org.apache.sshd.common.util.ExceptionUtils, .GenericUtils, .OsUtils

TODO Add javadoc
Author
Apache MINA SSHD Project

Field Summary

Modifier and TypeField and Description
public static final String
public static final String
public static final int
DEFAULT_COPY_SIZE

Size of preferred work buffer when reading / writing data to / from streams

public static final String
public static final CopyOption[]
public static final FileAttribute<?>[]
public static final OpenOption[]
public static final String
EOL

The local O/S line separator

private static final byte[]
public static final String
public static final String
public static final String
public static final String
public static final String
public static final String
public static final String
private static final LinkOption[]
public static final String
public static final String
public static final String
public static final String
public static final String
public static final String
public static final List<String>
public static final Set<StandardOpenOption>
WRITEABLE_OPEN_OPTIONS

A Set of StandardOpenOption-s that indicate an intent to create/modify a file

Constructor Summary

AccessConstructor and Description
private
IoUtils()

Private Constructor

Method Summary

Modifier and TypeMethod and Description
public static String
appendPathComponent(String prefix, String component)

public static Path

Returns:

the built path
buildPath
(Path
the root path
root
,
FileSystem
the filesystem
fs
,
List<String>
the parts of the path to build
namesList
)

Build a path from the list of path parts

public static Path

Returns:

the built path
buildRelativePath
(FileSystem
the filesystem for the path
fs
,
List<String>
the names list
namesList
)

Build a relative path on the filesystem fs from the path parts in the namesList

public static Boolean

Returns:

Boolean#TRUE/Boolean#FALSE or null according to the file status as explained above
checkFileExists
(Path
The Path to be tested
path
,
LinkOption...
The LinkOptions to use
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.
public static Path

Returns:

the chrooted path under the newRoot filesystem
chroot
(Path
the new root
newRoot
,
Path
the path to sanitize and chroot
toSanitize
)

Chroot a path under the new root

public static IOException

Returns:

The first IOException 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 one
closeQuietly
(Closeable...
The Closeables to close
closeables
)

Closes a bunch of resources suppressing any IOExceptions their Closeable#close() method may have thrown

public static IOException

Returns:

The thrown IOException when close() was called - null if no exception was thrown (or no resource to close to begin with)
closeQuietly
(Closeable
The resource to close - ignored if null
c
)

Closes the specified Closeable resource

public static IOException

Returns:

The first IOException 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 one
closeQuietly
(Collection<? extends Closeable>
The Closeables to close
closeables
)

Closes a bunch of resources suppressing any IOExceptions their Closeable#close() method may have thrown

public static long
public static long
copy(InputStream source, OutputStream sink, int bufferSize)

public static Path

Returns:

The same input path if it is a directory
ensureDirectory
(Path
The Path to check
path
,
LinkOption...
The LinkOptions to use when checking if path is a directory
options
)

public static byte[]

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

getEOLBytes
()

public static String

Returns:

Owner of the file or null if unsupported

Note

for Windows it strips any prepended domain or group name

getFileOwner
(Path
The Path
path
,
LinkOption...
The LinkOptions to use when querying the owner
options
)

Get file owner.

public static Path

Returns:

the extracted path
getFirstPartsOfPath
(Path
the path to extract parts of
path
,
int
the number of parts to extract
partsToExtract
)

Extracts the first n parts of the path.

public static LinkOption[]
getLinkOptions(boolean followLinks)

public static Set<PosixFilePermission>

Returns:

A Set of PosixFilePermission
getPermissions
(Path
The Path
path
,
LinkOption...
The LinkOptions to use when querying the permissions
options
)

If the "posix" view is supported, then it returns Files#getPosixFilePermissions(Path, LinkOption...), otherwise uses the getPermissionsFromFile(File) method

public static Set<PosixFilePermission>

Returns:

A Set 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)
getPermissionsFromFile
(File
The File to be checked
f
)

public static boolean
public static boolean

Returns:

true if the file ends in one of the WINDOWS_EXECUTABLE_EXTENSIONS
isWindowsExecutable
(String
The file name to be evaluated - ignored if null/empty
fileName
)

public static int

Returns:

actual length read; may be less than requested if EOF was reached
read
(InputStream
where to read input from
input
,
byte[]
destination
buffer
)

Read as many bytes as possible until EOF or achieved required length

public static int

Returns:

actual length read; may be less than requested if EOF was reached
read
(InputStream
where to read input from
input
,
byte[]
destination
buffer
,
int
initial offset into buffer
offset
,
int
length to read - ignored if non-positive
length
)

Read as many bytes as possible until EOF or achieved required length

public static List<String>

Returns:

The List of lines in the same order as it was read
readAllLines
(URL
The URL to read from
url
)

Reads all lines until no more available

public static List<String>

Returns:

The List of lines in the same order as it was read
readAllLines
(InputStream
The InputStream - Note: assumed to contain UTF-8 encoded data
stream
)

Reads all lines until no more available

public static List<String>
public static List<String>

Returns:

The List of lines in the same order as it was read
readAllLines
(BufferedReader
The BufferedReader to read all lines
reader
)

Reads all lines until no more available

public static List<String>

Returns:

The List of lines in the same order as it was read
readAllLines
(BufferedReader
The BufferedReader to read all lines
reader
,
int
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.
lineCountHint
)

Reads all lines until no more available

public static void
readFully(InputStream
where to read input from
input
,
byte[]
destination
buffer
)

Read the requested number of bytes or fail if there are not enough left.

public static void
readFully(InputStream
where to read input from
input
,
byte[]
destination
buffer
,
int
initial offset into buffer
offset
,
int
length to read, must be ≥ 0
length
)

Read the requested number of bytes or fail if there are not enough left.

public static Path

Returns:

the sanitized path
removeCdUpAboveRoot
(Path
the path to sanitize
toSanitize
)

Remove any extra directory ups from the Path

private static List<String>
public static void
setPermissions(Path
The 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

public static void
setPermissionsToFile(File
The File
f
,
Collection<PosixFilePermission>
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 set

perms
)

public static byte[]
public static PosixFilePermission

Returns:

The violating PosixFilePermission - null if no violating permission found
validateExcludedPermissions
(Collection<PosixFilePermission>
The current PosixFilePermissions - ignored if null/empty
perms
,
Collection<PosixFilePermission>
The permissions not allowed to exist - ignored if null/empty
excluded
)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ACL_VIEW_ATTRback to summary
public static final String ACL_VIEW_ATTR
CREATE_TIME_VIEW_ATTRback to summary
public static final String CREATE_TIME_VIEW_ATTR
DEFAULT_COPY_SIZEback to summary
public static final int DEFAULT_COPY_SIZE

Size of preferred work buffer when reading / writing data to / from streams

DIRECTORY_VIEW_ATTRback to summary
public static final String DIRECTORY_VIEW_ATTR
EMPTY_COPY_OPTIONSback to summary
public static final CopyOption[] EMPTY_COPY_OPTIONS
EMPTY_FILE_ATTRIBUTESback to summary
public static final FileAttribute<?>[] EMPTY_FILE_ATTRIBUTES
EMPTY_OPEN_OPTIONSback to summary
public static final OpenOption[] EMPTY_OPEN_OPTIONS
EOLback to summary
public static final String EOL

The local O/S line separator

EOL_BYTESback to summary
private static final byte[] EOL_BYTES
EXECUTABLE_VIEW_ATTRback to summary
public static final String EXECUTABLE_VIEW_ATTR
EXTENDED_VIEW_ATTRback to summary
public static final String EXTENDED_VIEW_ATTR
FILEKEY_VIEW_ATTRback to summary
public static final String FILEKEY_VIEW_ATTR
GROUP_VIEW_ATTRback to summary
public static final String GROUP_VIEW_ATTR
GROUPID_VIEW_ATTRback to summary
public static final String GROUPID_VIEW_ATTR
LASTACC_TIME_VIEW_ATTRback to summary
public static final String LASTACC_TIME_VIEW_ATTR
LASTMOD_TIME_VIEW_ATTRback to summary
public static final String LASTMOD_TIME_VIEW_ATTR
NO_FOLLOW_OPTIONSback to summary
private static final LinkOption[] NO_FOLLOW_OPTIONS
OTHERFILE_VIEW_ATTRback to summary
public static final String OTHERFILE_VIEW_ATTR
OWNER_VIEW_ATTRback to summary
public static final String OWNER_VIEW_ATTR
PERMISSIONS_VIEW_ATTRback to summary
public static final String PERMISSIONS_VIEW_ATTR
REGFILE_VIEW_ATTRback to summary
public static final String REGFILE_VIEW_ATTR
SIZE_VIEW_ATTRback to summary
public static final String SIZE_VIEW_ATTR
USERID_VIEW_ATTRback to summary
public static final String USERID_VIEW_ATTR
WINDOWS_EXECUTABLE_EXTENSIONSback to summary
public static final List<String> WINDOWS_EXECUTABLE_EXTENSIONS
WRITEABLE_OPEN_OPTIONSback to summary
public static final Set<StandardOpenOption> WRITEABLE_OPEN_OPTIONS

A Set of StandardOpenOption-s that indicate an intent to create/modify a file

Constructor Detail

IoUtilsback to summary
private IoUtils()

Private Constructor

Method Detail

appendPathComponentback to summary
public static String appendPathComponent(String prefix, String component)
buildPathback to summary
public static Path buildPath(Path root, FileSystem fs, List<String> namesList)

Build a path from the list of path parts

Parameters
root:Path

the root path

fs:FileSystem

the filesystem

namesList:List<String>

the parts of the path to build

Returns:Path

the built path

buildRelativePathback to summary
public static Path buildRelativePath(FileSystem fs, List<String> namesList)

Build a relative path on the filesystem fs from the path parts in the namesList

Parameters
fs:FileSystem

the filesystem for the path

namesList:List<String>

the names list

Returns:Path

the built path

checkFileExistsback to summary
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:Path

The Path to be tested

options:LinkOption[]

The LinkOptions to use

Returns:Boolean

Boolean#TRUE/Boolean#FALSE or null according to the file status as explained above

chrootback to summary
public static Path chroot(Path newRoot, Path toSanitize)

Chroot a path under the new root

Parameters
newRoot:Path

the new root

toSanitize:Path

the path to sanitize and chroot

Returns:Path

the chrooted path under the newRoot filesystem

closeQuietlyback to summary
public static IOException closeQuietly(Closeable... closeables)

Closes a bunch of resources suppressing any IOExceptions their Closeable#close() method may have thrown

Parameters
closeables:Closeable[]

The Closeables to close

Returns:IOException

The first IOException 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 one

Annotations
@SuppressWarnings:ThrowableResultOfMethodCallIgnored
See Also
Throwable#getSuppressed()
closeQuietlyback to summary
public static IOException closeQuietly(Closeable c)

Closes the specified Closeable resource

Parameters
c:Closeable

The resource to close - ignored if null

Returns:IOException

The thrown IOException when close() was called - null if no exception was thrown (or no resource to close to begin with)

Annotations
@SuppressWarnings:ThrowableResultOfMethodCallIgnored
closeQuietlyback to summary
public static IOException closeQuietly(Collection<? extends Closeable> closeables)

Closes a bunch of resources suppressing any IOExceptions their Closeable#close() method may have thrown

Parameters
closeables:Collection<? extends Closeable>

The Closeables to close

Returns:IOException

The first IOException 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 one

Annotations
@SuppressWarnings:ThrowableResultOfMethodCallIgnored
See Also
Throwable#getSuppressed()
copyback to summary
public static long copy(InputStream source, OutputStream sink) throws IOException
copyback to summary
public static long copy(InputStream source, OutputStream sink, int bufferSize) throws IOException
ensureDirectoryback to summary
public static Path ensureDirectory(Path path, LinkOption... options)
Parameters
path:Path

The Path to check

options:LinkOption[]

The LinkOptions to use when checking if path is a directory

Returns:Path

The same input path if it is a directory

Exceptions
UnsupportedOperationException:
if input path not a directory
getEOLBytesback to summary
public static byte[] getEOLBytes()
Returns:byte[]

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
getFileOwnerback to summary
public static String getFileOwner(Path path, LinkOption... options) throws IOException

Get file owner.

Parameters
path:Path

The Path

options:LinkOption[]

The LinkOptions to use when querying the owner

Returns:String

Owner of the file or null if unsupported

Note

for Windows it strips any prepended domain or group name

Exceptions
IOException:
If failed to access the file system
See Also
Files#getOwner(Path, LinkOption...)
getFirstPartsOfPathback to summary
public static Path getFirstPartsOfPath(Path path, int partsToExtract)

Extracts the first n parts of the path. For example
("/home/test/test12", 1) returns "/home",
("/home/test", 1) returns "/home/test"
etc.

Parameters
path:Path

the path to extract parts of

partsToExtract:int

the number of parts to extract

Returns:Path

the extracted path

getLinkOptionsback to summary
public static LinkOption[] getLinkOptions(boolean followLinks)
getPermissionsback to summary
public static Set<PosixFilePermission> getPermissions(Path path, LinkOption... options) throws IOException

If the "posix" view is supported, then it returns Files#getPosixFilePermissions(Path, LinkOption...), otherwise uses the getPermissionsFromFile(File) method

Parameters
path:Path

The Path

options:LinkOption[]

The LinkOptions to use when querying the permissions

Returns:Set<PosixFilePermission>

A Set of PosixFilePermission

Exceptions
IOException:
If failed to access the file system in order to retrieve the permissions
getPermissionsFromFileback to summary
public static Set<PosixFilePermission> getPermissionsFromFile(File f)
Parameters
f:File

The File to be checked

Returns:Set<PosixFilePermission>

A Set 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)

isExecutableback to summary
public static boolean isExecutable(File f)
isWindowsExecutableback to summary
public static boolean isWindowsExecutable(String fileName)
Parameters
fileName:String

The file name to be evaluated - ignored if null/empty

Returns:boolean

true if the file ends in one of the WINDOWS_EXECUTABLE_EXTENSIONS

readback to summary
public static int read(InputStream input, byte[] buffer) throws IOException

Read as many bytes as possible until EOF or achieved required length

Parameters
input:InputStream

where to read input from

buffer:byte[]

destination

Returns:int

actual length read; may be less than requested if EOF was reached

Exceptions
IOException:
if a read error occurs
readback to summary
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:InputStream

where to read input from

buffer:byte[]

destination

offset:int

initial offset into buffer

length:int

length to read - ignored if non-positive

Returns:int

actual length read; may be less than requested if EOF was reached

Exceptions
IOException:
if a read error occurs
readAllLinesback to summary
public static List<String> readAllLines(URL url) throws IOException

Reads all lines until no more available

Parameters
url:URL

The URL to read from

Returns:List<String>

The List of lines in the same order as it was read

Exceptions
IOException:
If failed to read the lines
See Also
readAllLines(InputStream)
readAllLinesback to summary
public static List<String> readAllLines(InputStream stream) throws IOException

Reads all lines until no more available

Parameters
stream:InputStream

The InputStream - Note: assumed to contain UTF-8 encoded data

Returns:List<String>

The List of lines in the same order as it was read

Exceptions
IOException:
If failed to read the lines
See Also
readAllLines(Reader)
readAllLinesback to summary
public static List<String> readAllLines(Reader reader) throws IOException
readAllLinesback to summary
public static List<String> readAllLines(BufferedReader reader) throws IOException

Reads all lines until no more available

Parameters
reader:BufferedReader

The BufferedReader to read all lines

Returns:List<String>

The List of lines in the same order as it was read

Exceptions
IOException:
If failed to read the lines
See Also
readAllLines(BufferedReader, int)
readAllLinesback to summary
public static List<String> readAllLines(BufferedReader reader, int lineCountHint) throws IOException

Reads all lines until no more available

Parameters
reader:BufferedReader

The BufferedReader to read all lines

lineCountHint:int

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:List<String>

The List of lines in the same order as it was read

Exceptions
IOException:
If failed to read the lines
readFullyback to summary
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:InputStream

where to read input from

buffer:byte[]

destination

Exceptions
IOException:
if there is a problem reading the file
EOFException:
if the number of bytes read was incorrect
readFullyback to summary
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:InputStream

where to read input from

buffer:byte[]

destination

offset:int

initial offset into buffer

length:int

length to read, must be ≥ 0

Exceptions
IOException:
if there is a problem reading the file
EOFException:
if the number of bytes read was incorrect
removeCdUpAboveRootback to summary
public static Path removeCdUpAboveRoot(Path toSanitize)

Remove any extra directory ups from the Path

Parameters
toSanitize:Path

the path to sanitize

Returns:Path

the sanitized path

removeExtraCdUpsback to summary
private static List<String> removeExtraCdUps(Path toResolve)
setPermissionsback to summary
public static void setPermissions(Path path, Set<PosixFilePermission> perms) throws IOException

If the "posix" view is supported, then it invokes Files#setPosixFilePermissions(Path, Set), otherwise uses the setPermissionsToFile(File, Collection) method

Parameters
path:Path

The Path

perms:Set<PosixFilePermission>

The Set of PosixFilePermissions

Exceptions
IOException:
If failed to access the file system
setPermissionsToFileback to summary
public static void setPermissionsToFile(File f, Collection<PosixFilePermission> perms)
Parameters
f:File

The File

perms:Collection<PosixFilePermission>

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 set

toByteArrayback to summary
public static byte[] toByteArray(InputStream inStream) throws IOException
validateExcludedPermissionsback to summary
public static PosixFilePermission validateExcludedPermissions(Collection<PosixFilePermission> perms, Collection<PosixFilePermission> excluded)
Parameters
perms:Collection<PosixFilePermission>

The current PosixFilePermissions - ignored if null/empty

excluded:Collection<PosixFilePermission>

The permissions not allowed to exist - ignored if null/empty

Returns:PosixFilePermission

The violating PosixFilePermission - null if no violating permission found