Java Utililty Methods Posix

List of utility methods to do Posix

Description

The list of methods to do Posix are organized into topic(s).

Method

voidaddPermissions(Set permissions, String prefix, long mode)
add Permissions
if ((mode & 1) == 1) {
    permissions.add(PosixFilePermission.valueOf(prefix + "_EXECUTE"));
if ((mode & 2) == 2) {
    permissions.add(PosixFilePermission.valueOf(prefix + "_WRITE"));
if ((mode & 4) == 4) {
    permissions.add(PosixFilePermission.valueOf(prefix + "_READ"));
...
StringfromPosixPermission(PosixFilePermission permission)
from Posix Permission
switch (permission) {
case OWNER_READ:
case GROUP_READ:
case OTHERS_READ:
    return READ;
case OWNER_WRITE:
case GROUP_WRITE:
case OTHERS_WRITE:
...
intgetFileMode(Set posixPermissions)
Given a set of PosixFilePermissions, returns an integer with the corresponding permission bits set
int fileMode = 0;
for (int i = 0; i < permissionBits.length; i++) {
    if (posixPermissions.contains(permissionBits[i])) {
        fileMode += 1 << i;
return fileMode;
PosixFileAttributesgetPosixFileAttributes(@Nonnull File file)
get Posix File Attributes
return Files.readAttributes(file.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
SetgetPosixFilePermissions(int mode)
Convert integer mode to PosixFilePermission object.
Set<PosixFilePermission> result = EnumSet.noneOf(PosixFilePermission.class);
if ((mode & 0400) != 0) {
    result.add(PosixFilePermission.OWNER_READ);
if ((mode & 0200) != 0) {
    result.add(PosixFilePermission.OWNER_WRITE);
if ((mode & 0100) != 0) {
...
SetgetPosixPermissions(File forFile)
get Posix Permissions
if (IS_POSIX) {
    try {
        return Files.getPosixFilePermissions(Paths.get(forFile.toURI()));
    } catch (IOException e) {
        throw new RuntimeException(e);
return new TreeSet<PosixFilePermission>();
...
SetgetPosixPermissions(int fileMode)
Given an integer representing file permissions, returns a set of PosixFilePermissions representing the corresponding permissions.
Set<PosixFilePermission> posixPermissions = new HashSet<>();
for (int i = 0; i < permissionBits.length; i++) {
    int bit = fileMode & (1 << i);
    if (bit != 0) {
        posixPermissions.add(permissionBits[i]);
return posixPermissions;
...
intgetPosixPermissionsAsInt(Set permissionSet)
get Posix Permissions As Int
int result = 0;
if (permissionSet.contains(PosixFilePermission.OWNER_READ)) {
    result = result | O400;
if (permissionSet.contains(PosixFilePermission.OWNER_WRITE)) {
    result = result | O200;
if (permissionSet.contains(PosixFilePermission.OWNER_EXECUTE)) {
...
booleangivePerm(File f, Set perms)
Sets specified permissions to a single file or directory.
try {
    Path p = Paths.get(f.toURI());
    if (OS.indexOf("win") >= 0) {
        p.toFile().setWritable(true);
    } else {
        Files.setPosixFilePermissions(p, perms);
} catch (IOException e) {
...
booleangivePerms(File f, Set perms, boolean recursive)
Sets permissions to file or directory given.
boolean success = givePerm(f, perms);
if (f.isDirectory() && recursive) {
    File[] files = f.listFiles();
    for (File file : files) {
        if (!givePerms(file, perms, recursive)) {
            success = false;
return success;