Java Posix posixFilePermissions(int mode)

Here you can find the source of posixFilePermissions(int mode)

Description

posix File Permissions

License

Open Source License

Declaration

public static Set<PosixFilePermission> posixFilePermissions(int mode) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Pivotal Software, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*www .j  a v  a  2s .  c o m*/
 *     Pivotal Software, Inc. - initial API and implementation
 *******************************************************************************/

import static java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE;
import static java.nio.file.attribute.PosixFilePermission.GROUP_READ;
import static java.nio.file.attribute.PosixFilePermission.GROUP_WRITE;
import static java.nio.file.attribute.PosixFilePermission.OTHERS_EXECUTE;
import static java.nio.file.attribute.PosixFilePermission.OTHERS_READ;
import static java.nio.file.attribute.PosixFilePermission.OTHERS_WRITE;
import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE;
import static java.nio.file.attribute.PosixFilePermission.OWNER_READ;
import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE;
import java.nio.file.attribute.PosixFilePermission;
import java.util.EnumSet;
import java.util.Set;

public class Main {
    private static final PosixFilePermission[] decodeMap = {
            //This is exactly the reverse order as in the enum.
            //But it seems safer not to rely on that.
            OTHERS_EXECUTE, OTHERS_WRITE, OTHERS_READ, GROUP_EXECUTE, GROUP_WRITE, GROUP_READ, OWNER_EXECUTE,
            OWNER_WRITE, OWNER_READ };

    public static Set<PosixFilePermission> posixFilePermissions(int mode) {
        int mask = 1;
        Set<PosixFilePermission> perms = EnumSet.noneOf(PosixFilePermission.class);
        for (PosixFilePermission flag : decodeMap) {
            if (flag != null && (mask & mode) != 0) {
                perms.add(flag);
            }
            mask = mask << 1;
        }
        return perms;
    }
}

Related

  1. getPosixPermissionsAsInt( Set permissionSet)
  2. givePerm(File f, Set perms)
  3. givePerms(File f, Set perms, boolean recursive)
  4. intFilePermissions(Set permissions)
  5. mapValue(PosixFilePermission p)
  6. setPermissionsToFile(File f, Collection perms)
  7. toOctalFileMode(Set permissions)
  8. toUnixFileMode(Set perms)
  9. unixModeToPosixSet(int unixMode)