Java Posix getFileMode(Set posixPermissions)

Here you can find the source of getFileMode(Set posixPermissions)

Description

Given a set of PosixFilePermissions, returns an integer with the corresponding permission bits set

License

Apache License

Parameter

Parameter Description
posixPermissions Set of permissions to represent as an integer

Return

Integer representing the permission bits

Declaration

public static int getFileMode(Set<PosixFilePermission> posixPermissions) 

Method Source Code


//package com.java2s;
/*//from   w  w w . j a v  a 2  s  .c  o m
 * Copyright 2018 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.nio.file.attribute.PosixFilePermission;

import java.util.Set;

public class Main {
    private static PosixFilePermission[] permissionBits = new PosixFilePermission[] {
            PosixFilePermission.OTHERS_EXECUTE, PosixFilePermission.OTHERS_WRITE, PosixFilePermission.OTHERS_READ,
            PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.GROUP_WRITE, PosixFilePermission.GROUP_READ,
            PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_READ };

    /**
     * Given a set of PosixFilePermissions, returns an integer with the corresponding permission bits
     * set
     * @param posixPermissions Set of permissions to represent as an integer
     * @return Integer representing the permission bits
     */
    public static int getFileMode(Set<PosixFilePermission> posixPermissions) {
        int fileMode = 0;
        for (int i = 0; i < permissionBits.length; i++) {
            if (posixPermissions.contains(permissionBits[i])) {
                fileMode += 1 << i;
            }
        }

        return fileMode;
    }
}

Related

  1. addPermissions(Set permissions, String prefix, long mode)
  2. fromPosixPermission(PosixFilePermission permission)
  3. getPosixFileAttributes(@Nonnull File file)
  4. getPosixFilePermissions(int mode)
  5. getPosixPermissions(File forFile)
  6. getPosixPermissions(int fileMode)